int num = 14; Write the code to add the value contained in num to the ArrayList.
ID: 3562291 • Letter: I
Question
int num = 14;
Write the code to add the value contained in num to the ArrayList. Remember that ArrayLists can only hold objects.
//precondition: list is a non-empty ArrayList containing Player objects.
//postcondition: returns the total of the int values stored in the points
field of the Player objects contained inside the ArrayList.
//the Player class contains method getPoints() which returns the point field
of the Player object.
public static double getTotalPoints (ArrayList list){
Explanation / Answer
// Given that there is an ArrayList named staff that contains Employee Objects, write the code that will loop through staff, calling and outputting the toString() method for each Employee object.
1) for(Employee emp : staff)
{
System.out.println(emp.toString());
}
// ArrayList list = new ArrayList();
//int num = 14;
//Write the code to add the value contained in num to the ArrayList. Remember that ArrayLists can only hold objects.
list.add(num);
3)
public static double getTotalPoints (ArrayList list)
{
//precondition: list is a non-empty ArrayList containing Player objects.
if(list != null && list.size() > 0)
{
int totalPoints = 0;
for(Player player : list)
{
int playerPoints = player.getPoints();
totalPoints = totalPoints + playerPoints;
}
// return as double
return (double)totalPoints ;
}
// list doesn't contain any Objects
else
return 0.0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.