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.
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"); } }
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()); } }
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.
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.