CS 117: Lab on Instantiable Classes

Lab #1: Creating a "student".

  1. Create a new BlueJ project called student. From the "Edit" menu, choose "Add Class From File." Go to the "T" disk drive at the top, then go into the "javalibs" folder, then go into the "dmusican" folder. Double click on the file named Student.java.

  2. Look at the Java source code for Student.java. What does this class do?

  3. In the top right corner of the window, click on the bar that says "Implementation". On the drop down box that appears, click "Interface". BlueJ will then automatically generate the javadoc for this class. What information does it contain? Scroll through it and look at it. Notice the detailed information it contains on author, version, and documentation for each method. How does BlueJ figure all this out? What purpose does a javadoc serve?

  4. Click on the "Interface" bar to change it back to "Implementation".

  5. Compile the Student class.

  6. Create an instance of the Student class using the right click menu and the new Student() option. Then inspect the object to see the values of its attributes.

  7. Right click the instance and use the appropriate methods to see what its idNumber, firstName, familyName and age are.

  8. Use the appropriate method of the object to change its firstName.  Don't forget to put the double quotes on the String. Inspect it to see if it worked.

  9. Use the appropriate method of the object to change its familyName.   Inspect it to see if it worked.

  10. Use the appropriate method of the object to change its age.   Inspect it to see if it worked.

  11. Create another Student object using the right click menu and the

    new Student(anIdNumber,aFirstName,aFamilyName,anAge)

    option.   Give the attributes some values as the object is created. Inspect the object to see if it worked.

  12. Create another object using the same option. See what happens when you key in the parameters incorrectly (e.g. key in the first name or the family name without quotes, or the age with quotes).

  13. Change the familyName of this object. Then use the getFamilyName behaviour to find out what its family name is now.

  14. From the "Edit" menu, choose "Add Class From File." Go to the "T" disk drive at the top, then go into the "javalibs" folder, then go into the "dmusican" folder. Click on the file named StudentInfo.java.

  15. Try to compile StudentInfo.java. What kind of error do you get?

  16. Go into Student.java, and change the line that says

    private String firstName;

    to say

    public String firstName;

  17. Recompile Student, then recompile StudentInfo. StudentInfo now compiles successfully. Why? What is the difference between a private and a public data value?

  18. Change firstName back to private in Student. Recompile.

  19. Modify the main method in StudentInfo so that it asks a user for an idNumber, firstName, familyName, and age for three different students, each of which gets stored in an individual student object. Output a summary of each student back to the user.

Lab #2: Creating a "Mars Probe".

Overview

In September of 1999, NASA lost a Mars probe because it had been improperly programmed. The coordinates were accidentally given to it in the English system instead of in the metric system. For this lab, you will create a class to represent better Mars probes than the one we lost.

Details

In a new project, you should create a class named MarsProbe. Within this class, you should have data members which represent destination coordinates (in kilometers), but the class should provide two different methods for updating the coordinates:

void updateMetric(double x, double y, double z);
void updateEnglish(double x, double y, double z);

updateMetric will take new coordinates in kilometers, and store it directly. updateEnglish will take new coordinates in miles, and convert it to kilometers before storing it. (Useful information: 1 mile = 1.6094 kilometers.)

You should also include methods for obtaining the coordinates:

double getXCoord();

double getYCoord();

double getZCoord();

Make appropriate use of the private and public visibility modifiers, and make sure to include a constructor. You should make the 1.6094 conversion factor a private data value in the MarsProbe class. Why? What would be the ramifications if you made it public?

Check the javadoc that BlueJ generates for your class. Add comments to your program appropriately so that the javadoc contains useful information.

What to turn in

Turn in both your MarsProbe class as well as another class called LaunchProbe. LaunchProbe should have a main method that asks the user for X, Y, Z coordinates in English units, stores them appropriately in a MarsProbe object, then outputs the coordinates back to the user in kilometers.