COMP 110-001: Introduction to Programming, SSI'15
Lab 0
15 points
Assigned: Friday, May 15, 2015
Due: Monday, May 18, 2015 by 11:59pm (EDT)
Description
In Eclipse, create a New Java Project called Lab0
Part 1
On the course webpage, you will find SecondProgram.java. Copy and paste the program into a new Java file named "SecondProgram.java" in Eclipse (in the project Lab0).
Run (the green button with a white arrow in the middle) SecondProgram.java.
Part 2
To read an integer value instead of a string value, we need to invoke the method nextInt(), for example:
- int myInt;
- Scanner keyboard = new Scanner(System.in);
- myInt = keyboard.nextInt();
If you want to read two integers from one input line, you can simply call nextInt() twice and store the returned values in two different variables:
- int myFirstInt, mySecondInt;
- Scanner keyboard = new Scanner(System.in); (If you've already defined the keyboard variable, you don't need this statement.)
- myFirstInt = keyboard.nextInt();
- mySecondInt = keyboard.nextInt();
If you want to do the calculation and assign the result another variable, you can do as follows
- int myResult; // This statement is to declear a variable to hold the result
- myResult = myFirstInt + mySecondInt; // This statement is to get the values from the two variables on the right side of "=", myFirstInt and mySecondInt, do the addition, and then assign the result to the variable on the left side of "=", myResult.
If you want to directly use the method, System.out.println, you can use "()" to tell the program to perform the actions inside of the parentheses first, for example
- System.out.println("The sum is: " + (myFirstInt + mySecondInt));
In this part, you will write a program to complete the following task:
(1) Print a message to ask user to enter two integers,
(2) Read two integers from user's input,
(3) Print out the sum, difference, product, quotient, and remainder of the two integers.
Here is a sample of the input and the output:
----------------------------------------------
Please enter two integers in one line:
>> 3 5
The sum is: 8
The difference is: -2
The product is: 15
The quotient is: 0
The remainer is: 3
----------------------------------------------
Add your code into the java file "SecondProgram.java", after the welcome message.
Hints
- Use another integer variable to store the result, or directly treat the arithmetic expression as a part of the argument of "System.out.println(...)"
- Move the statement, "keyboard.close();", to the end of the file.
Grading
- 5 points Part 1
- 10 points Part 2
How to hand in the assignment
- A file named yourlastname_lab0.jar, where yourlastname is your last name.
The .jar file should include SecondProgram.java.
Your java file should have the appropriate header.
Follow these instructions to create yourlastname_lab0.jar.
- For this assignment, the Main-Class in MANIFEST.MF should be SecondProgram
-
Submit yourlastname_lab0.jar via Sakai by Monday, May 18, 11:59pm.
- If you do not follow these instructions you will not get credit for this lab. Please let
me know if you have any questions about this assignment.