CS 151 Fall 2007 Lab 3:

Creating Classes: Constructors, Methods, Instance Fields

The goal for this lab is to gain more experience creating classes, specifying their interfaces, and implementing those interfaces.  We will also see how BlueJ can help us interactively "unit test" our classes.

 

  1. Open BlueJ either by selecting it from the Dock or navigating to it in the Applications folder and double clicking it.

  2. Mount your network directory/folder so that you can access the lab after you leave Olin 323.  Go to the Finder and from the Go menu select Connect to Server....  Type or choose afp://fileserver1 and enter your password if prompted.  Select the Personal volume.

  3. From the Project menu select New Project.  Select your computer from the drop down menu.  Then scroll down and open the Volumes folder and open Personal inside it.  You should see a folder with your login name.  Create the new project in that directory and call it "Lab3".

  4. Create a class called "Automobile" and remove the default method and instance variable BlueJ creates.  Decide whether you want to work in English or Metric units.  The choice is miles and gallons versus kilometers and liters.  Be sure to document your choice so people who use your class know which you chose.

 

  1. Add instance variables called fuelLevel and fuelEfficiency.  They should both be private and of type double. Precede each with a comment stating what it stores.  You don't need to initialize instance variables since we do that in the class' constructors.  For example,
                // the amount of fuel in gallons
                private double fuelLevel;

                // the number of miles per gallon of fuel the car gets
                private double fuelEfficiency;


  2. Add a constructor that takes a single parameter of type double, sets the fuelEfficiency to that value, and sets the fuelLevel to 0.  Remember no return type is needed for constructors.  They return a new object instance of the class.
                /**

       * Constructor initializing the car fuel efficiency

       * @param efficiency the car's fuel efficiency

        **/

        public Automobile (double efficiency)

{

             fuelEfficiency=efficiency;

             //This car starts with no fuel

             fuelLevel=0;

        }

 

  1. Add a constructor that takes two parameters, both doubles.  The first specifies is the efficiency like the previous constructor, and the second the initial amount of gas.
         /**

       * Constructor initializing fuel efficiency and fuel level to specified values

       * @param efficiency the car's fuel efficiency

        * @param amount the initial amount of fuel

        **/

        public Automobile (double efficiency, double amount)

{

             fuelEfficiency = efficiency;

             //This car starts with specified amount of fuel

             fuelLevel = amount;

        }

 

  1. Compile and fix any errors. 

 

  1. Try creating objects with both constructors.  Control-click the class in the BlueJ project window.  Select the constructor version, enter the requested parameter(s) when prompted.  You can keep the default (variable) name for the new object or change it if you like.  When you are done click OK.   A new red box will appear in the bottom of the project window.  Double click the newly created red object to inspect its instance fields.

 

  1. Create a public method called drive that take one parameter, a double, returns void, and drives the car that many miles.  Assume the car has enough fuel to drive the specified distance.
                /**

       Adds the specified amount of fuel to the car.

       @param distance (in miles) to drive the car

        **/

            

public void drive (double distance)

          {

               fuelLevel = fuelLevel – distance/fuelEfficiency;

          }

 

  1. Compile and fix any errors.  Test your drive method.  Create a new object by control clicking on Automobile, choosing one of the constructors, and supplying the needed parameters.  Inspect the fuel level of your object by double clicking it.  Execute the drive method by control clicking the red object, choosing drive, and entering the distance.  Inspect your car (the red object) again to see that the correct amount of fuel was used.

 

  1. Create a public method called addFuel that takes one parameter, a double, and returns void.  It should add the specified amount of gas to the car.  For example,
                /**

       Adds the specified amount of fuel to the car.

       @param fuelAmount amount of fuel to add

        **/

            

public void addFuel (double fuelAmount)

          {

               fuelLevel = fuelLevel + fuelAmount;

          }

 

  1. Compile and fix any errors.  Test your addFuel method.  Create a new object by control clicking on Automobile, choosing one of the constructors, and supplying the needed parameters.  Inspect the fuel level of your object by double clicking it.  Execute the addFuel method by control clicking the red object, choosing addFuel, and entering the fuel amount.  Inspect your car (the red object) again to see that the fuel added correctly.

 

 

  1. Add a public instance field/variable to your class called mileage.  Modify your constructors to initialize mileage to 0.  If desired add a new constructor that lets the mileage be specified.  Modify drive to add the distance to the mileage. Test your changes.

 

  1. Add public getFuelLevel, setFuelLevel, getFuelEfficiency, and setFuelEfficiency methods.  Each set method takes a single double parameter and returns nothing.  Each get method takes no parameters and returns a single double value.  For example,
            
    /**

       Gets the fuel efficiency of the car.

       @return the fuel efficiency (in miles/gallon)

        **/

            

public double getFuelEfficiency()

          {

               return fuelEfficiency;

          }


/**

       Sets the fuel efficiency of the car to the given value.

       @param the fuel efficiency (in miles/gallon)

        **/

            

public void setFuelEfficiency(double efficiency)

          {

               fuelEfficiency = efficiency;

          }

 

  1. Compile and fix any errors.

 

  1. Unit test the methods you just created.  Control click on your class in the BlueJ project window and choose one of the constructors.  Enter the parameter(s) and name the object.  Control-click the newly created red object to access its methods.  Chose the desired method and specify its parameters if it has any.  After executing a set method, double click the object to inspect its instance variables and see that your method changed the appropriate field.

 

  1. Modify addFuel and drive to use getFuelLevel and setFuelLevel instead of accessing the instance variable directly.  Test your modifications.