Programming Fundamentals I Java Programming Spring 2009 Lab 03 Instructor: Xuan Tung Hoang tung_hx@icu.ac.kr TA: Tran Minh Trung trungtm@icu.ac.kt Content Multiplication test Duplicate elimination Homework #3 XuanTung Hoang 2 Exercise 1: Multiplication test Write a program to check the capability of user to perform simple multiplication as following Use a Random object to produce two positive one-digit integ ers. The program should then prompt the user with a question, such as How much is 6 times 7? The user then inputs the answer. The program checks the user’s answer. If it is correct If the answer is wrong Display the message "Very good!" Ask another multiplication question Display the message "No. Please try again." Let the student try the same question repeatedly until the student finally gets it right. A separate method should be used to generate each new question. This method should be called once when the application begins execution and each time the user answers the question correctly. XuanTung Hoang 3 Exercise 1: Template (1) // Multiply.java // Program generates single digit multiplication problems import java.util.*; public class Multiply { Random randomNumbers = new Random(); int answer; // the correct answer // ask the user to answer multiplication problems public void quiz() { Scanner input = new Scanner( System.in ); int guess; // the user's guess /* write code to call method createQuestion to display the question */ System.out.println( "Enter your answer (-1 to exit):" ); guess = input.nextInt(); while ( guess != -1 ) { /* write code to call the method checkResponse to check the user’s answer */ System.out.println( "Enter your answer (-1 to exit):" ); guess = input.nextInt(); } // end while } // end method /* write code for the createQuestion, checkResponse method (next slide)*/ } // end class Multiply XuanTung Hoang 4 Exercise 1: Template (2) //createQuestion method: prints a new question and stores the corresponding answer /* write method header for the createQuestion method */ { // get two random numbers between 0 and 9 /* Write code to get two random numbers and store them in variables digit1 and digit2. */ /* Write code to multiply the two variables and store the result in variable answer */ System.out.printf( "How much is %d times %d?\n", digit1, digit2 ); } // end method createQuestion //checkResponse method: checks if the user answered correctly /* Write method header for checkResponse */ { /* Write code to test whether the answer is incorrect */ /* Write code to tell the user to try again, if the answer is incorrect */ else { System.out.println( "Very Good!" ); /* Write code to call method createQuestion to display another question */ } // end else } // end method checkResponse Q: How to generate random integers from 0 to n-1 ?: A: Google with key words: “random + java tutorial” XuanTung Hoang 5 Exercise 1: Sample output How much is 0 times 8? Enter your answer (-1 to 0 Very Good! How much is 3 times 9? Enter your answer (-1 to 21 No. Please try again. Enter your answer (-1 to 27 Very Good! How much is 8 times 1? Enter your answer (-1 to -1 XuanTung Hoang exit): exit): exit): exit): 6 Exercise 2: Duplicate elimination Write an application that inputs five numbers, each between 10 and 100. Display an input number only if it is not a duplicate of a number already read. Display the complete set of unique values input after the user inputs each new value. Use the smallest possible array to solve this problem. XuanTung Hoang 7 Exercise 2: Template (1) // Unique.java // Reads in 5 unique numbers. import java.util.Scanner; public class Unique { // gets 5 unique numbers from the user public void getNumbers() { Scanner input = new Scanner( System.in ); /* Create an array of five elements*/ int count = 0; // number of uniques read int entered = 0; // number of entered numbers while( entered < numbers.length ) { System.out.print( "Enter number: " ); /* Write code here to retrieve the input from the user */ // validate the input (next slide) /* Write an if statement that validates the input */ // print the list of unique values /* Write code to output the contents of the array */ } // end while } // end method getNumbers } // end class Unique XuanTung Hoang 8 Exercise 2: Template (2) // validate the input if ( //*add validate condition) { // flags whether this number already exists boolean containsNumber = false; // increment number of entered numbers entered++; /* Compare the user input to the unique numbers in the array using a for statement. If the number is unique, store new number */ /* add the user input to the array only if the number is not already in the array */ if ( !containsNumber ) { /* Write code to add the number to the array and increment unique items input */ } // end if else System.out.printf( "%d has already been entered\n“, number ); } // end if else System.out.println( "number must be between 10 and 100" ); XuanTung Hoang 9 Exercise 2: Sample output Enter number: 11 11 Enter number: 85 11 85 Enter number: 26 11 85 26 Enter number: 11 11 has already been entered 11 85 26 Enter number: 41 11 85 26 41 Enter number: 99 11 85 26 41 99 XuanTung Hoang 10 Homework #3 XuanTung Hoang 11 Submission and Report Due: 1th April, mid-night Compress your source/class files into a ZIP file Name the zip file according with the following template: StudentID_labNumber.zip Example: 20091004_lab1.zip Submit the file to TA Email address: submit.hw.code@gmail.com Subject: “StudentID_ICE0124_lab1” Example: 20061004_ICE0124_lab1 XuanTung Hoang 12