Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**Weight.java** **DriveWeight.java** **IO.java** Thanks! Encapsulation We saw in

ID: 3865883 • Letter: #

Question

**Weight.java**

**DriveWeight.java**

**IO.java**

Thanks!

Encapsulation We saw in Lab 7 that you could write a class without know anything about the classes that use your class except the interface (the method names they called in your class, what each method does, and what the parameters are for each method). This property is called "encapsulation": a class hides, or encapsulates, the details of how it represents data and how its methods work. We will further explore the idea in this lab. Download Start by downloading o Weight.java o DriveWeight.java o IO java (if it is not already in the folder where you are putting these files) What to do Weight is a class that represents a weight as a number of pounds and a number of ounces. DriveWeight is a class that uses Weight Note that, since there are 16 ounces in a pound, instead of representing a weight, e g., as 2 pounds 3 ounces, we could represent it as 35 total ounces because 2 16 +3-35. Your task is to change the class Weight so that it does just this - instead of two instance variables pounds and ounces, it should have only one instance variable, totalOunces. You have to do this in such a way that any class that uses Weight, e.g. DriveWeight, continues to work without any changes needed. In order to achieve this, you need to change Weight so that all the methods return the same things, but now do so by using the instance variable totalOunces instead of the variables pounds and ounces. To help get you started, the comments for getPounds tell you how to change that method.

Explanation / Answer

//**Weight.java**
public class Weight
{

/*int pounds; // change these two lines to just
int ounces; // the one line: int totalOunces
// then fix the rest of the */
private static int Ounces_Per_Pound = 16;
private int totalOunces;

public Weight(int pounds, int ounces){ // do not change this line
this.totalOunces = (pounds / Ounces_Per_Pound) + ounces;
}
  
public int getPounds( )
{ // do not change this line
return (totalOunces / Ounces_Per_Pound);
}

public int getOunces( ){ // do not change this line
return (totalOunces % Ounces_Per_Pound);
}

public int getTotalOunces( )
{ // do not change this line
return totalOunces;
}

public Weight sum(Weight other)
{ // do not change this line
int totalOuncesSum = totalOunces + other.getTotalOunces();
int totalPounds = totalOuncesSum / Ounces_Per_Pound;
int restOunces = totalOuncesSum % Ounces_Per_Pound;
return new Weight(totalPounds, restOunces);
}
}