-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.java
More file actions
143 lines (92 loc) · 3.05 KB
/
Copy pathCharacter.java
File metadata and controls
143 lines (92 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package ethicalengine;
import java.util.Random;
public abstract class Character {
/*
* enum Gender .
* enum BodyType .
*
* Gender must include the types FEMALE and MALE as well as a default option UNKNOWN .
* But can also include more diverse options if required,
*
*
*
* In computer programming, specifically object-oriented programming, a class invariant (or type invariant) is an invariant used for constraining objects of a class. Methods of the class should preserve the invariant. The class invariant constrains the state stored in the object.
> Class invariants are established during construction and constantly maintained between calls to public methods.
> Code within functions may break invariants as long as the invariants are restored before a public function ends.
*
*
* Age should be considered as a class invariant for which the following statement should always hold true
* age >= 0 .
*
* */
private int age = 0 ;
public Gender gender ;
private BodyType bodyType ;
/*
* 1. Gender must include types FEMALE, MALE AND UNKNOWN but also can include more diverse options
* if you can choose to. - DONE
*
* */
public enum Gender {
FEMALE, MALE, UNKNOWN ;
public static Gender getRandom() {
return values()[ new Random().nextInt(values().length) ];
}
}
/*
* 2. BodyType would include the types average, athletic, overweight and unspecified.
*
* DONE .
* */
/*
* Classes inheriting from Character.java are : Person.java and Animal.java
*
* DONE
*
* */
public enum BodyType {
AVERAGE, ATHLETIC, OVERWEIGHT, UNSPECIFIED ;
public static BodyType getRandom() {
return values()[ new Random().nextInt(values().length) ];
}
}
/*
* Make sure that the empty constructor initializes all of the attributes with the appropriate default values.
* */
public Character () {
this.age = 0 ; // age should be considered as a class invariant for which following statement always yield true age >= 0
this.gender = Gender.UNKNOWN ;
this.bodyType = BodyType.UNSPECIFIED ;
}
public Character(int age, Gender gender, BodyType bodyType) {
this.age = age ;
this.gender = gender;
this.bodyType = bodyType ;
}
public Character (Character c) {
this.age = c.getAge() ;
this.gender = c.getGender();
this.bodyType = c.getBodyType() ;
}
public int getAge() {
return this.age ;
}
public void setAge(int age) {
if(age >= 0 )
this.age = age ;
}
public Gender getGender () {
return this.gender;
}
public void setGender (Gender gender) {
this.gender = gender ;
}
public BodyType getBodyType () {
return this.bodyType ;
}
public void setBodyType ( BodyType bodytype) {
this.bodyType = bodytype ;
}
protected abstract boolean isYou();
public abstract String toString();
}