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.


Assignment5 - Loops
index.html (1.257 Kb)

Assignment #5

Objective

Computers are playing an increasing role in education.  Write an Application that will help an elementary school student learn multiplication.

Project Specifications

1.      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.

Item A5

>> View/Complete Assignment: A5
Item 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 Multiplier

java.lang.Object extended by Multiplier

public class Multiplier
extends Object
A Multiplier object will generate a multiplication question.
Constructor Summary
Multiplier()
          Constructs a Multiplier question with 2 operands and an answer.
 
Method Summary
 int getAnswer()
          Returns the answer to the generated question.
 int getNumber()
          Returns random number .
 int getOperand_1()
          Returns the operand_1.
 int getOperand_2()
          Returns the operand_2.
 String getQuestion()
          Returns the current multiplication question.
 boolean isAnswer(int guess)
          Compare the guess to the question answer.
 String nextQuestion()
          Constructs and returns a new multiplication question.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Constructor Detail

Multiplier

public Multiplier()
Constructs a Multiplier question with 2 operands and an answer.
Method Detail

getAnswer

public int getAnswer()
Returns the answer to the generated question.
Returns:
the answer

getOperand_1

public int getOperand_1()
Returns the operand_1.
Returns:
operand_1

getOperand_2

public int getOperand_2()
Returns the operand_2.
Returns:
operand_2

isAnswer

public boolean isAnswer(int guess)
Compare the guess to the question answer.
Parameters:
guess - the users guess to the problem
Returns:
true if guess is correct false otherwise

nextQuestion

public String nextQuestion()
Constructs and returns a new multiplication question.
Returns:
question

getQuestion

public String getQuestion()
Returns the current multiplication question.
Returns:
question

getNumber

public int getNumber()
Returns random number .
Returns:
operand


 

OK

Nenhum comentário:

Postar um comentário