Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- //===================================================
- /**
- The FeetInches class holds distances measured in
- feet and inches.
- */
- class FeetInches
- {
- // private int feet; // The number of feet
- // private int inches; // The number of inches
- private int allInches;
- //------------------------------------------------
- /**
- This constructor assigns 0 to the feet
- and inches fields.
- */
- public FeetInches()
- {
- this.allInches = 0;
- }
- //------------------------------------------------
- /**
- This constructor accepts two arguments which
- are assigned to the feet and inches fields.
- The simplify method is then called.
- @param f The value to assign to feet.
- @param i The value to assign to inches.
- */
- //------------------------------------------------
- public FeetInches(int f, int i)
- {
- this.allInches = f * 12 + i;
- simplify();
- }
- //------------------------------------------------
- /**
- The following is a copy constructor. It accepts a
- reference to another FeetInches object. The feet
- and inches fields are set to the same values as
- those in the argument object.
- @param object2 The object to copy.
- */
- public FeetInches(FeetInches object2)
- {
- return FeetInches(object2.getFeet(), object2.getInches());
- }
- //------------------------------------------------
- /**
- The simplify method adjusts the values
- in feet and inches to conform to a
- standard measurement.
- */
- private void simplify()
- {
- // We dont need to do this at all anymore
- }
- //------------------------------------------------
- /**
- The setFeet method assigns a value to
- the feet field.
- @param f The value to assign to feet.
- */
- public void setFeet(int f)
- {
- this.allInches = f * 12 + this.getInches();
- }
- //------------------------------------------------
- /**
- The setInches method assigns a value to
- the inches field.
- @param i The value to assign to inches.
- */
- public void setInches(int i)
- {
- this.allInches = this.getFeet() * 12 + i;
- }
- //------------------------------------------------
- /**
- getFeet method
- @return The value in the feet field.
- */
- public int getFeet()
- {
- int feet = Math.floor(allInches / 12);
- }
- //------------------------------------------------
- /**
- getInches method
- @return The value in the inches field.
- */
- public int getInches()
- {
- int inches = Math.remainder(allInches / 12);
- }
- //------------------------------------------------
- /**
- toString method
- @return a reference to a String stating
- the feet and inches.
- */
- public String toString()
- {
- return this.getFeet() + " feet " +
- this.getInches() + " inches";
- }
- //------------------------------------------------
- /**
- The add method returns a FeetInches object
- that holds the sum of this object and another
- FeetInches object.
- @param object2 The other FeetInches object.
- @return A reference to a FeetInches object.
- */
- public FeetInches add(FeetInches object2)
- {
- int totalFeet, // To hold the sum of feet
- totalInches; // To hold the sum of inches
- totalFeet = this.getFeet() + object2.getFeet();
- totalInches = this.getInches() + object2.getInches();
- return new FeetInches(totalFeet, totalInches);
- }
- //------------------------------------------------
- /**
- The equals method compares this object to the
- argument object. If both have the same values,
- the method returns true.
- @return true if the objects are equal, false
- otherwise.
- */
- public boolean equals(FeetInches object2)
- {
- if (this.getFeet() == object2.getFeet() &&
- this.getInches() == object2.getInches())
- return true;
- return false;
- }
- //------------------------------------------------
- /**
- The copy method makes a copy of the
- the calling object.
- @return A reference to the copy.
- */
- public FeetInches copy()
- {
- // Make a new FeetInches object and
- // initialize it with the same data
- // as the calling object.
- // Return a reference to the new object.
- return new FeetInches(this.getFeet(), this.getInches());
- }
- }
- //====================================================================
- /* Name of the class has to be "Main" only if the class is public. */
- public class FeetInchesTest
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- FeetInches f = new FeetInches(5, 9);
- System.out.println(f);
- }
- }
- FeetInchesTest.txt
- Displaying FeetInchesTest.txt.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement