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." Add the class /Accounts/courses/cs117/dmusican/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." Add the class /Accounts/courses/cs117/dmusican/StudentTester.java.

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

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

  17. private String firstName;

    to say

    public String firstName;

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

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

  20. Add a method to the class Student called summarize that prints out a pretty display of all the information about a particular student.
  21. Modify the test method in StudentTester 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. For each student object that you create, call the summarize method so that the information for all three objects is displayed to the screen..

Lab #2: Creating a "Car".

1. In a new project, create a class called Car. Within this class, you should have data members that represent year manufactured, miles driven, manufacturer name, and model name. You should provide mutator methods for updating these data values, namely:
void setYearManufactured(int year);
void setMilesDriven(int miles);
void setManufacturerName(String name);
void setModelName(String name);
You should also provide accessor methods for obtaining these data values, namely:
int getYearManufactured();
int getMilesDriven();
String getManufacturerName();
String getModelName();
Make appropriate use of the private and public visibility modifiers, and make sure to include a constructor.

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

3. It turns out that a car might be sold to a recent immigrant from Canada. Therefore, you want to be able to report the distance driven in kilometers instead of miles. Add a method called "getKilometersDriven" that returns the distance driven in kilometers instead of miles.  (Useful information: 1 mile = 1.6094 kilometers.)

What to turn in

Turn in both your Car class as well as another class called CarTester. CarTester should have a test method that asks the user for year manufactured, miles driven, manufacturer name, and model name. It should store this information appropriately in a Car object, then output the information back in a relevant manner for a Canadian customer.