CS Labs: Lab 5

CS Labs: Lab 5


Class, Instance, and Local Variables, Oh My!

Read all of these instructions before beginning the exercises. There are 9 checkpoints , plus the clean-up checkpoint, in this lab. You and your partner should work together using just one of your accounts. CHANGE WHO IS CONTROLLING THE COMPUTER AFTER EACH CHECKPOINT! If you need help with any exercise, raise your hand.

Copy the lab materials to your account. You will need to use the -r option on the Unix cp command in order to copy not only files, but directories. To copy, enter

             cp -r /home/Classes/Cs1/Labs/Lab05 .
     
Don't forget the dot . at the end of the command.After running this copy command, you will see a new directory in your account called Lab05. What you copied was actually a directory, plus all of its contents. Everything you need for the lab exercises today is contained in this new directory.

Change directories into Lab05 and start BlueJ by entering bluej&.

For the first exercises in this lab, you and your partner will need some paper and a pen or pencil.

Class, Instance and Local Variables

The next two BlueJ classes that you will work with contain the main() method within the class definition rather than in a separate class of its own. You may remember this technique from the last question in Lab #4. As you know, this is perfectly legal and quite useful in Java. This technique is frequently used when a programmer wants to exclusively test items in the class definition, and won't be accessing other classes that are not already built-in or imported.

Review the Foo class below and work through the main() method that you find at its end. With your partner, predict what you think the output will look like.

public class Foo
{
    private static int xx;
    private int yy;

    public Foo ()
    {
         xx = 2;
         yy = 3;
    }

    public Foo (int first, int second) 
    {
         xx = first;
         yy = second;
    }

    public void init () 
    {
        System.out.println( "xx = " + xx);
        System.out.println( "yy = " + yy);
    }

    public static void main ( String [] args )
    {
       Foo foo1 = new Foo();
       Foo foo2 = new Foo(7, 17);

       foo1.init();
       foo2.init();

       System.out.println( "done");
    }
}
1 Show us your predicted output.

In BlueJ, open the project Puzzle1 (the program for which you just predicted output) and run it. Were you right? If so, we checked you off on the next checkpoint already, otherwise...

2 Figure out where your logic went wrong, then call one of us over and explain your error(s) to us.

Close project Puzzle1.

Work through the following program with your partner and predict the output.

public class Strange
{
   private int x;

   public Strange ()
   {
      x = 34;
   }

   public void doSomething (int x)
   {
      System.out.println (" I was passed " + x);
      x = x + 1;
      System.out.println (" Now I have changed it to " + x);
   }

   public int getStrangeValue ()
   {
      return x;
   }

   public static void main ( String [] args )
   {
       int x = 6;
       Strange s1 = new Strange();
       Strange s2 = new Strange();

       s1.doSomething(10);
       s2.doSomething(20);

       System.out.println( "s1 has a strange value of " + s1.getStrangeValue());
       System.out.println( "s2 has a strange value of " + s2.getStrangeValue());
   }
}
3 Show us your predicted output.

Open project Puzzle2 (the program for which you just predicted output) and run it. Were you right? If so, we checked you off on the next checkpoint already, otherwise...

4 Tell us what part of the code mislead you, and explain the output of the program.

Close project Puzzle2.

Implementing and Testing Step by Step

Open project Thermometer1. Review the Thermometer class definition. You could easily add a main() method at the bottom of this class to print out the values of the constants listed, but we want you to demonstrate that you know how to print these values from outside the Thermometer class.

At this time, create a new class in the Thermometer1 project, and name it Demo1. (To add a new class, click the New Class button in the main BlueJ window.) Within Demo1, write a main() method that will print out the value of each class constant that you see in the Thermometer class.

5 Show us your finished program.

Close project Thermometer1 and open project Thermometer2. Edit the implementation of Thermometer and complete the implementation of the class methods convertFahrenheitToCelsius() and convertCelsiusToFahrenheit(). The following formula (where F = degrees Fahrenheit and C = degrees Celsius) shows how the two units are related:

             9
       F  =  - C  + 32
             5

Test your methods by running the Demo2 class, which is also in this project. Note that the Demo2 class does not instantiate a Thermometer object. It doesn't need to - why not?

6 Show us how you implemented the two temperature conversion methods, and run the program for us. Also, tell us why the Demo2 class did not need to create a Thermometer object to run the methods.

Close project Thermometer2.

Open project Thermometer3. Now that we have completed the class methods, it's time to write constructors so that we can instantiate Thermometer objects. We want you to write 2 constructors. The first (with no parameters) should set the initial temperature to 0 Celsius. The second should have one parameter which holds the value (given in Celsius) that the initial temperature should take.

Remember that you can test each constructor and view the instance variable values by using the Inspect option on instantiated objects, as you saw in lab last week.

7 Show us how you implemented the two constructors.

For the next checkpoint, you will finish the implementation of two methods: setTemperature() and setTemperatureUsingFahrenheit(). setTemperature() uses the value passed to it (through the parameter degreesInCelsius) to set the temperature of the thermometer. setTemperatureUsingFahrenheit() uses the Fahrenheit temperature value passed to it to set the thermometer to its equivalent in Celsius. Make your implementation task easier by calling another already-implemented method available within the Thermometer class.

8 Show us how you implemented setTemperature() and setTemperatureUsingFahrenheit().

Finally, complete the implementation of getTemperature() and getTemperatureInFahrenheit(). Again, do not copy any formulas to convert to Fahrenheit. Which already written method can you call to do that part of the job for you?

You are now ready to compile and run the Thermometer class with the Demo3 class.

9 Show us a run of Demo3.

After the Lab

Don't forget to exit Netscape before you log out.

10 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.

End of Lab


Author: Susan Haller, University of Wisconsin-Parkside
Acknowledgment: I would like to thank Erica Eddy and Lester I. McCann, my colleagues at the University of Wisconsin-Parkside, for their corrections, updates, and revisions to this series of labs.