CS100M Lab 10

November 1-2, 2004



1. Defining Classes and Creating Objects

In this lab, you will learn about objects and classes. In particular, we will experiment with private and public methods and fields. We will define a Student class, and then define another class StudentGroup that depends on Student. First, let us create a class that represents a student. Create a file called Student.java in DrJava and type in the following code:

/* A Student has a name and a test score */

public class Student {

   public String name;  //we'll change the visibility modifier later

   public int testscore;

}

Now save the file and compile it. Then create another file named Lab10.java and put the following code in it:

public class Lab10 {

   public static void main(String[] args) {

      Student john= new Student();

      john.name= "John Doe";

      john.testscore= 55;

      System.out.println(john.name + " got a score of " + 

                         john.testscore + " on the exam.");

   }

}

Run the program and write down the screen output below.

ANS:



2. Protecting Fields

When we created the Student class, we made the two fields, name and testscore, public. This means that code outside of the Student class can freely modify those fields. In many cases, this is not a desirable result. For example, we may want to make sure that testscore always contains an integer between 0 and 100. Thus, we don't want code outside of the Student class to be able to set the testscore field to some invalid number, for example, -5. One way to do this is to make use of getters and setters (methods). Setters allow us to enforce certain rules by adding validity checking code before changing the fields.

Now, modify the Student class by making testscore a private field, and create a getter and a setter for it. Call them getTestscore and setTestscore respectively. In setTestscore, make sure that the passed argument is a valid value. If the argument is not in the acceptable range, simply set testscore to 0. Test your program by modifying the main method of class Lab10:

public class Lab10 {

   public static void main(String args[]) {

      Student john= new Student();

      john.name= "John Doe";

      john.setTestscore(55);

      System.out.println(john.name + " got a score of " + 

                         john.getTestscore() + " on the exam.");

      john.setTestscore(-5);

      System.out.println(john.name + " got a score of " + 

                         john.getTestscore() + " on the exam.");



   }

}

Run the program and write down the screen output below.

ANS:



Now, modify the Student class to include a getter and a setter for the name field as well. Name the methods getName and setName respectively. Make the corresponding changes to class Lab10 and test your program.



3. ToString

We've used the println method to print primitive variable values before. Let's see what gets printed when you print the value of a reference variable. Add the following print statement to method main of Lab10.java:

      System.out.println(john);

Now run the program. The last thing printed is the reference ID of the object! This isn't the most useful information for us, so let's write a toString method in class Student to return the name and score of the student as a String. Then run Lab10 again. Now the statement System.out.println(john); uses the toString method that you have just defined to print the field values of the object instead of the reference ID of the object.


4. Constructor

Let's define a constructor for class Student:

   /* This Student has name s */

   public Student(String s) {

      setName(s);

   }

Now try to run your program again. This causes an error because the statements to create a Student object do not match the specification of the constructor. Change the code in Lab10 to use the above constructor to create the object and set the name. Now the program should run without error.

5. A Larger Class

Now that we have defined the Student class, we can make use of the Student class to build larger classes. Create a file called StudentGroup.java and put the following code in it:

/* A StudentGroup is made up of 3 Students */

public class StudentGroup {

   private Student student1;

   private Student student2;

   private Student student3;



   /* This StudentGroup has 3 Students with names s1, s2, and s3 */ 

   public StudentGroup(String s1, String s2, String s3) {

      student1= new Student(s1);

      student2= new Student(s2);

      student3= new Student(s3); 

   }



   /* Getter methods for the 3 Students in this StudentGroup */

   public Student getStudent1() { return student1; }

   public Student getStudent2() { return student2; }

   public Student getStudent3() { return student3; }



   /* = String description of this StudentGroup */

   public String toString() {

      // Replace this comment with code to return the names and scores of

      // the students.  Hint: This method should call the toString method

      // of the Student objects.

   }

}

Now test your StudentGroup class! Create a new java file called Lab10b.java and put a main method in it. In the main method, write statements to do the following:

Delete your files from the computer! (But you may want to email them to yourself for studying.)