starting some new stuff in Java and im kind of having a hard time. I feel like im on the right track just for some reason cannot find a way around the problem I am having without violating encapsulation. I have these two classes called Weight and Project. I have finished both of them but get errors in the Project class. Which include, 'toOunces', 'ounces', and 'OUNCES_IN_A_POUND'. these are private in the Weight class. I already understand that I cannot carry over private variables, methods, ect. from one class to another. I just have no clue how to go about this without violating encapsulation. below i have both the Weight class and the Project class as well as the assignment details.
I am not looking for anyone to do the work for me as I am just looking for tips, tricks, and suggestions. Also please bear with me as I am still learning the Java language and on the first week of CMIS 242.
public class Weight {
//Private variables
private static final int OUNCES_IN_A_POUND = 16;
private int pounds;
private double ounces;
//A public parameterized constructor, which initializes the private variables.
public Weight(int pounds, double ounces) {
this.pounds = pounds;
this.ounces = ounces;
normalize();
}
//instance methods
private double toOunces() {
return (pounds * OUNCES_IN_A_POUND) + ounces;
}
private void normalize() {
while (ounces >= OUNCES_IN_A_POUND) {
pounds++;
ounces -= OUNCES_IN_A_POUND;
}
}
public boolean lessThan(Weight weight) {
double thisOunces = toOunces();
double otherOunces = weight.toOunces();
return thisOunces < otherOunces;
}
public void addTo(Weight weight) {
double thisOunces = toOunces();
double otherOunces = weight.toOunces();
double totalOunces = thisOunces + otherOunces;
pounds = (int) (totalOunces / OUNCES_IN_A_POUND);
ounces = totalOunces % OUNCES_IN_A_POUND;
normalize();
}
public String toString() {
return pounds + " pounds and " + String.format("%.2f", ounces) + " ounces";
}
}
----------------------------------------------------------------------------------------------------
public class Project {
private static Weight findMinimum(Weight weight1, Weight weight2, Weight weight3) {
Weight minimumWeight = weight1;
if (weight2.lessThan(minimumWeight)) {
minimumWeight = weight2;
}
if (weight3.lessThan(minimumWeight)) {
minimumWeight = weight3;
}
return minimumWeight;
}
private static Weight findMaximum(Weight weight1, Weight weight2, Weight weight3) {
Weight maximumWeight = weight1;
if (weight2.toOunces() > maximumWeight.toOunces()) {
maximumWeight = weight2;
}
if (weight3.toOunces() > maximumWeight.toOunces()) {
maximumWeight = weight3;
}
return maximumWeight;
}
private static Weight findAverage(Weight weight1, Weight weight2, Weight weight3) {
double totalOunces = weight1.toOunces() + weight2.toOunces() + weight3.toOunces();
double averageOunces = totalOunces / 3.0;
int averagePounds = (int) (averageOunces / Weight.OUNCES_IN_A_POUND);
double averageOuncesRemaining = averageOunces % Weight.OUNCES_IN_A_POUND;
return new Weight(averagePounds, averageOuncesRemaining);
}
public static void main(String[] args) {
//instances of the weight class
Weight weight1 = new Weight(11, 3);
Weight weight2 = new Weight(7, 20);
Weight weight3 = new Weight(14, 6);
System.out.println("Weight 1: " + weight1.toString());
System.out.println("Weight 2: " + weight2.toString());
System.out.println("Weight 3: " + weight3.toString());
Weight minimumWeight = findMinimum(weight1, weight2, weight3);
System.out.println("The minimum weight is " + minimumWeight.toString());
Weight maximumWeight = findMaximum(weight1, weight2, weight3);
System.out.println("The maximum weight is " + maximumWeight.toString());
Weight averageWeight = findAverage(weight1, weight2, weight3);
System.out.println("The average weight is " + averageWeight.toString());
}
}
link to the PDF file for the assignment details: https://learn.umgc.edu/d2l/common/viewFile.d2lfile/Database/NzQ5OTk1NjE/Assignment1.pdf?ou=1016228