CS 160 Lab 3

Tuesday, February 1, 2005


Turn in your outlabs due today.

Today's lab is from the lectures I've given Friday and Monday, as well as chapters in the book. Chapter 3, and part of chapter 4 will be referenced, but mostly the stuff I said in class. Some of this you won't be able to complete from what I've covered in class so far, it is in the book, and I will cover it before it is due next week. If you get stuck today read the book and try to figure it out. After Wed. and part of Friday's lecture I will have covered all this in lecture, it will be easier to understand if you've already tried it in your own programs.

Objectives

Learn to design and use classes that include instance variables, constructors, methods and parameters.  Learn to design and apply programs that create objects from classes and manipulate those objects via methods

To Do (more help understanding Java)

Do the following.  If at any point you have questions be sure to ask for help from the TA or the consultants.  This is your opportunity to learn.

1.  Create a project called lab3 in BlueJ.

2.  Create a class called Student in that project.

3.  Replace the BlueJ contents of Student with the code below (cut and paste):

public class Student
{
   private String studentName;
   
   public void setName(String name)
   {
       studentName =  name;
   }
   
   public String getName()
   {
       return studentName;
   }
}

4.  Create a class called StudentTester in that project.

5.  Replace the BlueJ contents of StudentTester with the code below (cut and paste):

public class StudentTester
{
   public static void main(String[] args)
   {
       Student student1 = new Student();
       student1.setName("Jack");
       System.out.println(student1.getName());
   }
}

6.  Compile and run this program.

7.  Modify the class Student to have a constructor of the form

Student()

This constructor is to do nothing (it is the same as the default file). You may want to refer to your book to remind yourself of how to build a constructor.  Rerun the program.  You should get the same results as before

8.  Add a second constructor to class Student of the form

Student(String name)

This constructor is to create an object with the studentName instance field set to the name supplied by the call to the constructor.  Add statements to your main method in StudentTester to construct a new object called student2 with the name "Mary".  Use the constructor to set the name, not method setName.  Do not delete the statements already in main.  Compile and run the program.  You should see Jack and then Mary printed in the terminal window.

9. Draw a picture of memory after the code in StudentTester is executed. 10.  Show the results to the TA or one of the consultants.

Make sure your TA, or consultant checks off what you have done before you leave.

More Adavanced

Last 11..  Do the following series of modifications for this part (This will all be due next week, it is the new part of the outlab due next week):

10.  Show the results to the TA or one of the consultants.

Make sure your TA, or consultant checks off what you have done before you leave.


Outlab

Have all this completed by next week.