CIS 121 Laboratory January 27, 2000


A Couple of Additional Notes about the Programming Assignment

  • In each class definition there is a built-in instance method called toString(). The return type of this method is String. The body of the method contains a return statement in which we place the string we want returned. In order to use this method, we simply place the name of the object as an argument to System.out.println. For example, in our Room.java program we have the following.
    public class Room {
    
       ...
    
       public String toString() {
          return( ... );
       }
    
       ...
    
    }
    
    In order to use this method in our Hotel.java, we could do the following.
    public class Hotel() {
    
       ...
    
       theRoom = new Room(10,10);
    
       ...
    
       System.out.println(theRoom);
    
       ...
    
    }
    
  • In order to compare two strings for equality, use the equals method. For example, suppose you wished to compare the String boxHolder with the string "Mary", you would use the following code.

    if (boxHolder.equals("Mary"))
       ...