-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeight.java
246 lines (221 loc) · 4.82 KB
/
Weight.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import java.io.Serializable;
public class Weight implements Comparable, Cloneable, Serializable{
private int lbs;
private int ounces;
// no arg constructor
public Weight() {
this.lbs = 0;
this.ounces = 0;
}
// one arg constructor
public Weight(int lbs) {
if(!(lbs < 0))
this.lbs = lbs;
else {
System.out.println("Error: Given weight is under 0. Weight is set to 0.");
this.lbs = 0;
}
}
// constructor
public Weight(int lbs, int ounces) {
int ouncesInPounds = ounces / 16;
lbs += ouncesInPounds;
ounces %= 16;
if(ounces < 0) {
lbs -= 1;
ounces = 16 + ounces;
}
if(!(lbs < 0)) {
this.lbs = lbs;
this.ounces = ounces;
}
else {
System.out.println("Error: Given weight is under 0. Weight is set to 0.");
this.lbs = 0;
this.ounces = 0;
}
}
// copy constructor
private Weight(Weight other) {
this(other.lbs, other.ounces);
}
/**
* getLbs
* -----------
* PRE: Weight is not null
* POST: Returns lbs as an integer
*/
public int getLbs() {
return lbs;
}
/**
* setLbs
* -----------
* PRE: Parameter is positive
* POST: Sets lbs to the passed integer
*/
public void setLbs(int lbs) {
if(lbs >= 0) {
this.lbs = lbs;
}
else {
System.out.println("Given weight less than 0. Did not set weight.");
}
}
/**
* getOunces
* -----------
* PRE: Weight is not null
* POST: Returns ounces as an integer
*/
public int getOunces() {
return ounces;
}
/**
* setOunces
* -----------
* PRE: The parameter in ounces combined with lbs is positive
* POST: Sets ounces to the passed integer
*/
public void setOunces(int ounces) {
int ouncesInPounds = ounces / 16;
if(ouncesInPounds + this.lbs < 0) {
System.out.println("Given weight result in weight below 0. Did not change weight.");
return;
}
if(ounces < 0) {
this.lbs -= 1;
this.ounces = 16 - ounces;
}
else {
this.lbs += ouncesInPounds;
this.ounces = ounces % 16;
}
}
/**
* setWeight
* -----------
* PRE: lbs and ounces combined must be positive
* POST: sets weight's lbs and ounces in simplified version
*/
public void setWeight(int lbs, int ounces) {
int ouncesInPounds = ounces / 16;
lbs += ouncesInPounds;
ounces %= 16;
if(ounces < 0) {
lbs -= 1;
ounces = 16 + ounces;
}
if(lbs >= 0) {
this.lbs = lbs;
this.ounces = ounces;
}
else {
System.out.println("Given weight result in weight below 0. Did not change weight.");
}
}
/**
* getWeight
* -----------
* PRE: Weight is not null
* POST: returns double of weight in lbs
*/
public double getWeight() {
return Double.valueOf(this.lbs) + (Double.valueOf(this.ounces) / 16);
}
/**
* add (one arg)
* -----------
* PRE: Parameter combined with current weight must be positive
* POST: Current weight will add the passed parameter (lbs)
*/
public void add(int lbs) {
if(this.lbs + lbs >= 0) {
this.lbs += lbs;
}
else {
System.out.println("Added weight combines for under 0. Did not add weight.");
}
}
/**
* add (two args)
* -----------
* PRE: Parameter combined with current weight must be positive
* POST: Current weight will add the passed parameters
*/
public void add(int lbs, int ounces) {
int ouncesInPounds = ounces / 16;
lbs += ouncesInPounds;
ounces %= 16;
if(ounces < 0) {
lbs -= 1;
ounces = 16 + ounces;
}
if(this.lbs + lbs >= 0) {
this.lbs += lbs;
this.ounces += ounces;
}
}
/**
* add(Weight)
* -----------
* PRE: other is not null
* POST: adds the other weight onto this instance's weight
*/
public void add(Weight other) {
this.add(other.lbs, other.ounces);
}
/**
* equals
* -----------
* PRE: This weight and parameter must not be null
* POST: Returns true if weights are equal, returns false if not equal
*/
public boolean equals(Weight other) {
if(this.lbs == other.lbs &&
this.ounces == other.ounces)
return true;
else
return false;
}
/**
* toString
* -----------
* PRE: Weight is not null
* POST: Returns string form of weight. ex. "5 lbs. 14 oz."
*/
public String toString() {
return this.lbs + " lbs. " + this.ounces + " oz.";
}
/**
* clone
* -----
* Returns deep copy of Weight
*/
@Override
public Weight clone() {
return new Weight(this.lbs, this.ounces);
}
/**
* compareTo
* ---------
* PRE: o is not null
* POST: returns -2 if o is not of type Weight,
* return -1 if this weight less than other weight
* returns 0 if weights are equal
* returns 1 if this weight is greater than other weight
*/
@Override
public int compareTo(Object o) {
if(o.getClass() != this.getClass())
return -2;
Weight other = (Weight) o;
if(this.equals(other))
return 0;
if(this.lbs < other.lbs)
return -1;
else if(this.lbs == other.lbs && this.ounces < other.ounces)
return -1;
return 1;
}
}