0% found this document useful (0 votes)
14 views

COS315-Java Homework2 KHH200

1. The document describes a Java programming homework assignment that involves building a number guessing game. 2. It includes screenshots and descriptions of the student testing different aspects of the game, such as entering guesses, tracking the number of tries, and generating random numbers. 3. Source code is provided for the GuessModel class that controls the game logic and GuessSessionBean class that manages the game state, such as the random number, number of guesses, and results of each guess.

Uploaded by

Craxbus K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

COS315-Java Homework2 KHH200

1. The document describes a Java programming homework assignment that involves building a number guessing game. 2. It includes screenshots and descriptions of the student testing different aspects of the game, such as entering guesses, tracking the number of tries, and generating random numbers. 3. Source code is provided for the GuessModel class that controls the game logic and GuessSessionBean class that manages the game state, such as the random number, number of guesses, and results of each guess.

Uploaded by

Craxbus K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

COS315 - Java Programming Homework 2

Kiril Hadzhyiski 200146294


10/7/2022

Task1

1. Trying to enter number 2

2. The result after I entered 2

3. On this screenshot im entering number 5


4. And this screenshot is the result after I entered 5

Task 2 changed the code to keep track of numberOfTries


1.

I sumbitted 6 on my first try


2.

The result after sumbiting 6


3.

I entered 4 on my second try


4.

The result after entering 4


5.

Entering 7 on my thrid try


6.
And the result after entering 7
Task 3 Generates random number

First game
1.

Entering number 4
2.

And the result after entering 4


3.

Entering number 5
And after entering number 5
4.

Entering number 7
5.

And the result after sumbiting number 7


6.

Entering number 6
7.

And the result after entering number 7


Game 2:
1.

Entering number 6
2.

And the result after entering number 6


3.

Sumbitting number 5
4.

The result after entering number 5


5.

Entering number 4
6.

And the result after entering number 4.


Source Code GuessModel.java :
package edu.aubg.guess;
import javax.annotation.Resource;
import edu.aubg.guess.beans.GuessSessionBean;
public class GuessModel {
@Resource(name = "guessSessionBean")
private GuessSessionBean guessSessionBean;
public String play(int theGuess) {
int theNumber;
theNumber = guessSessionBean.getNumber();
int numberOfTries;
String theResult = "";
if (theGuess == theNumber) {
theResult = "win";
guessSessionBean.setTries(0);
return theResult;
}
numberOfTries = guessSessionBean.getTries();
numberOfTries = numberOfTries + 1;
guessSessionBean.setTries(numberOfTries);
theNumber = guessSessionBean.getNumber();
if (numberOfTries >= 3) {
theResult = "lose";
guessSessionBean.setTries(0);
}
else if (theGuess < theNumber) {
theResult = "low";
}
else if (theGuess > theNumber) {
theResult = "high";
}
return theResult;
}
}

Source Code GuessSessionBean.java :


package edu.aubg.guess.beans;

public class GuessSessionBean {

private int numberOfTries=0;


private int theNumber = 0;

private int min = 0;


private int max = 9;

public int getTries() {

return this.numberOfTries;
}

public void setTries(int numberOfTries) {

this.numberOfTries = numberOfTries;
}
public GuessSessionBean() {

this.theNumber = (int)Math.floor(Math.random()*(max-min+1)+min);
}
public int getNumber() {
return this.theNumber;
}
}

You might also like