Lab 2: Fundamental Data Types
CS 141: Introduction to Programming,
Fall 2015
This lab is due Wednesday, Sept 9. Be prepared to demo at the
beginning of lab.
Note, no class on Monday.
Programming Problems
Do the following problems from the text. Create a separate Netbeans project for each.:
- p. 125, problem 12: String Manipulator (not to be handed in)
- p. 126, problem 17: Word Game (not to be handed in) This is a type of Mad Lib. Have fun - make up your own story from the words the user enters.
- p. 125, problem 13: Restaurant Bill. Be careful with rounding and output format. The tax and tip should be rounded separately. See the sample
run below. You may want to use the Math.round() function (ask instructor) and System.out.printf.
Your program should look like this sample run
Please enter the charge for the meal: $11.71
Meal charge: $ 11.71
Tax : $ 0.88
Tip : $ 2.27
Total : $ 14.86
After you have completed problem 13,
expand your program to include following functionality:
- Prompt the user for the amount you give the waiter to pay for your bill.
- Based on the amount you give and the amount of the bill, the program computes and displays the total amount of change
together with the breakdown into bills and coins (i.e. # of
dollars, quarters, dimes, nickels, pennies) that must be returned to you.
Now, when you run your code,it should look like:
Please enter the charge for the meal: $11.71
Meal charge: $ 11.71
Tax : $ 0.88
Tip : $ 2.27
Total : $ 14.86
Enter your payment: $20
Your total change is $5.14
dollars = 5
quarters = 0
dimes = 1
nickels = 0
pennies = 4
This is harder to do than you might think. Be
careful with rounding and formatting. Suggested strategy:
- Compute the total amount of change (for example, suppose the amount of change is 11.56)
- Convert to cents as an integer: (cents = 1156)
- Do an integer divide by 100 to get the dollars. (result: dollars = 11)
- Use the "mod" function to get the remainder. (remainder = 56) In Java, the symbol for the mod function is %.
- Do an integer divide of the remainder by 25 to get the quarters. ( quarters = 2)
- Use the "mod" function to get the new remainder. (remainder = 6)
- Repeat for dimes, nickels, pennies. (in this example, the result should be 0 dimes, 1 nickel, 1 penny)
- Print out the results.
Develop Good Programming Habits
Formatting: Make it a habit to format your Java code. Do this as you are working because it will make it easier to see the program structure which, in turn,
will make it easier to locate and debug errors.
Commenting: Make it a habit to comment your code, including
placing the following at the top of your code:
- Your name.
- Name of lab (i.e. Lab 2).
- Name of the problem (e.g. Word Game).
- Date.
- A brief description of what the program does.
Deliverables
You are to turn in the expanded version of problem 13. To receive full credit on this lab, you need to:
-
Make sure that your Java code:
- Is well commented.
- Is well formatted, i.e. proper indentation and line returns.
- When executed, the Input/Output (I/O) is formatted as shown in the sample runs. (If you use dialog boxes instead of
the Scanner object, the user input will be a little different).
-
Demonstrate your program to the instructor or lab assistant by the end of lab on the due date. You will be asked to
display your commented/formatted code and to run the code. If your code is not properly formatted or does not give the appropriate output,
you will be given the opportunity to correct the code but only if there is still time to make corrections before the due date/time.
This is why it is to your benefit to complete and demonstrate your code early.
You also may be asked questions about what the code is doing, how it works, and
how it might be changed slightly to alter the output.
-
Submit your code to Wise:
- For this lab, you only need to submit the .java file(s). That is, close Netbeans and navigate to your Netbeans project folder.
Here, you will see the "src" folder which contains your .java file. Find the .java file
and attach it your Wise submission.
It is also suggested that you delete any old java files in the src folder that are not part of the
final program.
-->