DOCX

advertisement
CSE 1102 – Fall 2009
Lab Assignment 1
Objectives:
When you complete this assignment you will be able to:
• Edit and execute a program using BlueJ.
Set-up Procedure:
The following procedure explains how to set up BlueJ with Wheels on a lab machine. The
Wheels library will be used for future labs so you may refer to these instructions to configure
BlueJ to include this library.
1
Log onto a lab machine.
2
Create a directory for saving your course work in your account on the Z drive.
3
Copy wheels.zip, the zip file for the Wheels package, into that directory. Do not unzip it.
The wheels.zip file is available in several places. It is on the CD that comes with your textbook
and it is available at the textbook website,
http://wps.aw.com/aw_sanders_oopjava_1/36/9390/2403930.cw/index.html.
4
Start BlueJ, which is located in the Programs/Applications folder on the lab computers.
5
Open Preferences… under the Tools menu.
6
Select the Libraries tab.
7
Click the Add button.
8
Find the wheels.zip file in the Select directory or jar/zip file window and click Open.
9
At this point, the wheels.zip file should be shown under User Libraries. Click OK.
10
Quit BlueJ and restart it for this to take effect.
To install BlueJ on another computer, download the Java SE Development Kit (JDK) from
http://java.sun.com/javase/downloads/index.jsp. The current version as of 6/1/09 is JDK 6
Update 14. Then download BlueJ from http://www.bluej.org/download/download.html. The
rest of the set-up procedure is the same as above.
Assignment:
In this assignment you will create and run the program “Lab1.java”. The text of the
program is below.
First, you must create a project for the program. In BlueJ select Project and then New
Project…. Give the project the name Lab 1 and click Create. An empty project appears with a
document.
Click on New Class, name the file “Lab1”, double-click on the box labeled “Lab1” to open the
editor, and enter the program. Do the same for “TempConverter.”
Click on the Compile button to compile your program. After it compiles successfully, click on
the Close button. To run the program, right-click on the “Lab1” box, select void main (String []
args), and then click on OK. The program runs in another window, which probably opens in the
background. You can move the program to the foreground by clicking the Java icon button (a
steaming coffee cup) at the bottom of your screen. It should look similar to this:
Congratulations! You have successfully run your first Java program. Now let’s make some
changes to the program.
1. Add two accessor methods to the TempConverter class which return the last
Fahrenheit temperature and last Celsius temperature which were converted.
2. Add instructions to Lab1.java which use your new accessor method to get the last
temperature converted to Fahrenheit.
3. Add instructions Lab1.java which convert a second Celsius temperature to
Fahrenheit.
4. Add instructions to Lab1.java which compute and print the difference between the
two temperatures which were converted to Fahrenheit by the TempCoverter.
5. Repeat steps 2-4 for Celsius temperatures.
TempConverter.java
public class TempConverter {
double _lastFTemp;
double _lastCTemp;
public TempConverter(){
_lastFTemp = 0.0;
_lastCTemp = 0.0;
}
public double getFahrenheit(double celsius){
double fahrenheit;
_lastCTemp = celsius;
fahrenheit = (celsius * 1.8)+ 32.0;
return fahrenheit;
}
public double getCelsius(double fahrenheit){
double celsius;
_lastFTemp = fahrenheit;
celsius = (fahrenheit - 32.0)/1.8;
return celsius;
}
}
Lab1.java
public class Lab1 {
TempConverter _converter;
public Lab1(){
double ctemp, ftemp;
_converter = new TempConverter();
ctemp = 100.0;
ftemp = _converter.getFahrenheit(ctemp);
System.out.println(ctemp + " degrees Celsius = " + ftemp + "
degrees Fahrenheit.");
ftemp = 100.0;
ctemp = _converter.getCelsius(ftemp);
System.out.println(ftemp + " degrees Fahrenheit = " + ctemp + "
degrees Celsius.");
}
public static void main(String[] args){
Lab1 myLab1 = new Lab1();
}
}
Download