ADVANCED OBJECT-ORIENTED PROGRAMMING Programming Assignment: DESIGNING JAVA CLAS
ID: 3590216 • Letter: A
Question
ADVANCED OBJECT-ORIENTED PROGRAMMING
Programming Assignment: DESIGNING JAVA CLASSES
Introduction -
This exercise is meant to introduce you to designing your own Java classes (including constructors and methods). You will be implementing:
A couple of simple “guessing game” classes (GuessLogic and GuessList) that generates a random number between 1 and some limit, and keeps track of whether the player has correctly guessed it, what numbers they have previously guessed, and how many guesses they have made.
A couple of very simple main command line applications (EasyGame and HardGame) that run guessing games (with slightly different rules) for, using those support classes.
Here is a simple UML diagram that gives the relationships between the classes:
In addition, you will be producing a simple design document in advance of the final implementation, listing the methods your GuessLogic and GuessList support classes will implement.
The SimpleGame Application -
This game repeatedly prompts the player for a number between 1 and 10 until they correctly guess it.
- The only feedback for incorrect guesses is that it is not correct.
- When they guess the number, they are told how many tries it took.
- They have an unlimited number of tries in this game.
- If they guess a number that has already been guessed, they are warned and the previous guesses are all printed out. These guesses do not count against the number of tries.
For example:
The HardGame Application -
This game repeatedly prompts the player for a number between 1 and 100.
If the guess is incorrect. the player is told whether the guess is too high or too low. They have a limit of 7 guesses. If they make 7 incorrect guesses, the player loses and the game is over. They are not told whether a guess has already been made, and previously made guesses count against the number of tries. If they lose, the correct number and the guesses they made are printed out.
For example:
The GuessLogic Class -
You are to implement a simple support class that controls the logic of the guessing game (stored in the file GuessLogic.java).
Note that I am not specifying the methods and constructors for that class (except for the toString method below). These are the main design decisions for this assignment! However, I do have the following requirements:
Separating the Logic and the User Interface -
All of the logic for the game must be handled within GuessLogic class. In other words, the application classes are not to keep track of the correct answer, the number of guesses made, the number of guesses remaining, or the numbers previously guessed. On the other hand, the application classes are responsible for all of the I/O. In other words, the GuessLogic class is not to print anything (except possibly for the purposes of debugging). Neither is it to return any messages to be printed by the application – methods in the GuessLogic class should only return numeric or Boolean values (as a justification, your company plans to market the applications in other languages).
One thing to think about is how some GuessLogic method can return whether a guess is “too high”, “too low”, or “correct” without returning a string. A better way of doing this is to define a static constant for each in the GuessLogic class for each, and return that instead. For example, you might define LOW = -1 in GuessLogic class, and compare the returned value to GuessLogic.LOW.
Suggestions - I do suggest that you think about providing GuessLogic methods for the following:
(1) Allowing the user to “guess” a given number.
(2) Finding whether the number has been guessed.
(3) Finding the numbers that have already been guessed.
(4) Finding the number of guesses made.
(5) Finding whether the player has any guesses left.
You should also have the GuessLogic constructor generate the correct answer. Generating random numbers can be done with the nextInt method in the Random class (in java.util). See the Java API for details.
The GuessList Class -
You are to implement a separate support class to keep track of the numbers already guessed. This class will be used by GuessLogic , which will construct and store an instance of that class. There are a number of ways to do this, including:
i. Keeping an array of int, & adding each new guess to the end of that array (like the Roster class does for names).
ii. Keeping an array of boolean, each corresponding to a possible number. Initially each is false & is then set to true when that number is guessed.
I suggest that you think about providing GuessList methods for the following:
i. Finding whether the number has been guessed.
ii. Finding the numbers that have already been guessed.
Encapsulation -
As mentioned in class, your representation must be properly encapsulated. That means all member variables must be private.
Documentation -
As with any program, your source code should be well documented! For this assignment, this includes the following:
For the GuessLogic and GuessList classes, make sure that you put a “header” at the top of each method and constructor that briefly explains what it does (particularly in terms of the member variables).
For the applications, make sure that you explain how they work, particularly in terms of manipulating the GuessLogic object.
Note that this documentation is to be in JavaDoc format. That is, I should be able to run your classes through the JavaDoc tool and see your documentation as an API.
The Design Document -
The initial stage of most object-oriented projects is the creation of a design document that lets developers of a class and its users agree on the methods and constructors the classes will provide. This will be the first stage of this assignment. It is worth 10 of the possible 50 overall points of the assignment.
Specifically, you will turn in a document describing each constructor and method your GuessLogic and GuessList support classes will provide. To keep things simple, these descriptions will be similar to the JavaDoc documentation you will turn in later (in fact, if you do the design correctly you can just reuse it in your final documentation).
Include the following for each method:
(1) An abstract description of what it does.
(2) What each input parameter represents.
SimpleGame GuessLogic |- GuessList HardGameExplanation / Answer
*****************************************************************************
JAVA CLASSES ARE GIVEN BELOW:
1.GuessList
2.GuessLogic
3.EasyGame
4.HardGame
*****************************************************************************
/*
THIS CLASS IS FOR STORING THE USER CHOICES WHICH THE USER HAS MADE FOR GUESSING THE NUMBER
*/
import java.util.ArrayList;
public class GuessList {
private ArrayList<Integer> guess;
public GuessList() {
guess=new ArrayList<>();
}
public ArrayList<Integer> getGuess() {
return guess;
}
public void setGuess(int n) {
this.guess.add(n);
}
public boolean checkOldGuess(int n) {
if(guess.contains(n))
return true;
return false;
}
}
*******************************************************************
/*
THIS CLASS CONTAINS ALL THE LOGIC FOR THE EASY GAME AND THE HARD GAME. WHEN THE OBJECT OF THIS CLASS IS CREATED, IT GENERATES A GUESS NUMBER.
THE OBJECT OF THIS CLASS IS CREATED IN THE EASYGAME CLASS AND HARDGAME CLASS AND THEN USING THE OBJECT, IT CALL THE METHOD OF THIS CLASS FOR THE
LOGIC PART.
*/
import java.util.ArrayList;
public class GuessLogic {
private static final int LOW=-1;
private static final int HIGH=1;
private static final int EQUAL=0;
private GuessList guessList;//A MEMBER OF GUESSLIST CLASS WHICH STORES THE USER GUESS IN A LIST
static final int HARDGAMEGUESSES=7;
private int guessNumber;//THE NUMBER TO BE GUESSED BY THE USER
private int turns;//STORES USERS TURNS TO GUESS THE NUMBER
public GuessLogic(int gameType){
guessList = new GuessList();
//0 for easy game and any other number for hard game
if(gameType==0)
guessNumber=generateGuessNumberEasy();
else
guessNumber=generateGuessNumberHard();
}
public int getTurns() {
return turns;
}
public void setTurns(int turns) {
this.turns = turns;
}
public int getGuessNumber() {
return guessNumber;
}
public void setGuessNumber(int guessNumber) {
this.guessNumber = guessNumber;
}
//METHOD GENERATES RANDOM NUMBER FOR EASY GAME
public static int generateGuessNumberEasy() {
int randomNum = 1 + (int)(Math.random() * 10);
return randomNum;
}
//METHOD GENERATES RANDOM NUMBER FOR HARD GAME
public static int generateGuessNumberHard() {
int randomNum = 1 + (int)(Math.random() * 100);
return randomNum;
}
//METHOD FOR HARD GAME TO PRINT IF HIS GUESS IS HIGH LOW OR EQUAL TO THE GUESS NUMBER. USERNUMBER IS A PARAMETER.
public int isGuessed(int userNumber) {
if(guessNumber>userNumber)
return LOW;
else if(guessNumber<userNumber)
return HIGH;
else
return EQUAL;
}
//METHOD RETURNS A LIST OF ALL THE GUESSES A USER HAS MADE
public ArrayList<Integer> getGuess() {
return guessList.getGuess();
}
//METHOD TO STORE THE USER GUESSES IN A LIST WHICH IS IN GUESSLIST CLASS
public void setGuess(int n) {
guessList.setGuess(n);
}
//METHOD TO CHECK IF THE USER HAS USED A NUMBER FOR GUESS BEFORE OR NOT
public boolean checkOldGuess(int n) {
return guessList.checkOldGuess(n);
}
//METHOD TO INCREASE HIS NUMBER OF TURNS
public void increaseTurns() {
turns++;
}
//METHOD TO CHECK IF HE HAS TURNS LEFT FOR A HARD GAME
public boolean hasTurnsLeft() {
if(getTurns() < HARDGAMEGUESSES) {
return true;
}
return false;
}
}
********************************************************************
/*
THIS CLASS IS FOR THE EASY GAME APPLICATION IT TAKE THE INPUT N FOR USER GUESSES AND CREATES AN OBJECT
OF CLASS GUESSLOGIC AND USES ITS METHODS FOR THE LOGIC. ONLY UI IS HANDLED BY THIS CLASS
*/
import java.util.Scanner;
public class EasyGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
GuessLogic logic = new GuessLogic(0);//This is a easy game so ) is passed
/*Uncomment the line below to see the actual guess number*/
//System.out.println(logic.getGuessNumber());
int n;
//A loop till he guesses the number
do{
System.out.println("Enter a number between 1 - 10");
//User input for guess number
n=sc.nextInt();
//Checking if user has guessed the number before or not
if(logic.checkOldGuess(n)==true) {
System.out.println("You have already guessed that!");
System.out.println("Your previous guesses: "+logic.getGuess());//Printing all the guesses he has made if he enters a duplicate guess
}
else {
logic.setGuess(n); //Storing the user guesses in a list
logic.increaseTurns();// increasing his turns
if (logic.getGuessNumber() != n)
System.out.println("Sorry. That is not correct. Try again.");//If his guess is wrong
}
}while(logic.getGuessNumber()!=n);//Loop breaks when the guess is correct
System.out.println("That's correct you guessed it in "+logic.getTurns()+" guesses.");
sc.close();
}
}
*********************************************************************************
/*
THIS CLASS IS FOR THE HARD GAME APPLICATION IT TAKE THE INPUT N FOR USER GUESSES AND CREATES AN OBJECT
OF CLASS GUESSLOGIC AND USES ITS METHODS FOR THE LOGIC. ONLY UI IS HANDLED BY THIS CLASS
*/
import java.util.Scanner;
public class HardGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
GuessLogic logic = new GuessLogic(1);//// This is a hard game so constructor takes other than 0 as input
/* Uncomment the line below to see the actual guess number */
// System.out.println(logic.getGuessNumber());
int n; // UserInput
System.out.println("You have 7 tries to guess a number between 1 - 100");
// A loop till he guesses his turns or exhausts his tries.
do {
System.out.println("Enter a guess:");
// User input for guess number
n = sc.nextInt();
// Checking if user has guessed the number before or not
logic.setGuess(n);// Storing the user guesses in a list
logic.increaseTurns();// increasing his turns
if (logic.getGuessNumber() != n) {
if (logic.isGuessed(n) == -1)
System.out.println("That guess was too low.");
else if (logic.isGuessed(n) == 1) {
System.out.println("That guess was too high.");
}
}
} while (logic.getGuessNumber() != n && logic.hasTurnsLeft());
if (logic.getGuess().get(logic.getGuess().size() - 1) == logic.getGuessNumber()) {//CHECKS FOR THE LAST GUESS IN THE LIST IT HAS!
System.out.println("You guessed it!");
} else {
System.out.println("You are out of guesses. The correct number was: "+logic.getGuessNumber());
System.out.println("Your guesses were: "+logic.getGuess());
}
sc.close();
}
}
*****************************************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.