Spaces:
Running
Running
TurkuBasicOOPinJava / Week 7: Enum, Generic Type, Streams, write to file, class diagram /15B. Write a class based on UML diagram
| Write a class according to the UML diagram below. | |
| The functionality of the methods is not important (as long as the code compiles) | |
| - it's enough that everything is defined according to the diagram. | |
| https://ville.utu.fi/static_resources/jubery_utufi/Game_UML.png | |
| Game | |
| -name: String | |
| -players: int | |
| ^ | |
| - | |
| | | |
| | | |
| | | |
| | | |
| | | |
| +Game(name: String, players: int) | |
| +getName(): String | |
| +setName(name: String): void | |
| +getPlayers(): int | |
| +setPlayers(players: int): void | |
| ===================================== | |
| import java.util.Random; | |
| import java.lang.reflect.Field; | |
| public class Test { | |
| public static void main(String[] args) { | |
| } | |
| } | |
| // ADD | |
| class Game { | |
| // attributes | |
| private String name; | |
| private int players; | |
| // constructor | |
| public Game(String name, int players){ | |
| this.name = name; | |
| this.players = players; | |
| } | |
| public String getName() { | |
| return this.name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public int getPlayers() { | |
| return this.players; | |
| } | |
| public void setPlayers(int players) { | |
| this.players = players; | |
| } | |
| } | |
| Testing the class Game... | |
| Constructor found, game object created! | |
| Checking attributes... | |
| Attribute name found! | |
| Attribute type: class java.lang.String | |
| Attribute private: true | |
| Attribute players found! | |
| Attribute type: int | |
| Attribute private: true | |
| Checking methods... | |
| getName works! | |
| getPlayers works! | |
| setName works! | |
| setPlayers works! | |