-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject1.java
More file actions
99 lines (79 loc) · 2.99 KB
/
Project1.java
File metadata and controls
99 lines (79 loc) · 2.99 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
*/
public class Project1 {
//findMinimum method to find and return minimum weight
private static Weight findMinimum (Weight weight1, Weight weight2, Weight weight3) {
//if-else statements to determine if weight1, weight2, or weight3 is smallest
if ((weight1.lessThan(weight2)) && (weight1.lessThan(weight3))) {
return weight1;
}
else if ((weight2.lessThan(weight1)) && (weight2.lessThan(weight3))) {
return weight2;
}
else {
return weight3;
}
}
//findMaximum method to find and return maximum weight
private static Weight findMaximum (Weight weight1, Weight weight2, Weight weight3) {
//if-else statements to determine if weight1, weight2, or weight3 is largest
if ((!weight1.lessThan(weight2)) && (!weight1.lessThan(weight3))) {
return weight1;
}
else if ((!weight2.lessThan(weight1)) && (!weight2.lessThan(weight3))) {
return weight2;
}
else {
return weight3;
}
}
//findAverage method to find and return average weight
private static Weight findAverage (Weight weight1, Weight weight2, Weight weight3) {
//Initialize variables
double average;
int numOfObjects = 0;
//Create averageWeight object
Weight averageWeight = new Weight(0, 0);
//Add all weights to averageWeight
averageWeight.addTo(weight1);
numOfObjects++;
averageWeight.addTo(weight2);
numOfObjects++;
averageWeight.addTo(weight3);
numOfObjects++;
//Calculate average which will return a value of ounces
average = averageWeight.getPounds() / numOfObjects;
//Prepare averageWeight to be normalized with new average
averageWeight.setPounds(0);
averageWeight.setOunces(average);
//Create temp object
Weight temp = new Weight(0, 0);
//Call addTo method with averageWeight object to force normalization
averageWeight.addTo(temp);
return averageWeight;
}
//main method
public static void main(String[] args) {
//Create objects
Weight weight1 = new Weight(11, 3);
Weight weight2 = new Weight(7, 20);
Weight weight3 = new Weight(14, 6);
//Print proof of object creation
System.out.println("Created weight1 with " + weight1.toString());
System.out.println("Created weight2 with " + weight2.toString());
System.out.println("Created weight3 with " + weight3.toString());
//Call findMinimum
Weight minimumWeight = findMinimum(weight1, weight2, weight3);
System.out.println("\nThe minimum weight is " + minimumWeight.toString());
//Call findMaximum
Weight maximumWeight = findMaximum(weight1, weight2, weight3);
System.out.println("\nThe maximum weight is " + maximumWeight.toString());
//Call findAverage
Weight averageWeight = findAverage(weight1, weight2, weight3);
System.out.println("\nThe average weight is " + averageWeight.toString());
}
}