• To analyze a problem and implement its solution
• To practice writing a programmer-defined class
• To gain experience writing code to test a programmer-defined class

Background Information:
•In a n addition problem, 3 + 7, the first operand is 3, the second operand is 7, and the operator is ‘+’.
• In a subtraction problem 8 – 4, the first operand is 8, the second operand is 4, and the operator is ‘-‘.

The Problem:
You have been hired by a school district to write a program for their first grade students who need practice in basic mathematics. The children have learned the following skills:
• addition - two operands, both in the range 0 - 12
• subtraction - two operands, the first in the range 6-12 and the second less than or equal to the first – for example (12-10, 6-2, etc.)
• Rule: if the first operand is 7, for example, the second operand must range from 0 to 7.
• Your task is to write a programmer defined class that contains a randomly generated addition or subtraction problem.

In order for this program to be useful, it must be able to generate all possible equations for both operators. For this project, you will write and test a Question class that could be used in the math game (later in Project 4).

Start Eclipse & Create a New Project
Create a new project, proj3, which contains 2 classes, Question and Project3App. Be sure to name the package proj3 when creating the classes.

Writing Java Statements – The Question Class
The purpose of the Question class:
• to create a simple mathematical equation that can be used in a math game (which will be written in a future project).   
• The equation can be addition or subtraction.   
• If the program generates a subtraction question, it must ensure that the first operand is greater than or equal to the second so the answer is not a negative number (the children haven't learned negative numbers yet).

Specifications for the Question class:
• 3 instance variables o an integer named operand1 for the first operand in the equation o an integer named operand2 for the second operand in the equation o a character named operator to store + or – that specifies an addition or subtraction problem
• 3 accessor methods for the instance variables o getOperand1 o getOperand2 o getOperator
• A default constructor: The default constructor will generate random values for the three instance variables: operand1, operand2, operator (it must meet the specifications in The Problem section above).

Answer the questions below to help you think through the logic of what needs to be done.
1. If the program needs to randomly choose one of two operations (+ or -), how would you do that?
2. How do you generate a random number in the range 0-12 (for addition problems)?
3. How do you generate a random number in the range 6-12 (for subtraction)?
4. Given a number x, how do you generate a random number in the range 0-x (for subtraction)?

Recall, the purpose of the constructor is to give each of the instance variables initial values. Write the constructor so that it generates appropriate values for operand1, operand2, and operator. The values must be random.

• A toString method that returns a String containing the question (but not the answer). The format should be operand1, a space, operator, a space, operand2, a space and an equal sign.
For example,
4 + 6 =

• A determineAnswer method that calculates the answer to the question and returns the answer (an integer). The answer should not be stored as an instance variable.

• Add a Javadoc comment below the package statement that contains the title, description, and author (use the @author tag). You can copy and paste from a previous project or lab and change the description.

• Add Javadoc to the constructor and each of the methods. Use appropriate tags (@param, @return) where applicable. See the Card class from lab 4 for guidance.

Specifications for the Project3App class:

Once you have the instance variables, accessor methods, constructor, toString, and determineAnswer methods written, test your code in the application class to make sure it works properly.

• Add a Javadoc comment below the package statement that contains the title, description, and author (use the @author tag). You can copy and paste from a previous project or lab and change the description.

• Create a main method, and add the appropriate Javadoc above the method header. (You can copy and paste from a previous project or lab).

• Create and display Question objects: Use a for loop (to be discussed in class) to generate and display 15 questions. Precede each question with a label that indicates the question number and a colon, for example Question 1: See the expected output below.

• Add a call to the determineAnswer method into your loop to display the answer after the = sign (see expected output below). Visually inspect your output and verify that the determineAnswer method is calculating correct answers.

• Your output will have different numbers due to the random nature of the operands and operators.

Expected output:
Question 1: 12 + 0 = 12
Question 2: 6 + 12 = 18
Question 3: 3 + 8 = 11
Question 4: 11 - 0 = 11
Question 5: 8 + 7 = 15
Question 6: 8 - 3 = 5
Question 7: 8 - 2 = 6
Question 8: 12 - 9 = 3
Question 9: 0 + 10 = 10
Question 10: 4 + 5 = 9
Question 11: 2 + 1 = 3
Question 12: 1 + 12 = 13
Question 13: 12 + 0 = 12
Question 14: 10 - 1 = 9

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Question {
    private int operand1;
    private int operand2;
    private char operator;

    * default constructor
    * generate random values for the three instance variables
    public Question() {
       Random random = new Random();
       if (random.nextBoolean()) {            
            operand1 = random.nextInt(13);
            operand2 = random.nextInt(13);
            operator = '+';
       } else {   
            operand1 = random.nextInt(7) + 6;
            operand2 = random.nextInt(operand1 + 1);
            operator = '-';
$23.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats