Spaces:
Running
Running
Create 16. Football match, part 1: Class Player
Browse files
Week 4: Writing classes/16. Football match, part 1: Class Player
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Let's start by implementing a football match in the Player class.
|
| 2 |
+
|
| 3 |
+
So write a class Player with the following properties:
|
| 4 |
+
Attribute 'number' (integer)
|
| 5 |
+
Attribute 'name' (string)
|
| 6 |
+
A constructor that takes the number and name as parameters and sets the values of the attributes
|
| 7 |
+
Get methods for number and name (name in the form 'getName')
|
| 8 |
+
Set methods for number and name (name of the form 'setName')
|
| 9 |
+
|
| 10 |
+
Note! As usual, the class must not be public - so do not use the attribute public in the definition.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
import java.util.Random;
|
| 17 |
+
import java.lang.reflect.Field;
|
| 18 |
+
|
| 19 |
+
public class Test{
|
| 20 |
+
public static void main(String[] args){
|
| 21 |
+
final Random r = new Random();
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
System.out.println("Testing class Player...");
|
| 26 |
+
|
| 27 |
+
Player pl = new Player(10, "Pele");
|
| 28 |
+
|
| 29 |
+
System.out.println("Player object created!");
|
| 30 |
+
|
| 31 |
+
try {
|
| 32 |
+
Field number = pl.getClass().getDeclaredField("number");
|
| 33 |
+
boolean numberPrivate = number.getModifiers() == 2;
|
| 34 |
+
System.out.println("Number is private: " + numberPrivate);
|
| 35 |
+
} catch (Exception e) {
|
| 36 |
+
System.out.println("Attribute number is not defined");
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
try {
|
| 40 |
+
Field name = pl.getClass().getDeclaredField("name");
|
| 41 |
+
boolean namePrivate = name.getModifiers() == 2;
|
| 42 |
+
System.out.println("Name is private: " + namePrivate);
|
| 43 |
+
} catch (Exception e) {
|
| 44 |
+
System.out.println("Attribute name is not defined");
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
System.out.println("Testing observation methods...");
|
| 48 |
+
System.out.println("Name: " + pl.getName());
|
| 49 |
+
System.out.println("Number: " + pl.getNumber());
|
| 50 |
+
|
| 51 |
+
System.out.println("Testing setting methods...");
|
| 52 |
+
pl.setName("Ronaldo");
|
| 53 |
+
pl.setNumber(7);
|
| 54 |
+
System.out.println("Name: " + pl.getName());
|
| 55 |
+
System.out.println("Number: " + pl.getNumber());
|
| 56 |
+
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
// ADD
|
| 62 |
+
class Player {
|
| 63 |
+
// attributes
|
| 64 |
+
private int number;
|
| 65 |
+
private String name;
|
| 66 |
+
|
| 67 |
+
// constructor
|
| 68 |
+
public Player(int number, String name) {
|
| 69 |
+
this.number = number;
|
| 70 |
+
this.name = name;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
public int getNumber() {
|
| 74 |
+
return this.number;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
public String getName() {
|
| 78 |
+
return this.name;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
public void setNumber(int number) {
|
| 82 |
+
this.number = number;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
public void setName(String name) {
|
| 86 |
+
this.name = name;
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
Testing class Player...
|
| 94 |
+
Player object created!
|
| 95 |
+
Number is private: true
|
| 96 |
+
Name is private: true
|
| 97 |
+
Testing observation methods...
|
| 98 |
+
Name: Pele
|
| 99 |
+
Number: 10
|
| 100 |
+
Testing setting methods...
|
| 101 |
+
Name: Ronaldo
|
| 102 |
+
Number: 7
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|