Objectives
After completing this lab, you should be able to:
create a project and class
read data from the keyboard using the Scanner class
make calculations using assignment statements and variables
display results using the print() and println() methods
Lab Activity #1 Convert to seconds
1. Start BlueJ
2. In your CIS 162 Labs folder, create a new project called Lab2.
3. Create a new Class called ConvertToSeconds
4. Edit the top comment section to describe the class and add both of your names as authors.
5. Remove the two automatically generated methods.
6. Create a main method (see Activity 1.5.3 in book)
7. Prompt the user to enter three integers for hours, minutes and seconds.
8. Use variables with meaningful names
9. Calculate and display the number of equivalent seconds
10. When working correctly, show your instructor
Sample Output
Enter hours: 1
Enter minutes: 6
Enter seconds: 6
This is equivalent to 3966 seconds.
Lab Activity #2 Convert from seconds
1. Create a new Class called ConvertFromSeconds
2. Prompt the user to enter one integer for the number of seconds.
3. Calculate and display the number of equivalent hours, minutes and seconds (refer to section 2.5 about integer division and the remainder operator)
hours = numSeconds / 3600;
numSeconds = numSeconds % 3600; // remainder seconds
4. When working correctly, show your instructor
Sample Output
Enter seconds: 20000
This is 5 hours, 33 minutes and 20 seconds.
Lab Activity #3 Convert to dollars
1. Create a new Class called ConvertToDollars
2. Prompt the user to enter four integers for quarters, dimes, nickels and pennies.
3. Calculate and display the equivalent in dollars and cents.
4. When working correctly, show your instructor
NOTES:
Work your solution taking into consideration that 1 dollar contains 100 pennies, 1 quarter contains 25 pennies, etc.
We will learn how to format numbers and currencies later, for now use the following instruction to format the result.
System.out.println(This is equivalent to $ + );
Sample Output
Enter quarters: 6
Enter dimes: 6
Enter nickels: 6
Enter pennies: 6
This is equivalent to $2.46
Grading Criteria
Reviews
There are no reviews yet.