Spaces:
Running
Running
File size: 3,336 Bytes
cb1f949 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | Note! This programming task can also be solved without using streams - as can actually all tasks related to streams.
Thus, you are not forced to use streams in the tasks - however, it's advisable to 'try using streams at first'.
In the program, the class 'HockeyPlayer' is defined.
Familiarize yourself with the properties of the class and 'write the class method'
public static void print(ArrayList<HockeyPlayer> players)
which uses a stream to print the details of the players in the list exactly as shown in the example below.
The points should include both the player's assists and goals.
The program prints:
Michael Johnson, 21 points
James Smith, 50 points
Oliver Williams, 5 points
import java.util.Random;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
final Random r = new Random();
String[] fn = "Oliver Harry Michael George Leo Jack James William Noah".split(" ");
String[] ln = "Johnson Smith Davis Miller Wilson Clark Thompson Lewis".split(" ");
int count = r.nextInt(5) + 5;
ArrayList<HockeyPlayer> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
HockeyPlayer hp = new HockeyPlayer(fn[r.nextInt(fn.length)] + " " +
ln[r.nextInt(ln.length)], r.nextInt(30), r.nextInt(30));
System.out.println("Added to list " + hp);
list.add(hp);
}
System.out.println("");
System.out.println("Printing:");
print(list);
}
//ADD
public static void print(ArrayList<HockeyPlayer> players) {
// approach 1 forEach - works
// players.stream().forEach(player -> System.out.println(player.getName() + ", " + (player.getGoals() + player.getAssists()) + " points"));
// approach 2 for - works
for (int i = 0; i < players.size(); i++) {
System.out.println(players.get(i).getName() + ", "
+ (players.get(i).getGoals() + players.get(i).getAssists())
+ " points");
}
}
}
class HockeyPlayer {
private String name;
private int goals;
private int assists;
public HockeyPlayer(String name, int goals, int assists) {
this.name = name;
this.goals = goals;
this.assists = assists;
}
public String getName() {
return name;
}
public int getGoals() {
return goals;
}
public int getAssists() {
return assists;
}
public String toString() {
return name + ", " + goals + " goals and " + assists + " assists.";
}
}
Added to list James Miller, 10 goals and 1 assists.
Added to list George Johnson, 14 goals and 18 assists.
Added to list Noah Thompson, 18 goals and 13 assists.
Added to list George Johnson, 18 goals and 20 assists.
Added to list Leo Thompson, 26 goals and 18 assists.
Added to list Leo Johnson, 10 goals and 29 assists.
Added to list George Wilson, 14 goals and 28 assists.
Added to list Leo Miller, 26 goals and 11 assists.
Printing:
James Miller, 11 points
George Johnson, 32 points
Noah Thompson, 31 points
George Johnson, 38 points
Leo Thompson, 44 points
Leo Johnson, 39 points
George Wilson, 42 points
Leo Miller, 37 points
|