Edited by noel222, 23 February 2012 - 03:32 PM.
14 replies to this topic
#1
Posted 24 January 2012 - 03:43 PM
nevermind
|
|
|
#2
Posted 24 January 2012 - 05:13 PM
Quote
have to write a new class that puts each teammate in an arraylist
When you get the class, create one for each teammate and add it to the arraylist.
Then to sum up the goals for the all the teammates, write a loop that gets the object for each teammate and gets the goal count from it and adds that to the total.
#3
Posted 24 January 2012 - 05:18 PM
I already have a class where I state each teammates goals. Now I want to put each teammate into the array and then have all the goals added together to create the total goals for the team. But, I literally have no clue on how to do this. Can you please elaborate.
Thank You
Thank You
#4
Posted 24 January 2012 - 05:22 PM
If you have a TeamMate class, then you need to add an instance of it for each teammate to the ArrayList object.
See the API doc for the ArrayList class for the method to use to add objects to the ArrayList.
When you have added all of the teammate objects to the class, you need a loop to get each object from the arraylist, get the goal from that object and add that to the total sum.
See the API doc for the ArrayList class for the method to use to add objects to the ArrayList.
When you have added all of the teammate objects to the class, you need a loop to get each object from the arraylist, get the goal from that object and add that to the total sum.
#5
Posted 24 January 2012 - 05:25 PM
nevermind
Edited by noel222, 23 February 2012 - 03:32 PM.
#6
Posted 24 January 2012 - 05:32 PM
That is not what I was thinking of.
Create a new class: TeamMate that will hold the name and number of goals.
Create instances for each teammate and add them to the list, etc as I've already said above.
Be sure to define the ArrayList outside of any methods so that all methods can see it.
Create a new class: TeamMate that will hold the name and number of goals.
Create instances for each teammate and add them to the list, etc as I've already said above.
Be sure to define the ArrayList outside of any methods so that all methods can see it.
#7
Posted 24 January 2012 - 05:40 PM
I know I keep asking you the same question, but I really am not understanding. I thought that the teammate.add code added each teammate with the values that I have from my teammate class. Then the rest of my code would add up the values of from the teammate class. I am obviously not seeing this correctly, but I am not sure of what to do.
#8
Posted 24 January 2012 - 05:46 PM
Take it one small step at a time.
Add the teammates to the array list and print out the contents of the arraylist by printing the teammate variable.
System.out.println(teammate);
Check if all the data is there.
Add the teammates to the array list and print out the contents of the arraylist by printing the teammate variable.
System.out.println(teammate);
Check if all the data is there.
#9
Posted 24 January 2012 - 05:54 PM
I attempted to do this, but it did not work. I searched online, and I cannot seem to add each teammate and the value associated with it to the table. In my code, I have "chris" and the other teammates, and I know that is not adding the actual value of chris, but I just cannot seem to figure out how to do this. When I don't use the "", I get an error saying chris can't be resolved to a variable
#10
Posted 24 January 2012 - 05:56 PM
You need to define a class TeamMate that has two values: the name and the number of goals.
Create an instance of this class with the two values and add that to the arraylist.
Create an instance of this class with the two values and add that to the arraylist.
#11
Posted 24 January 2012 - 06:08 PM
Ok, so I have decided to manually enter the number of goals of each teammate instead of calling the value from another class. This is what I have now, but it still is not adding the values together and returning the sum
#12
Posted 24 January 2012 - 06:09 PM
Quote
it still is not adding the values together
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









