Java Programming HOMEWORK 4 (due 4/19, Thursday) Name: _______________________________ Score:____________ There are 3 questions. Each question has 20 points (so 60 points maximum). You can use Dr.Java to test if your method works correctly. Please print out this homework, and you can write your answer in the printed paper. Or, you may write your answer directly in this file and print it out. Please turn in either one that you want. 1. (20pts) Answer the following questions. 1) What is a repetition statement? (what it is, how it works, give an example) 2) What is a conditional statement? (what it is, how it works, give an example) 3) Write an expression to calculate (use method of Math class) 4) Write a statement to create an object of Random class. Object name is rand. 5) Write a statement to declare a variable named flag, whose type is Boolean. 1 2. (20pts). Please write the output messages in a box after executing following Java programs. You may want to write “none” if no messages are printed out, or “infinite loop” if the loop does not end. (Assume that each program is written in main method.) (Example) int number = 3; for (int i=0; i<number; i++) { System.out.println( “yes”); } yes yes yes 1) int number = 3; for (int k=number; k<=number; k++) { System.out.println( “yes”); } 2) int number = 5; for (int k=0; k<number; k++) { if( k > 2 ) System.out.println( “yes”); else System.out.println( “no”); } 3) int number = 3; for (int a=0; a<number; a++) { for (int b=0; b<number; b++) { System.out.println( “yes”); } } 2 4) int min = 2; int number = 10; while ( number > min ) { if ( number % 2 == 0 ) { System.out.println( “yes”); } number = number – 1; } 5) int number = 3; boolean quit = false; while ( quit != true ) { if( number > 0 ) { System.out.println( “yes”); } else { quit = true; } number = number – 1; } 3 3. (20pts). Complete a class, named MyCashRegister. The class will calculate the total price of items bought by a customer and print out the number of items and total price as a receipt for the customer. If the customer bought more than $100, then he/she gets 5% of discount from the total price. For example, the customer who bought $150 of items will pay 150 x 0.05 = $142.5. *** For question 3, please turn in MyCashRegister.java with homework 4 Here is what you have to do … 1) Your program will keep asking a customer to enter the price (double type) of items he/she bought. 2) Your program will calculate the total price (double type) of the items. 3) When a customer enters 0, then the program will stop asking. 4) Then, your program will print out the number of items the customer bought and final price with either discount or no discount (based on the total price). Here is the sample output. (your output should be similar to this.) Example 1 Welcome to Chonho Super Market. Please enter the price of item > 25.5 Please enter the price of item > 9.95 Please enter the price of item > 0 You bought 2 items and the price is $35 Thank you for your shopping. Example 2 Welcome to Chonho Super Market. Please enter the price of item > 65 Please enter the price of item > 25.45 Please enter the price of item > 59.55 Please enter the price of item > 0 You bought 3 items and price is $142.5 Thank you for your shopping. 4 You can download this file at http://www.cs.umb.edu/~chonho/teaching/2007/files/MyCashRegister.java import acm.io.*; import java.awt.*; import javax.swing.*; public class MyCashRegister { public static void main(String[] args) { JFrame frame = new JFrame("MyCashRegister"); IOConsole console = new IOConsole(); frame.getContentPane().add(BorderLayout.CENTER, console); frame.setSize(500, 300); frame.setVisible(true); // print out welcome message // variable declaration here (I give you one of them) // you may want to have three more variables (price, total, count) boolean quit = false; // In the loop, you will ask customers to enter item price // and also keep calculating the number of items and total price while( quit != true ) { } // print out the number of items and the total price } } 5