Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5. //===================================================
  6. /**
  7.    The FeetInches class holds distances measured in
  8.    feet and inches.
  9. */
  10.  
  11. class FeetInches
  12. {
  13. //    private int feet;      // The number of feet
  14. //    private int inches;    // The number of inches
  15.     private int allInches;
  16.  
  17.    //------------------------------------------------
  18.    /**
  19.       This constructor assigns 0 to the feet
  20.       and inches fields.
  21.    */
  22.    public FeetInches()
  23.    {
  24.        this.allInches = 0;
  25.    }
  26.  
  27.    //------------------------------------------------
  28.    /**
  29.       This constructor accepts two arguments which
  30.       are assigned to the feet and inches fields.
  31.       The simplify method is then called.
  32.       @param f The value to assign to feet.
  33.       @param i The value to assign to inches.
  34.    */
  35.    //------------------------------------------------
  36.    public FeetInches(int f, int i)
  37.    {
  38.        this.allInches = f * 12 + i;
  39.       simplify();
  40.    }
  41.  
  42.    //------------------------------------------------
  43.    /**
  44.       The following is a copy constructor. It accepts a
  45.       reference to another FeetInches object. The feet
  46.       and inches fields are set to the same values as
  47.       those in the argument object.
  48.       @param object2 The object to copy.
  49.    */
  50.  
  51.    public FeetInches(FeetInches object2)
  52.    {
  53.       return FeetInches(object2.getFeet(), object2.getInches());
  54.    }
  55.  
  56.    //------------------------------------------------
  57.    /**
  58.       The simplify method adjusts the values
  59.       in feet and inches to conform to a
  60.       standard measurement.
  61.    */
  62.  
  63.    private void simplify()
  64.    {
  65.        // We dont need to do this at all anymore
  66.    }
  67.  
  68.    //------------------------------------------------
  69.    /**
  70.       The setFeet method assigns a value to
  71.       the feet field.
  72.       @param f The value to assign to feet.
  73.    */
  74.  
  75.    public void setFeet(int f)
  76.    {
  77.        this.allInches = f * 12 + this.getInches();
  78.    }
  79.  
  80.    //------------------------------------------------
  81.    /**
  82.       The setInches method assigns a value to
  83.       the inches field.
  84.       @param i The value to assign to inches.
  85.    */
  86.  
  87.    public void setInches(int i)
  88.    {
  89.        this.allInches = this.getFeet() * 12 + i;
  90.    }
  91.  
  92.    //------------------------------------------------
  93.    /**
  94.       getFeet method
  95.       @return The value in the feet field.
  96.    */
  97.  
  98.    public int getFeet()
  99.    {
  100.        int feet = Math.floor(allInches / 12);
  101.    }
  102.  
  103.    //------------------------------------------------
  104.    /**
  105.       getInches method
  106.       @return The value in the inches field.
  107.    */
  108.  
  109.    public int getInches()
  110.    {
  111.        int inches = Math.remainder(allInches / 12);
  112.    }
  113.  
  114.    //------------------------------------------------
  115.    /**
  116.       toString method
  117.       @return a reference to a String stating
  118.       the feet and inches.
  119.    */
  120.  
  121.    public String toString()
  122.    {
  123.       return this.getFeet() + " feet " +
  124.                    this.getInches() + " inches";
  125.    }
  126.  
  127.    //------------------------------------------------
  128.    /**
  129.       The add method returns a FeetInches object
  130.       that holds the sum of this object and another
  131.       FeetInches object.
  132.       @param object2 The other FeetInches object.
  133.       @return A reference to a FeetInches object.
  134.    */
  135.  
  136.    public FeetInches add(FeetInches object2)
  137.    {
  138.       int totalFeet,    // To hold the sum of feet
  139.           totalInches;  // To hold the sum of inches
  140.  
  141.       totalFeet = this.getFeet() + object2.getFeet();
  142.       totalInches = this.getInches() + object2.getInches();
  143.       return new FeetInches(totalFeet, totalInches);
  144.    }
  145.  
  146.    //------------------------------------------------
  147.    /**
  148.       The equals method compares this object to the
  149.       argument object. If both have the same values,
  150.       the method returns true.
  151.       @return true if the objects are equal, false
  152.       otherwise.
  153.    */
  154.  
  155.    public boolean equals(FeetInches object2)
  156.    {
  157.       if (this.getFeet() == object2.getFeet() &&
  158.                this.getInches() == object2.getInches())
  159.             return true;
  160.  
  161.     return false;
  162.    }
  163.  
  164.    //------------------------------------------------
  165.    /**
  166.       The copy method makes a copy of the
  167.       the calling object.
  168.       @return A reference to the copy.
  169.    */
  170.  
  171.    public FeetInches copy()
  172.    {
  173.       // Make a new FeetInches object and
  174.       // initialize it with the same data
  175.       // as the calling object.
  176.  
  177.       // Return a reference to the new object.
  178.       return new FeetInches(this.getFeet(), this.getInches());
  179.    }
  180. }
  181.  
  182. //====================================================================
  183. /* Name of the class has to be "Main" only if the class is public. */
  184. public class FeetInchesTest
  185. {
  186.     public static void main (String[] args) throws java.lang.Exception
  187.     {
  188.         FeetInches f = new FeetInches(5, 9);
  189.         System.out.println(f);
  190.     }
  191. }
  192. FeetInchesTest.txt
  193. Displaying FeetInchesTest.txt.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement