* a Weight Class That Uses English Measure

* a Weight Class That Uses English Measure

<p>/** * A weight class that uses "English measure" * (i.e., pounds and ounces) * * @version 4.0 * @author CS139, James Madison University */ public class Weight { // These attributes are essential to the definition of a Weight private int ounces, pounds, sign;</p><p>/** * Explicit Value Constructor * * @param pounds The number of pounds (must be positive) * @param ounces The number of ounces (must be positive) * @param positive true for a postive Weight, false for negative */ public Weight(int pounds, int ounces, boolean positive) { this.pounds = Math.abs(pounds); this.ounces = Math.abs(ounces); this.sign = 1; if (!positive) this.sign = -1; }</p><p>I/** * Change this Weight by a given amount * * @param other The amount to change this Weight by */ public void changeBy(Weight other) { int otherWeightInOunces, thisWeightInOunces, total; thisWeightInOunces = this.toOunces(); otherWeightInOunces = other.toOunces(); total = thisWeightInOunces + otherWeightInOunces; pounds = total / 16; ounces = total % 16; }</p><p>/** * Check to see if this Weight is equal to * a given Weight * * @param other The other Weight to use in the comparison * @return true if the two are equal and false otherwise */ public boolean equals(Weight other) { boolean comparison; int otherWeightInOunces, thisWeightInOunces; thisWeightInOunces = this.toOunces(); otherWeightInOunces = other.toOunces(); comparison = false; if (thisWeightInOunces == otherWeightInOunces) comparison = true; return comparison; }</p><p>/** * Get a String representation of this Weight * * @return The String */ public String toString() { String s, poundString; s = new String(); if (sign < 0) s += "Negative "; poundString = " lbs. "; if (pounds == 1) poundString = " lb. "; s += pounds + poundString + ounces + " oz."; return s; }</p><p>/** * Get that value of this Weight in ounces * (e.g., this method returns 17 for a Weight of 1lb. 1oz.) * * Note: This method is private because it is only used * within the class. * * @return The Weight in ounces */ private int toOunces() { int total; total = sign * (pounds*16 + ounces); return total; }</p><p>}</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us