Introduction In this lab you will change a simple class so that it uses a differ
ID: 3867062 • Letter: I
Question
Introduction
In this lab you will change a simple class so that it uses a different internal representation but does not change its interface.
This is not a test. You may use your textbook and class notes, and you may ask the TAs for help and hints (not answers, of course). In particular, when you see error messages that you cannot decipher, you should ask the TAs to explain them. Labs are graded on a 4 point scale: 4 - perfect or almost perfect, 3 - some major problems but otherwise right, 2 - you turned something in, 0 - nothing turned in.
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
Weight.java
DriveWeight.java
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.
--------------------------------------------------------------------------------------------------------------
DriveWeight.java
----------------------------------------------------------------------------------------------
Weight.java
----------------------------------------------------------------------------------------------------
IO.java
public class DriveWeight{ // do not change anything in this class and do not turn it in public static Weight readWeight( ){ int lbs = IO.readInt( ); int ozs = IO.readInt( ); return new Weight(lbs, ozs); } public static void main(String [ ] args){ Weight w1 = readWeight( ); Weight w2 = readWeight( ); Weight w3 = w1.sum(w2); System.out.println("the sum is " + w3.getPounds( ) + " pounds, " + w3.getOunces( ) + " ounces = " + w3.getTotalOunces( ) + " ounces"); } }
Explanation / Answer
public class Weight{
int totalOunces;
public Weight(int pounds, int ounces){ // do not change this line
this.totalOunces = (pounds * 16) + ounces;
}
public int getPounds( ){ // do not change this line
return this.totalOunces/16;
}
public int getOunces( ){ // do not change this line
return this.totalOunces - (this.getPounds() * 16);
}
public int getTotalOunces( ){ // do not change this line
return this.totalOunces;
}
public Weight sum(Weight other){ // do not change this line
int sumLbs = this.getPounds() + other.getPounds(); // do change these lines
int sumOzs = this.getOunces() + other.getOunces(); // *
if (sumOzs >= 16){ // *
sumOzs = sumOzs - 16; // *
sumLbs = sumLbs + 1; // *
} // *
return new Weight(sumLbs, sumOzs); // *
}
}
public class Weight{
int totalOunces;
public Weight(int pounds, int ounces){ // do not change this line
this.totalOunces = (pounds * 16) + ounces;
}
public int getPounds( ){ // do not change this line
return this.totalOunces/16;
}
public int getOunces( ){ // do not change this line
return this.totalOunces - (this.getPounds() * 16);
}
public int getTotalOunces( ){ // do not change this line
return this.totalOunces;
}
public Weight sum(Weight other){ // do not change this line
int sumLbs = this.getPounds() + other.getPounds(); // do change these lines
int sumOzs = this.getOunces() + other.getOunces(); // *
if (sumOzs >= 16){ // *
sumOzs = sumOzs - 16; // *
sumLbs = sumLbs + 1; // *
} // *
return new Weight(sumLbs, sumOzs); // *
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.