Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: New to Java Need help with Homework

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. BasketballGameDemo.zip

    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());


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java Need help with Homework

    I obviously need to learn how to post the code correctly too. Sorry about that.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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)
    {


    }
    Last edited by javapenguin; October 20th, 2011 at 09:25 PM.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java Need help with Homework

    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 + '}';
        }
    }


     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());
     
     
     
        }
    }

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java Need help with Homework

    Wow that made a big difference, thanks!!

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java Need help with Homework

    I have some unused things in there that i tried and didn't remove everything.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

    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 + '}';
        }
    }

    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}
    Last edited by javapenguin; October 20th, 2011 at 09:39 PM.

  8. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Help On Java Homework (DUE TONIGHT!!!)
    By agmolina90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2011, 08:28 AM
  2. Help With Java Homework: Set Operations
    By kilroyjr in forum Java Theory & Questions
    Replies: 10
    Last Post: April 1st, 2011, 09:41 AM
  3. Homework
    By jdonaldson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:09 AM
  4. Help for java homework
    By Dark_Shadow in forum Object Oriented Programming
    Replies: 6
    Last Post: December 7th, 2009, 03:08 AM
  5. MORE java homework help.
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: October 13th, 2009, 07:15 PM