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

Introduction In this lab you will change a simple class so that it uses a differ

ID: 3867061 • 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


/**
*
* @author Sam
*/
public class Weight{

    int totalOunces; // the one line: int totalOunces
    // then fix the rest of the class

    public Weight(int pounds, int ounces){ // not changed
        totalOunces = pounds * 16 + ounces;
    }
  
    public int getPounds( ){ // not changed
        return totalOunces/16;
    }

    public int getOunces( ){ // not changed
        return totalOunces%16;
    }

    public int getTotalOunces( ){ // not changed
        return totalOunces;
    }

    public Weight sum(Weight other){ // not changed
        int sumLbs = this.getPounds() + other.getPounds();
        int sumOzs = this.getOunces() + other.getOunces();
        if (sumOzs >= 16){
            sumOzs = sumOzs - 16;
            sumLbs = sumLbs + 1;
        }
        return new Weight(sumLbs, sumOzs);
    }
}

This should do the trick for you!! Like it?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote