1 Attachment(s)
New to Java Need help with Homework
I think I am really close I just cant get the code to change the status of the game to game over from the In progress status. Can somebody point me into the right direction please. I don't somebody to do it for me I just have tried everything that I can think of, I know it's something simple but I'm just not seeing it. Thanks in advance for the help. Attachment 805
I attempted to attach the program itself if that is a help.
public class BasketballGame {
private String firstTeamName;
private String secondTeamName;
private int firstTeamScore;
private int secondTeamScore;
private String gameStatus;
String winnersName = "";
public BasketballGame(String firstTeamName, String secondTeamName) {
this.firstTeamName = firstTeamName;
this.secondTeamName = secondTeamName;
this.firstTeamScore = 0;
this.secondTeamScore = 0;
this.gameStatus = "In progress";
}
public String getFirstTeamName() {
return firstTeamName;
}
public int getFirstTeamScore() {
return firstTeamScore;
}
public String getGameStatus() {
return gameStatus;
}
public String getSecondTeamName() {
return secondTeamName;
}
public int getSecondTeamScore() {
return secondTeamScore;
}
public void setFirstTeamScoreOnePoint() {
this.firstTeamScore = +1;
}
public void setFirstTeamScoreTwoPoints() {
this.firstTeamScore += 2;
}
public void setFirstTeamScoreThreePoints() {
this.firstTeamScore += 3;
}
public void setSecondTeamScoreOnePoint() {
this.secondTeamScore = +1;
}
public void setSecondTeamScoreTwoPoints() {
this.secondTeamScore += 2;
}
public void setSecondTeamScoreThreePoints() {
this.secondTeamScore += 3;
}
public String determineWinner() {
if (this.firstTeamScore > this.secondTeamScore) {
winnersName = this.firstTeamName;
} else {
winnersName = this.secondTeamName ;
}
return winnersName;
}
public BasketballGame()
{
while (winnersName.equals(firstTeamName) || winnersName.equals(secondTeamName))
{
gameStatus = "Game Over";
}
}
@Override
public String toString() {
return "BasketballGame{" + "First Team :" + firstTeamName
+ ", Second Team :" + secondTeamName
+ firstTeamName + " = " + firstTeamScore + " "
+ secondTeamName + " = " + secondTeamScore + " "
+ ", Game Status = " + gameStatus + '}';
}
}
public class BasketballGameDemo {
public static void main(String[] args)
{
BasketballGame game1 = new BasketballGame("Cats","Dogs");
game1.setFirstTeamScoreOnePoint();
game1.setFirstTeamScoreTwoPoints();
game1.setFirstTeamScoreThreePoints();
game1.setFirstTeamScoreOnePoint();
game1.setFirstTeamScoreTwoPoints();
game1.setFirstTeamScoreThreePoints();
// System.out.println(game1.toString());
game1.setSecondTeamScoreTwoPoints();
game1.setSecondTeamScoreThreePoints();
game1.setSecondTeamScoreThreePoints();
System.out.println(game1);
System.out.println("The winner is " + game1.determineWinner());
Re: New to Java Need help with Homework
I obviously need to learn how to post the code correctly too. Sorry about that.
Re: New to Java Need help with Homework
[highlight=java] Put code here [/highlight]
Haven't looked at the code much yet, but am wondering, why is it
while (winnersName.equals(firstTeamName) || winnersName.equals(secondTeamName))
{
gameStatus = "Game Over";
}
instead of
if(winnersName.equals(firstTeamName) || winnersName.equals(secondTeamName)
{
}
Actually, it might be best just to write, not sure if you intended it to be if or while statement, but would this work....
if (winnersName != null)
{
}
Re: New to Java Need help with Homework
Code java:
public class BasketballGame {
private String firstTeamName;
private String secondTeamName;
private int firstTeamScore;
private int secondTeamScore;
private String gameStatus;
private String winnersName = "";
private String gameStatusChange = "";
public BasketballGame(String firstTeamName, String secondTeamName) {
this.firstTeamName = firstTeamName;
this.secondTeamName = secondTeamName;
this.firstTeamScore = 0;
this.secondTeamScore = 0;
this.gameStatus = "In progress";
}
public String getFirstTeamName() {
return firstTeamName;
}
public int getFirstTeamScore() {
return firstTeamScore;
}
public String getGameStatus() {
return gameStatus;
}
public String getSecondTeamName() {
return secondTeamName;
}
public int getSecondTeamScore() {
return secondTeamScore;
}
public void setFirstTeamScoreOnePoint() {
this.firstTeamScore = +1;
}
public void setFirstTeamScoreTwoPoints() {
this.firstTeamScore += 2;
}
public void setFirstTeamScoreThreePoints() {
this.firstTeamScore += 3;
}
public void setSecondTeamScoreOnePoint() {
this.secondTeamScore = +1;
}
public void setSecondTeamScoreTwoPoints() {
this.secondTeamScore += 2;
}
public void setSecondTeamScoreThreePoints() {
this.secondTeamScore += 3;
}
public String determineWinner() {
if (this.firstTeamScore > this.secondTeamScore) {
winnersName = this.firstTeamName;
} else {
winnersName = this.secondTeamName ;
}
return winnersName;
}
public BasketballGame()
{
while (winnersName.equals(firstTeamName) || winnersName.equals(secondTeamName))
{
gameStatus = "Game Over";
}
}
@Override
public String toString() {
return "BasketballGame{" + "First Team :" + firstTeamName
+ ", Second Team :" + secondTeamName
+ firstTeamName + " = " + firstTeamScore + " "
+ secondTeamName + " = " + secondTeamScore + " "
+ ", Game Status = " + gameStatus + '}';
}
}
Code java:
public class BasketballGameDemo {
public static void main(String[] args)
{
BasketballGame game1 = new BasketballGame("Cats","Dogs");
game1.setFirstTeamScoreOnePoint();
game1.setFirstTeamScoreTwoPoints();
game1.setFirstTeamScoreThreePoints();
game1.setFirstTeamScoreOnePoint();
game1.setFirstTeamScoreTwoPoints();
game1.setFirstTeamScoreThreePoints();
// System.out.println(game1.toString());
game1.setSecondTeamScoreTwoPoints();
game1.setSecondTeamScoreThreePoints();
game1.setSecondTeamScoreThreePoints();
System.out.println(game1);
System.out.println("The winner is " + game1.determineWinner());
}
}
Re: New to Java Need help with Homework
Wow that made a big difference, thanks!!
Re: New to Java Need help with Homework
I have some unused things in there that i tried and didn't remove everything.
Re: New to Java Need help with Homework
It's because you're using a different constructor.
You're creating an object of BasketballGame that uses the non-default constructor. Only the default (the no param one) changes the game status when the game is over.
You could do this, and even call it inside the determineWinner() method.
public void setGameStatus(String gameStatus)
{
this.gameStatus = gameStatus;
}
Maybe not.....
Ahhhhh....I see the problem. Add that method shown above and put, before the return statement in determineWinner()
setGameStatus("Game over");
also, at the end of your main method, add another
System.out.println(game1);
and it will show that the status has changed. You were printing it out before a winner was determined, and thus before the game was over.
Code java:
public class BasketballGame {
private String firstTeamName;
private String secondTeamName;
private int firstTeamScore;
private int secondTeamScore;
private String gameStatus;
private String winnersName = "";
private String gameStatusChange = "";
public BasketballGame(String firstTeamName, String secondTeamName) {
this.firstTeamName = firstTeamName;
this.secondTeamName = secondTeamName;
this.firstTeamScore = 0;
this.secondTeamScore = 0;
this.gameStatus = "In progress";
}
public String getFirstTeamName() {
return firstTeamName;
}
public int getFirstTeamScore() {
return firstTeamScore;
}
public String getGameStatus() {
return gameStatus;
}
public String getSecondTeamName() {
return secondTeamName;
}
public int getSecondTeamScore() {
return secondTeamScore;
}
public void setFirstTeamScoreOnePoint() {
this.firstTeamScore = +1;
}
public void setFirstTeamScoreTwoPoints() {
this.firstTeamScore += 2;
}
public void setFirstTeamScoreThreePoints() {
this.firstTeamScore += 3;
}
public void setSecondTeamScoreOnePoint() {
this.secondTeamScore = +1;
}
public void setSecondTeamScoreTwoPoints() {
this.secondTeamScore += 2;
}
public void setSecondTeamScoreThreePoints() {
this.secondTeamScore += 3;
}
public String determineWinner() {
if (this.firstTeamScore > this.secondTeamScore) {
winnersName = this.firstTeamName;
} else {
winnersName = this.secondTeamName ;
}
setGameStatus("Game over");
return winnersName;
}
public void setGameStatus(String gameStatus)
{
this.gameStatus = gameStatus;
}
public BasketballGame()
{
while (winnersName.equals(firstTeamName) || winnersName.equals(secondTeamName))
{
gameStatus = "Game Over";
}
}
@Override
public String toString() {
return "BasketballGame{" + "First Team :" + firstTeamName
+ ", Second Team :" + secondTeamName
+ firstTeamName + " = " + firstTeamScore + " "
+ secondTeamName + " = " + secondTeamScore + " "
+ ", Game Status = " + gameStatus + '}';
}
}
Code java:
public class BasketballGameDemo {
public static void main(String[] args)
{
BasketballGame game1 = new BasketballGame("Cats","Dogs");
game1.setFirstTeamScoreOnePoint();
game1.setFirstTeamScoreTwoPoints();
game1.setFirstTeamScoreThreePoints();
game1.setFirstTeamScoreOnePoint();
game1.setFirstTeamScoreTwoPoints();
game1.setFirstTeamScoreThreePoints();
// System.out.println(game1.toString());
game1.setSecondTeamScoreTwoPoints();
game1.setSecondTeamScoreThreePoints();
game1.setSecondTeamScoreThreePoints();
System.out.println(game1);
System.out.println("The winner is " + game1.determineWinner());
System.out.println(game1);
}
}
BasketballGame{First Team :Cats, Second Team :DogsCats = 6 Dogs = 8 , Game Status = In progress}
The winner is Dogs
BasketballGame{First Team :Cats, Second Team :DogsCats = 6 Dogs = 8 , Game Status = Game over}
Re: New to Java Need help with Homework
Wow, thank you so much! I would have had this working hours ago if i had seen that I printed before i determined the winner. I thought i was going crazy!