Assignment 2
Be sure to type up your answers so you can submit them electronically on the
BlackBoard Connect system. You may complete this assignment individually or
with another student in the class. Make sure all your files have your full name
and student number on it.
If you worked in a pair, be sure to have both your full names and student
numbers on everything. Both of you will need to submit (the same copy) on
Blackboard Connect, even though one copy will be graded and you will both get
the same mark.
Type up your answers using a text editor as instructed in the labs, or use
Eclipse if you are already comfortable with it.
The file format of the Java program you are to submit must be .java.
This assignment is marked out of 29 points. If you get 29/29, you will get 5%
towards the course grade. There is a max possible of 33/29 if you complete the
bonus successfully.
Due Date
October 24, 2013, 11:00PM.
24 Hour Silence Policy
Due to the large class size and the potential volume of questions, we are
implementing a 24-hour silence policy. This means that any questions posted on
the discussion forum in the 24 hours immediately preceding the assignment
deadline may not get answered in time. This does not mean that we will
stop answering questions the moment the day before the deadline arrives, it just means
that we cannot promise all the questions will be answered in time, due to our
prior engagements, such as other classes and meetings we have.
For this reason, please be sure to attempt the assignment early, and try to
complete it as if it were due two days prior to the real deadline.
In any case, we will try to answer all the questions that show up on the
discussion forum. Note that questions sent to emails will not be
answered, as we want all course related material to be viewed for everyone's
benefit.
What You Are Given
You are given a partial program that lets kids practice arithmetic.
The following files are provided for you:
Note: You cannot change what is provided in these files (unless
otherwise indicated). Unacceptable changes will result in a lower mark even if
your program works.
Question 1 [6 points]
Complete the constructor method in the Activity class. Currently, the
files you are given expects this method to not take any input parameter. But as
you have seen in the past, this is part of the programmer's design choice. Here
are two options for completing the constructor method:
- Do this: The constructor does not take any inputs, so you will have
to write a mutator method for setting the instructions. A method that changes
the number and answer attributes is already given to you.
- Or alternatively, do this: The constructor takes inputs so when you
initialize your attributes, you can give them meaningful values (by assigning
them to the input accordingly) and not default values. In this case, you don't
need to write an additional mutator method for setting the instructions.
Whichever option you choose, you will need to make sure the code in the
TestActivity class works with your solution.
When you are done, run Activity with
TestActivity to make sure it works before you move on.
Nothing is displayed when you run it, but your objects are still created. The
grading criteria for this question is:
- [4 points] Initialize all 4 attribute correctly
- [2 points] Ensuring the remaining program works correctly in order
to create the two Activity objects in the test class
Question 2 [6 points]
In the Activity class, add a method called play() that does
the following:
- [1 point] Display the instructions to the user
- [1 point] Display the value stored in your number
attribute to the user
- [1 point] Get the number that user types in and store it in a
variable
- [1 point] Check to see if the number user typed matches the actual
answer that is already stored in your answer attribute
- [1 point] If the two match, call win()
- [1 point] If the two don't match, call lose()
Once you think the method works, do the following in the TestActivity
class:
- Uncomment the line: nextNum.play();
- Uncomment the line: tellTime.play();
When you are done, run Activity with
TestActivity to make sure it works before you move on.
You know it works if you get to play two games and you get output as follows:
What is the next number after this one?
451
452
Horray! That's correct! Good job!
How many minutes are there if the following number is in seconds?
834
13
Horray! That's correct! Good job!
Note that the numbers 451 and 834 are randomly generated, so you may not have
the same numbers in your output. Note also that the numbers 452 and 13 are
typed in by the user.
Question 3 [11 points]
In this question, we turn to the Game class. To give you more practice
on designing your own class, translating it to Java code, and getting it to
work the way you want to, the instructions in this question will be more
open-ended. While this gives you more flexibility and control, it will also be
harder because it requires you to have a good understanding of the
Activity and
TestActivity classes, as well as other programming concepts you've
been learning in class.
First of all, this Game class will act like the TestActivity
class in the sense that it will create Activity objects and let user
enter answers. However, it will additionally let the user choose which activity
the user wants to play, and at the end of playing one activity, let the user
choose whether s/he wants to play another one. That's all the Game
class is responsible for.
To complete this class, we recommend the following steps:
- [1 point] Complete the constructor so that all your attributes
are initialized. You can use the initialization done in your
TestActivity class to guide you (those lines of code should be the
same).
- [1 point] As the last line in your constructor, call
beginGame(), which is a method you will define next.
- [5 points] Define a method called beginGame() that asks
the user which activity s/he wants to play, reads in the user's answer,
displays that answer back to the user (as a confirmation to the user), and
calls another method named playActivity(), which is a method
you will define next.
Ensure your method's visibility adheres to the encapsulation guidelines!
- [4 points] Define a method called playActivity() that
checks what the user has typed as the selection, and based on that choice,
call the play() either belonging to nextNum or
tellTime so that the user plays the activity that s/he indicated.
Note that you will need to re-set the number and answer
attributes in your object based on a randomly generated number before you
call play().
Ensure your method's visibility adheres to the encapsulation guidelines!
Test your Game class with the TestGameDesign class provided.
Question 4 [6 points]
Once you get the above playActivity() part working and tested,
you can add more statements to the method that lets the user play another game.
In particular, after playing an activity once, your method will additionally do
the following:
- [1 point] Ask the user if s/he wants to play again, expecting
the user to type either 'y' for Yes or 'n' for No
- [1 point] Read the input in as a letter
- [1 point] Display what the user entered back on the screen
(just as a confirmation to the user)
- [1 point] Check that if the user typed 'y' or something else
- [1 point] If the user typed 'y', then call
beginGame()
- [1 point] If the user typed something else, then display "See
you next time!"
Test your Game class with the TestGameDesign class provided.
When you get your program working, you will see output similar to the
following:
Which activity would you like to play?
Activity 1: One, two, thre, ...
Activity 2: How many minutes is it?
1
You entered: 1
What is the next number after this one?
213
214
Horray! That's correct! Good job!
Do you want to play again? (Type: y/n)
y
You entered: y
Which activity would you like to play?
Activity 1: One, two, thre, ...
Activity 2: How many minutes is it?
2
You entered: 2
How many minutes are there if the following number is in seconds?
3397
512
That doesn't sound right. It should be 56 Better luck next time!
Do you want to play again? (Type: y/n)
y
You entered: y
Which activity would you like to play?
Activity 1: One, two, thre, ...
Activity 2: How many minutes is it?
2
You entered: 2
How many minutes are there if the following number is in seconds?
1721
28
Horray! That's correct! Good job!
Do you want to play again? (Type: y/n)
n
You entered: n
See you next time!
Question 5 [Bonus: 4 points]
Create a new number activity that has the same activity structure and add it to
the game. Show that it works In particular:
- [1 point]
Create a new Activity object
- [1 point]
Add it to your menu in beginGame() so that the game can display
it and the user can choose it
- [2 points]
Modify your playActivity() so that it considers a third choice
the user might make and call that object's methods accordingly
Make sure your Game class still works with the TestGameDesign class
provided.
What to Submit
For this assignment, submit the following:
- Activity.java
- TestActivity.java
- Game.java
- Output-ActivityOnly.txt
For output that shows your program works based on questions up to and including Question 2.
Make sure your output clearly shows how your program works with different activities.
- Output-Game.txt
For output that shows your program works based on questions up to and including Question 4.
Make sure your output clearly shows how your program asks the user to play
different activities again, and that different numbers are generated each game.
- Output-Bonus.txt (optional)
For output that shows your program works based on questions up to and including Question 5.
This is required only if you completed the bonus question.
- Include your full name and student ID.
If you worked in a pair, be sure to have both your full names and student
numbers on everything. Both of you will need to submit (the same copy) on
Blackboard Connect, even though one copy will be graded and you will both get
the same mark.
- List the full names of the people who helped you in this assignment
(include students and TAs if applicable), and mention what they helped you with
-- failure to do so honestly will result in a lower mark and may be reported
for academic discipline