Results 1 to 6 of 6
Thread: Method Writing Help
- 11-16-2010, 06:27 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Method Writing Help
With this following code:
By the way, there are not errors in this code as it compiles and runs fine.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()); } } }
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
- 11-16-2010, 06:37 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
So if the A_position member value is "pitcher" you want your method to return true and false otherwise? Here's a spoiler:
Note that this way the method takes care of the A_position being null.Java Code:public boolean isPitcher() { return "pitcher".equals(A_position); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2010, 06:41 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
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. :)
- 11-16-2010, 06:43 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
By the way,
could be replaced withJava Code:double returnBat = A_average; return returnBat;
ErikJava Code:return A_average;
I'm new to Java but I like to help where ever I can. :)
- 11-16-2010, 07:07 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
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?!
- 11-16-2010, 09:14 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Similar Threads
-
Writing a method
By Proton in forum New To JavaReplies: 10Last Post: 05-13-2010, 02:03 PM -
I need some help in writing a method
By loraineEd in forum New To JavaReplies: 2Last Post: 01-02-2010, 04:23 PM -
Writing a recursive method :S
By Thousand in forum New To JavaReplies: 1Last Post: 12-06-2009, 03:13 PM -
question about writing a toString method
By heather.diggs in forum New To JavaReplies: 2Last Post: 11-12-2008, 11:13 PM -
Writing the filter method
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 07-03-2007, 03:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks