Assignment5 - Loops index.html (1.257 Kb) Assignment #5ObjectiveComputers are playing an increasing role in education. Write an Application that will help an elementary school student learn multiplication. Project Specifications1. Download the Multiplier class and its Help file (index.html) provided in Blackboard – Assignments – Assignment5 and save them in a new folder. 2. Review the help to check for data, constructors, and all the methods available for this class. 3. Provide an application called GameTester in the same folder where you placed the Multiplier class. 4. Use comments to show your name, the date and the assignment number. 5. Use the class Multiplier to generate a multiplication question and display it to the user. 6. Prompt the user for the answer and check it. Display a message accordingly. 7. If the answer is correct congratulate the user and proceed to ask the user whether they want to play again. 8. If the answer is incorrect display an encouraging message and let the user try again. 9. Allow the user to try to get the correct answer 3 times. After that display the correct answer and ask whether they want to play again. 10. Do NOT modify the Multiplier class. 11. Please make sure your program is indented correctly. Due Date:Submit your *.java file through Blackboard no later than Nov 9 by 11:59 pm. | |||||||||||||||||||||||||||||||||||||||||||||||||||
A5 >> View/Complete Assignment: A5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
Solution import java.util.*; public class GameTester { public static void main(String args[]) { Multiplier game = new Multiplier(); String again = "y"; int answer = 0; int counter; Scanner scan = new Scanner(System.in); while ( again.equalsIgnoreCase( "y" ) ) { System.out.println( game.nextQuestion() ); answer = scan.nextInt(); if ( game.isAnswer( answer ) ) System.out.println ("Correct!!!"); else { counter = 1; while ( !game.isAnswer( answer ) && counter <= 2 ) { System.out.println ("Sorry, the answer is incorrect. Try again."); System.out.println( game.getQuestion() ); answer = scan.nextInt(); counter++; } if (!game.isAnswer( answer )) System.out.println ("Sorry, the answer is incorrect. The answer is " + game.getAnswer() ); else System.out.println ("Correct!!!"); } System.out.println("Would you like to play again?"); again = scan.next(); } } } import java.util.*; /** A Multiplier object will generate a multiplication question. */ public class Multiplier { private int operand_1; private int operand_2; private int answer; private String question; private Random generator; /** Constructs a Multiplier question with 2 operands and an answer. */ public Multiplier() { generator = new Random(); operand_1 = getNumber(); operand_2 = getNumber(); answer = operand_1 * operand_2; question = "How much is " + operand_1 + " times " + operand_2 + " ?"; } /** Returns the answer to the generated question. @return the answer */ public int getAnswer() { return answer; } /** Returns the operand_1. @return operand_1 */ public int getOperand_1() { return operand_1; } /** Returns the operand_2. @return operand_2 */ public int getOperand_2() { return operand_2; } /** Compare the guess to the question answer. @param guess the users guess to the problem @return true if guess is correct false otherwise */ public boolean isAnswer( int guess ) { if ( guess == answer ) return true; else return false; } /** Constructs and returns a new multiplication question. @return question */ public String nextQuestion() { operand_1 = getNumber(); operand_2 = getNumber(); answer = operand_1 * operand_2;; question = "How much is " + operand_1 + " times " + operand_2 + " ?"; return question; } /** Returns the current multiplication question. @return question */ public String getQuestion() { return question; } /** Returns random number . @return operand */ public int getNumber() { return generator.nextInt(11); } /** Returns the multiplication question. @return question */ public String toString() { return question; } }
Class Multiplierjava.lang.Object Multiplier
Multiplierpublic Multiplier()
getAnswerpublic int getAnswer()
getOperand_1public int getOperand_1()
getOperand_2public int getOperand_2()
isAnswerpublic boolean isAnswer(int guess)
nextQuestionpublic String nextQuestion()
getQuestionpublic String getQuestion()
getNumberpublic int getNumber()
|
I am student of CIS at - Massachusetts Bay College - Massbay, Wellesley, MA. I would like to share some of my classes, so who is interested in learn computation, feel free to copy, and exchange information. Também falo português e hablo un poquito del español. Nil, 12/11/2010
Total de visualizações de página
sábado, 11 de dezembro de 2010
Write an Application that will help an elementary school student learn multiplication.
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário