Results 1 to 8 of 8
  1. #1
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Hmm... Something is not right here!

    I can't seem to figure out how to get this to run correctly.
    After running the program, I get the wrong answer. Please help me find out what is wrong :)
    It should state
    "Programmed by Chuck Norris.
    Player One goals per game = 1.2
    Player Two goals per game = 1.6"


    Write a Java class definition for class HockeyPlayer. Each object of this class has the following data fields:

    fullname : a String value representing the player’s name.
    number : an int value representing the player’s jersey number.
    gamesPlayed : an int value representing the number of games played.
    goalsScored : an int value representing the number of goals scored.

    Your class should also define the following methods:

    A constructor that accepts two parameters: a String representing the player’s name and an integer
    representing the player’s jersey number. This constructor should initialize gamesPlayed and goalsScored to 0.

    Two mutator methods:

    updateGames adds a specified number of games played to the player’s total.
    updateGoals adds a specified number of goals scored to the player’s total.

    Two accessor methods:

    getFullname should return the fullname of the player.
    getGoalsPerGame should return a double value representing the average number of goals scored per game. If
    the player has yet to play a game, getGoalsPerGame should return 0.

    Java Code:
    public class HockeyPlayer
    {
    	String fullname;
    	public int number;
    	public int goals;
    	public int games;
    	
    	
    	public HockeyPlayer(String string, int number) 
    	{
    		String p1 ="Player One";
    		fullname =p1;
    		String p2 ="Player Two";
    	    
    	}
    
    	
    	
    	public void number()
    	{
    		Number p1=10;
    		Number p2=20;
    	}
     
    	public void gamesPlayed()
    	{
    		games = 0;
    	}
    
    	public void goalsScored()
    	{
    		goals=0;
    	}
    
    	public void updateGames(int amount) 
    	{
    		games = games + amount;
    	}
    
    	public void updateGoals(int amount) 
    	{		
    		goals = goals + amount;
    	}
    	
    	public void fullName()
    	{
    		String p1 = "Player Two";
    	}
    
    	public String getFullname() 
    	{
    		return fullname;	
    	}
    
    	public int getGoalsPerGame() 
    	{
    		return  (goals/games);
    		
    		
    	}
    }
    Java Code:
    public class HockeyPlayerTester 
    {
    	
    	public static void main(String[] args) 
    	{
    	System.out.println("Programmed by Chuck Norris.");
    	System.out.println();
    	HockeyPlayer p1 = new HockeyPlayer("Player One", 10);
    	HockeyPlayer p2 = new HockeyPlayer("Player Two", 20);
    	p1.updateGames(6);
    	p1.updateGames(4);
    	p1.updateGoals(12);
    	p2.updateGames(5);   
    	p2.updateGoals(8);
    	System.out.println(p1.getFullname() + " goals per game = " + p1.getGoalsPerGame());
    	System.out.println(p2.getFullname() + " goals per game = " + p2.getGoalsPerGame());
    	}
    
    }
    Last edited by pence; 05-22-2013 at 10:24 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

    Default Re: Hmm... Something is not right here!

    I see a few problems with the constructor for your HockeyPlayer class:

    Java Code:
    public HockeyPlayer(String string, int number)
        {
            String p1 ="Player One";
            fullname =p1;
            String p2 ="Player Two";
             
        }
    1) You are always setting the fullname variable to "Player One", regardless of what is passed in.
    2) You are not doing anything with the p2 variable that you declare and initialize to "Player Two".
    3) You are not doing anything with the number variable (2nd argument in your constructor).

    Try rewriting your constructor to set fullname equal to the String passed in (1st argument), and setting number equal to the int passed in (2nd argument).
    Another thing to consider, use more meaningful names for your constructor parameters. Instead of having a String variable called 'string', maybe name it
    fullName, since that is the member variable whose value you are setting with that parameter. Keep in mind that if you do that, inside of the constructor you
    will need to use the 'this' keyword:

    Java Code:
    public HockeyPlayer(String fullname, int number)
        {
             this.fullname = fullname;
             this.number = number;         
        }

    There are also some mistakes you are making with your methods:

    Java Code:
     public void number()
        {
            Number p1=10;
            Number p2=20;
        }
    Here, you have a method called number, which takes in no arguments, and is also a void type, meaning it does not return anything. However, inside of the method, you create and initialize 2 Number objects, p1 and p2, but they are not used.
    Last edited by sehudson; 05-22-2013 at 10:59 PM.

  3. #3
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Hmm... Something is not right here!

    Thanks for the quick reply! I'm going to try and fix the things you mentioned and see where that gets me :)

  4. #4
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Hmm... Something is not right here!

    Okay so I got the program to run this

    "
    Player One goals per game = 1.0
    Player Two goals per game = 1.0
    "

    Before it just ran
    "
    Player One goals per game = 1.0
    Player One goals per game = 1.0
    "

    So that is a start. I still can not figure out if I need to do anything else with number, and how to get the points per game to change to the correct answers.

    This is what I have...

    Java Code:
    public class HockeyPlayer
    {
    	String fullname;
    	public int number;
    	public int goals;
    	public int games;
    	
    	
    	public HockeyPlayer(String fullName, int number) 
    	{
    		this.fullname = fullName;
    		this.number = number;
    	    
    	}
    	
    	public HockeyPlayer()
    	{
    		games = 0;
    		goals = 0;
    	}
    	
    		
    
    	public void updateGames(int amount) 
    	{
    		games = games + amount;
    	}
    
    	public void updateGoals(int amount) 
    	{		
    		goals = goals + amount;
    	}
    	
    
    	public String getFullname() 
    	{
    		return fullname;	
    	}
    
    	public double getGoalsPerGame() 
    	{
    		return  (goals/games);
    		
    		
    	}
    }

  5. #5
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Hmm... Something is not right here!

    For anyone who was curious, I think I got it! Just took a few hours :)

    Java Code:
    public class HockeyPlayerTester 
    {
    	
    	public static void main(String[] args) 
    	{
    	System.out.println("Programmed by ...");
    	System.out.println();
    	HockeyPlayer p1 = new HockeyPlayer("Player One", 10);
    	HockeyPlayer p2 = new HockeyPlayer("Player Two", 20);
    	p1.updateGames(6);
    	p1.updateGames(4);
    	p1.updateGoals(12);
    	p2.updateGames(5);   
    	p2.updateGoals(8);
    	System.out.println(p1.getFullname() + " goals per game = " + p1.getGoalsPerGame());
    	System.out.println(p2.getFullname() + " goals per game = " + p2.getGoalsPerGame());
    	}
    
    }

    Java Code:
    public class HockeyPlayer
    {
    	String fullname;
    	public int number;
    	public double Goals;
    	public double Games;
    
    	
    	
    	public HockeyPlayer(String fullName, int number) 
    	{
    		this.fullname = fullName;
    		this.number = number;
    	    
    	}
    	
    	public HockeyPlayer()
    	{
    		Games = 0;
    		Goals = 0;
    	}
    	
    	
    	public void updateGames(int amount) 
    	{
    		Games = Games + amount;
    	}
    
    	public void updateGoals(int amount) 
    	{		
    		Goals = Goals + amount;
    	}
    	
    
    	public String getFullname() 
    	{
    		return fullname;	
    	}
    
    	
    	public double getGoalsPerGame() 
    	{
    		return Goals / Games;
    	}
    }

  6. #6
    owld3nmartin is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Hmm... Something is not right here!

    Pence, thank you so much for asking for help! I have this same project for my Java class, and I had no idea what to do. I tried to do it all on my own, but I just could not figure it out. I searched Google for a part of the instructions and found your post! Thank you so much! I cannot thank you enough!!! Thank you!

  7. #7
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Hmm... Something is not right here!

    Nice! UWF?

  8. #8
    owld3nmartin is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Hmm... Something is not right here!

    Yep! This is my first semester getting into Java, so I am very lost and confused. I am, however, very excited that I have found this forum!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •