KaiquanMah commited on
Commit
a3b5dbb
·
verified ·
1 Parent(s): 8ac7df8

public dtype getAttrituteName() {...}

Browse files
Week 4: Writing classes/08b. Write get methods ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In the attached program, the class 'Game' is defined.
2
+
3
+ The class has 4 attributes defined, but no get methods.
4
+ Complete the class by writing get methods for all the attributes.
5
+ Name the methods in the format shown in the tutorial (for example, getName()).
6
+
7
+
8
+ import java.util.Random;
9
+
10
+ public class Test{
11
+ public static void main(String[] args){
12
+ final Random r = new Random();
13
+
14
+
15
+ System.out.println("Testing Game-class");
16
+ Game p1 = new Game("Tetrix", "puzzle game", r.nextInt(20) + 1984, r.nextInt(10) + 8);
17
+ Game p2 = new Game("Doom 9", "first-person shooter", r.nextInt(20) + 1984, r.nextInt(10) + 8);
18
+ Game p3 = new Game("Outrun 4", "racing game", r.nextInt(20) + 1984, r.nextInt(10) + 8);
19
+
20
+ Game[] pelit = {p1, p2, p3};
21
+
22
+ for (Game game : pelit) {
23
+ System.out.println("Testing the game: " + game);
24
+ System.out.println("name: " + game.getName());
25
+ System.out.println("genre: " + game.getGenre());
26
+ System.out.println("release year: " + game.getReleaseYear());
27
+ System.out.println("age rating: " + game.getAgeRating());
28
+ System.out.println("");
29
+ }
30
+
31
+ }
32
+ }
33
+ class Game {
34
+ private String name;
35
+ private String genre;
36
+ private int releaseYear;
37
+ private int ageRating;
38
+
39
+ public Game(String name, String genre, int releaseYear, int ageRating) {
40
+ this.name = name;
41
+ this.genre = genre;
42
+ this.releaseYear = releaseYear;
43
+ this.ageRating = ageRating;
44
+ }
45
+
46
+
47
+
48
+ // ADD OBJECT OBSERVATION METHODS
49
+ public String getName() {
50
+ return this.name;
51
+ }
52
+ public String getGenre() {
53
+ return this.genre;
54
+ }
55
+ public int getReleaseYear() {
56
+ return this.releaseYear;
57
+ }
58
+ public int getAgeRating() {
59
+ return this.ageRating;
60
+ }
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+ @Override
69
+ public String toString() {
70
+ return "Game(" + name + ", " + genre + ", " + releaseYear + ", "
71
+ + ageRating + ")";
72
+ }
73
+ }
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ Testing Game-class
82
+ Testing the game: Game(Tetrix, puzzle game, 2000, 14)
83
+ name: Tetrix
84
+ genre: puzzle game
85
+ release year: 2000
86
+ age rating: 14
87
+
88
+ Testing the game: Game(Doom 9, first-person shooter, 1987, 12)
89
+ name: Doom 9
90
+ genre: first-person shooter
91
+ release year: 1987
92
+ age rating: 12
93
+
94
+ Testing the game: Game(Outrun 4, racing game, 1998, 9)
95
+ name: Outrun 4
96
+ genre: racing game
97
+ release year: 1998
98
+ age rating: 9
99
+
100
+
101
+
102
+
103
+