-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeight.java
More file actions
99 lines (79 loc) · 2.24 KB
/
Weight.java
File metadata and controls
99 lines (79 loc) · 2.24 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
/*
* Matthew Homan
* CMIS 242 7382
* March 27, 2023
* Project 1
*/
//Weight Class for all Weight Objects
public class Weight {
//Initialize variables
private final int OUNCES_IN_A_POUND = 16;
private int pounds;
private double ounces;
//Constructor
public Weight (int pounds, double ounces) {
this.pounds = pounds;
this.ounces = ounces;
//Call normalize to ensure there are not more ounces than 15
normalize();
}
//getPounds method which calls toOunces in order to be able to perform necessary math
public double getPounds() {
double convertedOunces = 0;
convertedOunces = toOunces();
return convertedOunces;
}
//Normal getters and setters for other variables
public void setPounds(int pounds) {
this.pounds = pounds;
}
public double getOunces() {
return ounces;
}
public void setOunces(double ounces) {
this.ounces = ounces;
}
//toOunces which converts pounds to ounces
private double toOunces () {
return (pounds * OUNCES_IN_A_POUND) + ounces;
}
//normalize method to ensure there are not more ounces than 15
private void normalize () {
//do-while loop to reduce ounces to under 16 while adding to pounds
do {
if (ounces >= OUNCES_IN_A_POUND) {
pounds++;
ounces = ounces - OUNCES_IN_A_POUND;
}
} while (ounces >= OUNCES_IN_A_POUND);
}
//lessThan method to find the smallest weight
public boolean lessThan (Weight weight) {
//if-else statements to compare two objects' pounds and then ounces
if (this.pounds < weight.pounds) {
return true;
}
else if (this.pounds > weight.pounds) {
return false;
}
else {
if (this.ounces < weight.ounces) {
return true;
}
else {
return false;
}
}
}
//addTo method to sum multiple weights
public void addTo (Weight weight) {
this.pounds = this.pounds + weight.pounds;
this.ounces = this.ounces + weight.ounces;
//normalize is added here to change averageWeight into a normalized figure for output
normalize();
}
//toString method for printing pounds and ounces
public String toString () {
return pounds + " pounds and " + String.format("%.2f", ounces) + " ounces.";
}
}