HW5: Calculation Game

Warning

This is not an easy homework. Please start it earlier. Also, if I find you copy other student's program or let other students copy your program. You will automatically get zero point and I will report your name to student office.

Description

You are going to write a program named CalGame.java. The program display several numbers and a target number. You have to use basic arithematic operations (+, -, *, /) to get the target number. Please try the demonstration here:
The game has 3 properties: The program show have the following constructor: CalGame(6, 5, 24)


CalGame(13,4,24)


GalGame(12, 6, 60)


Detailed Description

We will use max = 6, num = 5, target = 24 as example.

At the beginning, 5 random numbers from 1 to max are generated. The lower set of buttons are disabled (setEnabled(false)).
The target number appears at the end of second row.


You can then click on any buttons to highlight the button you choose.
You can choose 2 numbers and one operations (in any order)
The last number you click is the second operand. The other number is the first operand. The new result wil be
operand1 op operand2 (op is +, -, *, /).
I choose a "+" sign first. The button is highlighted.


Then I choose the "4" button.


Then I choose the "2" button. Now 2 number buttons and 1 operation button are clicked. The program disables the 2 number buttons (each number can only be used once). 4 + 2 = 6. So a 6 appears and the button is enabled (setEnabled(true)).


Next, I want to get 6-5. I can click on the 6 button, then 5 button, then - button. Or 6 button, - button, then 5 button. Any order is fine.




Anytime you can unclick the highlight button by clicking on it again.
I click on 4, 5 button

I then click on 4 button. The 4 button is unclicked.


I click on + ,1


I then click on 6, /, 4 , however, 6/4 is not an integer. there will be a beep sound for warning. A beep sound can be made by Toolkit.getDefaultToolkit().beep();


You can change the operation you choose by clicking on new operation. I click on * button. The final number is 24 (the target number).
A next button appears. Click on it will take you to a new game. Notice the trial number and win number are both increased by 1.
Give Up and restart button are disabled.


Click on next button will give you a new game.
Give Up and restart button are enabled.


If you click on 2 numbers and then click on a third number. The second number will be unclicked.
I click on 25 and 5 button.


Then click on 1 button.


Then click on - button


Anytime you can change the operation by clicking a new operation button. Example, I click on 24, *


Then I click on - button.


You can click on restart button to undo all the calcution and restart the game with the same numbers.


You can click on Give Up button to give up the game at anytime and start a new one. The trial number will be increased by 1.


Hint

Here are the codes to generate a random number from 1 to max.
    // the method return a random number from 1 to max
    public static int random(int max) {
        return (int)(Math.random()*max) + 1;
    }

Test

You can test the program by commenting the random(int max) codes and add the following codes.
    public static int index = 0;
    public static int[] randomNum =  {5,4,2,5,4, 1,5,5,5,5, 
                                    2,4,4,4,6, 5,1,4,4,1};
  
    
    public static int random(int max) {
        int length = randomNum.length;
        index = index%length;
        int num = randomNum[index]%max;
        index++;
        return num;
    }
and go through the above steps. Remember to remove it and uncomment the random(int max) after you are done with the testing.

What to submit

Call your source code file CalGame.java. Do not place this file inside another folder within your submit folder.

Remark

Solution

CalGame.java