Results 1 to 6 of 6
  1. #1
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default Method Writing Help

    With this following code:
    Java Code:
    public class baseballPlayer {
    	private String A_FirstName;
    	private String A_LastName;
    	private double A_average;
    	private String A_position;
    	private String A_Team;
    	
    	public baseballPlayer(String FirstName, String LastName, double Batting, String pos, String TeamName)
    	{
    		A_FirstName = FirstName;
    		A_LastName = LastName;
    		A_average = Batting;
    		A_position = pos;
    		A_Team = TeamName;
    	}
    		
    		public String getFirstName()
    		{
    			String returnFirstName = A_FirstName; 
    			return returnFirstName; 
    		}
    		public String getLastName()
    		{
    			String returnLastName = A_LastName; 
    			return returnLastName; 
    		
    		}
    		public double getBatting()
    		{
    			double returnBat = A_average; 
    			return returnBat; 
    		}
    		public String getPos()
    		{
    			String returnPos = A_position; 
    			return returnPos; 
    		}
    		public String getTeam()
    		{
    			String returnTeam = A_Team; 
    			return returnTeam; 
    		}
    		
    	public double findBattingAverage (baseballPlayer Team[]){
    		double sum = 0.0;
    		for (int i = 0; i<Team.length - 1; i++)
    		{
    		sum += Team[i].getBatting();
    		}
    		return sum/Team.length;
    	}
    	
    	
    public static void main(String[] args){
    	baseballPlayer team[]=new baseballPlayer[8];
    	team[0]= new baseballPlayer("Michael", "Hall", 0.500, "First Base", "Angels");
    	team[1]= new baseballPlayer("Carl", "Ford", 0.265, "Third Base", "Angels");
    	team[2]= new baseballPlayer("John", "Daniels", 0.750, "Catcher", "Angels");
    	team[3]= new baseballPlayer("Dominique", "Wilkins", 0.300, "Right Field", "Angels");
    	team[4]= new baseballPlayer("Bugs", "Bunny", 0.650, "Center Field", "Angels");
    	team[5]= new baseballPlayer("Derek", "Jones", 0.355, "Second Base", "Angels");
    	team[6]= new baseballPlayer("Jason", "Giambi", 0.450, "Left Field", "Angels");
    	team[7]= new baseballPlayer("Elmer", "Fudd", 0.200, "Pitcher", "Angels");
    
    	for (int i=0; i<team.length ; i++)
    	{
    	System.out.println(team[i].getFirstName());
    	}
    }
    }
    By the way, there are not errors in this code as it compiles and runs fine.

    We are required to write a method to check if we have a pitcher( the name of the position on the team), if we dont return false and if we do return true.

    How would i start this method? would i have to use a for loop?

    could it be
    public boolean checkPitcher
    ... for loop?
    how to check if pitcher is in team? please describe

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by socboy6579 View Post
    We are required to write a method to check if we have a pitcher( the name of the position on the team), if we dont return false and if we do return true.

    How would i start this method? would i have to use a for loop?

    could it be
    public boolean checkPitcher
    ... for loop?
    how to check if pitcher is in team? please describe
    So if the A_position member value is "pitcher" you want your method to return true and false otherwise? Here's a spoiler:

    Java Code:
    public boolean isPitcher() {
       return "pitcher".equals(A_position);
    }
    Note that this way the method takes care of the A_position being null.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Yep, a for loop would be perfect.

    I'd create three classes: BaseballTeam, BaseballPlayer and BaseballApp.

    The BaseballTeam would have the methods

    private BaseballPlayer[] team;
    public void addPlayer(BaseballPlayer player)
    public double getBettingAverage()
    public boolean hasPosition(String position)

    The BaseballPlayer class would have the methods

    public String getFirstName() and so on

    The BaseballApp would have the main class in which you create the baseball playes and add them to the team.

    In BaseballTeam.hasPosition you would go through the team to check whether the team has a certain position. You can use the same sort of loop as you are already using in the findBettingAverage.

    Good luck.
    I'm new to Java but I like to help where ever I can. :)

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    By the way,

    Java Code:
        double returnBat = A_average; 
        return returnBat;
    could be replaced with

    Java Code:
        return A_average;
    Erik
    I'm new to Java but I like to help where ever I can. :)

  5. #5
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    now i figured out the code,, now its im waiting to figure out how to make a client class to test all the methods..
    any pointers?!

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    You can use the BaseballApp as client class.
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. Writing a method
    By Proton in forum New To Java
    Replies: 10
    Last Post: 05-13-2010, 03:03 PM
  2. I need some help in writing a method
    By loraineEd in forum New To Java
    Replies: 2
    Last Post: 01-02-2010, 05:23 PM
  3. Writing a recursive method :S
    By Thousand in forum New To Java
    Replies: 1
    Last Post: 12-06-2009, 04:13 PM
  4. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 12:13 AM
  5. Writing the filter method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2007, 04:32 PM

Posting Permissions

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