Spaces:
Running
Running
Create 18. Football match, part 3: Class Match
Browse files
Week 4: Writing classes/18. Football match, part 3: Class Match
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Finally, write a class to model one football match.
|
| 2 |
+
|
| 3 |
+
So write a (non-public) class 'Match' with the following properties:
|
| 4 |
+
Attributes team1 and team2, with type Team
|
| 5 |
+
Attributes of integer type goals1 and goals2
|
| 6 |
+
A constructor that takes teams 1 and 2 as parameters (but not goal totals).
|
| 7 |
+
Method void addGoal(Team team), which increases the number of goals of the given team by one
|
| 8 |
+
Method printStatus(), which prints the game status in the format shown in the example below;
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Example output when calling the method printStatus:
|
| 12 |
+
FCT - UniFC: 1 - 0
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import java.lang.reflect.Field;
|
| 19 |
+
import java.util.ArrayList;
|
| 20 |
+
import java.util.HashMap;
|
| 21 |
+
import java.util.Random;
|
| 22 |
+
|
| 23 |
+
public class Test{
|
| 24 |
+
public static void main(String[] args){
|
| 25 |
+
final Random r = new Random();
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
System.out.println("Testing class Match...");
|
| 29 |
+
|
| 30 |
+
Team t1 = new Team("FCT");
|
| 31 |
+
Team t2 = new Team("UniFC");
|
| 32 |
+
|
| 33 |
+
Match match = new Match(t1, t2);
|
| 34 |
+
System.out.println("Match object created!");
|
| 35 |
+
|
| 36 |
+
for (String fname : new String[] {"team1", "team2", "goals1", "goals2"}) {
|
| 37 |
+
try {
|
| 38 |
+
Field fld = match.getClass().getDeclaredField(fname);
|
| 39 |
+
boolean isPrivate = fld.getModifiers() == 2;
|
| 40 |
+
System.out.println(fname + " is private: " + isPrivate);
|
| 41 |
+
} catch (Exception e) {
|
| 42 |
+
System.out.println("Attribute " + fname + " is not defined");
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
System.out.println("Starting situation: ");
|
| 47 |
+
match.printStatus();
|
| 48 |
+
|
| 49 |
+
System.out.println("Adding goals...");
|
| 50 |
+
|
| 51 |
+
match.addGoal(t1);
|
| 52 |
+
match.printStatus();
|
| 53 |
+
|
| 54 |
+
match.addGoal(t2);
|
| 55 |
+
match.printStatus();
|
| 56 |
+
|
| 57 |
+
match.addGoal(t1);
|
| 58 |
+
match.printStatus();
|
| 59 |
+
|
| 60 |
+
match.addGoal(t1);
|
| 61 |
+
match.printStatus();
|
| 62 |
+
|
| 63 |
+
match.addGoal(t2);
|
| 64 |
+
match.printStatus();
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
//ADD
|
| 73 |
+
class Match {
|
| 74 |
+
// attributes
|
| 75 |
+
private Team team1;
|
| 76 |
+
private Team team2;
|
| 77 |
+
private int goals1;
|
| 78 |
+
private int goals2;
|
| 79 |
+
|
| 80 |
+
// constructor
|
| 81 |
+
public Match(Team team1, Team team2) {
|
| 82 |
+
this.team1 = team1;
|
| 83 |
+
this.team2 = team2;
|
| 84 |
+
// remember to initialise attributes to avoid nullpointerexception
|
| 85 |
+
this.goals1 = 0;
|
| 86 |
+
this.goals2 = 0;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
public void addGoal(Team team) {
|
| 90 |
+
if (team1.equals(team)) {
|
| 91 |
+
this.goals1 += 1;
|
| 92 |
+
}
|
| 93 |
+
else {
|
| 94 |
+
this.goals2 += 1;
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
public void printStatus() {
|
| 99 |
+
System.out.println(this.team1.getName() + " - " + this.team2.getName() + ": " + this.goals1 + " - " + this.goals2);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
Testing class Match...
|
| 109 |
+
Match object created!
|
| 110 |
+
team1 is private: true
|
| 111 |
+
team2 is private: true
|
| 112 |
+
goals1 is private: true
|
| 113 |
+
goals2 is private: true
|
| 114 |
+
Starting situation:
|
| 115 |
+
FCT - UniFC: 0 - 0
|
| 116 |
+
Adding goals...
|
| 117 |
+
FCT - UniFC: 1 - 0
|
| 118 |
+
FCT - UniFC: 1 - 1
|
| 119 |
+
FCT - UniFC: 2 - 1
|
| 120 |
+
FCT - UniFC: 3 - 1
|
| 121 |
+
FCT - UniFC: 3 - 2
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|