Advanced Programming - CS239

 

LAB 7: DEVELOPING A LENGTH CLASS – to be continued

 

Getting Ready: Before going any further you should:

  1. Make a directory on your N: drive for this lab or on your floppy or flash drive.  (Note: You will need to use this class in future labs.)
  2. Set up your development environment.

 

Part I: This part of the lab is concerned with "stubbing-out" a

Length class and creating a driver for testing that class. The driver below can be used to test your Length class.

 

  1. Download  the Driver.java program from the class web page or copy and paste it into your environment from the boxes below.

 

* A driver that can be used to test the Length class

*

* @author Prof. David Bernstein, James Madison University

* @version 1.0

* @author Professor Elizabeth Adams, James Madison University

* @version 1.1

* @author Professor Nancy Harris, James Madison University

* @version 1.2

*/

 

public class Driver

{

   /**

   * The entry point of the application

   *

   * @param args The command line arguments

   */

 

   public static void main(String[] args)

      {

      Length billsHeight, growth, mikesHeight;

 

            // test 2 paramter constructor

      mikesHeight = new Length(5, 5);

      billsHeight = new Length(5, 5);

 

            // test toString

      System.out.println("Age 12: ");

      printSummary("Mike", mikesHeight, "Bill", billsHeight);

      

            // get ready to test changeLength

      growth = new Length();

      mikesHeight.changeBy(growth);

       

            //change length for both persons

      growth = new Length(0, 4);

      billsHeight.changeBy(growth);

      System.out.println("Age 16: ");

      printSummary("Mike", mikesHeight, "Bill", billsHeight);

 

            // and again

      growth = new Length(1, 1);

      mikesHeight.changeBy(growth);

      growth = new Length(0,10);

      billsHeight.changeBy(growth);

      System.out.println("Age 20: ");

      printSummary("Mike", mikesHeight, "Bill", billsHeight);

      

            // test three parameter constructor (they begin to lose height)

      growth = new Length(0,1,false);

      billsHeight.changeBy(growth);

      System.out.println("Age 70: ");

      printSummary("Mike", mikesHeight, "Bill", billsHeight);

   }

 

   // Normally we would want the main method to be the only thing in a class.

   // However, this is simply a test class.  So the printSummary which

   // supports the testing will be permitted here. 

   // This Driver will not be used when Length is fully tested.

  

   /**

   * Print summary information for two Length objects

   *

   * @param name1 The name for the first Length

   * @param length1 The first Length object

   * @param name2 The name for the second Length

   * @param length2 The second Length object

   */

 

   private static void printSummary(String name1, Length length1,

                                    String name2, Length length2)

      {

      System.out.println(" " + name1 + ": " + length1); // Uses toString

      System.out.println(" " + name2 + ": " + length2); // Uses toString

      

            // tests the equals length and uses its result to print a message.    

            if (length1.equals(length2))

      {

         System.out.println(" == SAME ==");

      }

      else

      {

            System.out.println(" == DIFFERENT ==");

      }

            System.out.println("\n");

      }

}

 

 

 

 

 

 

This Driver.java  program is strictly used to test your Length class methods and is not to be altered in Part 1. 

 

  1. Stub-out a class named Length.

Stubbing out means that you will provide attributes and methods.  For each method, you will provide the header, an appropriate return statement that will return a value of the required type and all documentation to describe the operation of each method.  No actual code (other than the attribute declarations and headers) should be included in the stub.  You will use the stubbed-out class to begin your development step.

  

The Length class stubs must contain:

    1. Three int attributes that will hold the number of feet in the Length, the number of inches in the Length, and the sign of the Length.
    2. An explicit value constructor that is passed the number of feet in the Length, the number of inches in the Length, and the sign of the Length as a boolean.  true = positive, false = negative.  (3 parameter constructor)
    3. An explicit value constructor that is passed the number of feet in the Length, and the number of inches in the Length. This constructor will always make the sign positive.  (2 parameter constructor)
    4. A default constructor. This constructor will always make the feet and inches 0 and the sign positive.
    5. A public changeBy method that returns nothing and is passed a Length. This method will change this Length (i.e., the calling object, Length) by the Length that is passed in.
    6. A public equals method that returns a boolean value and is passed a Length. This method will check to see if this Length (i.e., the calling object, Length) has the same value(s) as the Length that is passed in.
    7. A public toString method that returns a String representation of the this Length (i.e., the calling object, Length) and is not passed anything.
    8. A private toInches method that returns an int and is not passed anything. This method returns the equivalent number of inches in the "calling" Length. For example, this method must return 17 for a Length of 1 ft. 5 in., positive sign.

 

Note: The number of inches and feet in a Length must always be non-negative.  In addition, the number of inches must never be 12 or greater.  You should include code or comments for this wherever a length value is changed.

 

  1. Compile and execute (after fixing any syntax errors) the driver. What was output?

 

 

 

Stop!!!

Print out your Length.java program and turn it in BEFORE CONTUING ON WITH THIS LAB.  You will continue with this same program, for the later steps, but be sure to turn in this initial step first.  This must be completed and submitted before the end of the lab.

 

 

NOTE:  Your Driver.java program should only have in it method headers , attribute declarations, javadocs,  internal comments (i.e. the elements described above).

 

 

 

Get Professor Adams to initial this box before continuing with the steps on the next page.

 

 

 

 


 

Part II: This part of the lab is concerned with developing and testing the Length class.

  1. Complete the three-parameter version of the explicit value constructor.

 

  1. Complete the toString method.

 

  1. Compile and execute (after fixing any syntax errors) the driver. What was output?

 

 

 

  1. Complete the two -parameter version of the explicit value constructor. To avoid "code duplication", this version must call the three-parameter version.

 

  1. Complete the default constructor. To avoid "code duplication", this version must call the two -parameter version.

 

  1. Compile and execute (after fixing any syntax errors) the driver. What was output?

 

 

 

Steps 1-6 of Part 2  must be turned in by Tuesday Feb 8 but you may turn it all in.

 

  1. Complete and test the toInches method.

 

  1. Compile and execute (after fixing any syntax errors) the driver. What was output?

 

 

 

  1. Complete the changeBy method. This method must use the toInches method.

 

  1. Complete the equals method.

 

  1. Compile and execute (after fixing any syntax errors) the driver. What was output?