Spaces:
Running
Running
Create 15B. Write a class based on UML diagram
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/15B. Write a class based on UML diagram
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a class according to the UML diagram below.
|
| 2 |
+
|
| 3 |
+
The functionality of the methods is not important (as long as the code compiles)
|
| 4 |
+
- it's enough that everything is defined according to the diagram.
|
| 5 |
+
https://ville.utu.fi/static_resources/jubery_utufi/Game_UML.png
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
Game
|
| 9 |
+
-name: String
|
| 10 |
+
-players: int
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
^
|
| 14 |
+
-
|
| 15 |
+
|
|
| 16 |
+
|
|
| 17 |
+
|
|
| 18 |
+
|
|
| 19 |
+
|
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
+Game(name: String, players: int)
|
| 23 |
+
+getName(): String
|
| 24 |
+
+setName(name: String): void
|
| 25 |
+
+getPlayers(): int
|
| 26 |
+
+setPlayers(players: int): void
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
=====================================
|
| 31 |
+
|
| 32 |
+
import java.util.Random;
|
| 33 |
+
import java.lang.reflect.Field;
|
| 34 |
+
|
| 35 |
+
public class Test {
|
| 36 |
+
public static void main(String[] args) {
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
// ADD
|
| 44 |
+
class Game {
|
| 45 |
+
// attributes
|
| 46 |
+
private String name;
|
| 47 |
+
private int players;
|
| 48 |
+
|
| 49 |
+
// constructor
|
| 50 |
+
public Game(String name, int players){
|
| 51 |
+
this.name = name;
|
| 52 |
+
this.players = players;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
public String getName() {
|
| 57 |
+
return this.name;
|
| 58 |
+
}
|
| 59 |
+
public void setName(String name) {
|
| 60 |
+
this.name = name;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
public int getPlayers() {
|
| 64 |
+
return this.players;
|
| 65 |
+
}
|
| 66 |
+
public void setPlayers(int players) {
|
| 67 |
+
this.players = players;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
Testing the class Game...
|
| 76 |
+
Constructor found, game object created!
|
| 77 |
+
|
| 78 |
+
Checking attributes...
|
| 79 |
+
Attribute name found!
|
| 80 |
+
Attribute type: class java.lang.String
|
| 81 |
+
Attribute private: true
|
| 82 |
+
|
| 83 |
+
Attribute players found!
|
| 84 |
+
Attribute type: int
|
| 85 |
+
Attribute private: true
|
| 86 |
+
|
| 87 |
+
Checking methods...
|
| 88 |
+
getName works!
|
| 89 |
+
getPlayers works!
|
| 90 |
+
|
| 91 |
+
setName works!
|
| 92 |
+
setPlayers works!
|
| 93 |
+
|