More Lists

Grocery List:

        
	        ______        
	       |      |
 numberOfItems |      |    MAX_SIZE  ________
               |______|
 _______________________
|                       |
|     GROCERY LIST      |
|                       |
|_______________________|
|                       |                
|_______________________|
|                       |
|_______________________|
|                       |                
|_______________________|
|                       |
|_______________________|
|                       |                
|_______________________|
|                       |
|_______________________|
|                       |                
|_______________________|








Simple Answering Machine:

The Computer Science term for this kind of list is a "stack". It is analogous to a "stack of cafeteria trays" where trays are added to the top of the stack and taken from the top of the stack.

                ______        
	       |      |
 numberOfItems |      |    MAX_SIZE  ________
               |______|
 _______________________________________________________
|                                                       |
|                    ANSWERING MACHINE                  |
|_______________________________________________________|
|                               |                       |
|           Message             |      Date / Time      |
|_______________________________|_______________________|
|                               |                       |               
|_______________________________|_______________________|
|                               |                       |
|_______________________________|_______________________|
|                               |                       |                
|_______________________________|_______________________|
|                               |                       |
|_______________________________|_______________________|
|                               |                       |                
|_______________________________|_______________________|
|                               |                       |
|_______________________________|_______________________|
|                               |                       |                
|_______________________________|_______________________|
|                               |                       |               
|_______________________________|_______________________|
|                               |                       |                
|_______________________________|_______________________|
|                               |                       |               
|_______________________________|_______________________|


Phone Message Class

import java.util.*;

 public class PhoneMessage {
 
   private String message;
   
   private Date dateTime;
   
   public PhoneMessage(String message) {
   
     this.message = message;
     dateTime = new Date();
   }
   
   public String toString() {
   
     String s = "";
     
     s += dateTime.toString() + " ";
     
     s += message;
     
     return s;
   }
}
 










Answering Machine Class

public class AnsweringMachine {

  private final int MAX_SIZE = 10;
  private int numberOfMessages;
  private PhoneMessage[] messages;
  
  public AnsweringMachine() {
    numberOfMessages = 0;
    messages = new PhoneMessage[MAX_SIZE];
  }
  
  public boolean addMessage(String message) {
  
    if (numberOfMessages < messages.length) {
      
      messages[numberOfMessages] = 
                 new PhoneMessage(message);
      
      numberOfMessages++;
      
      return true;
    }
    
    else
      
      return false;
  }
  
  public String lastMessage() {
    if (numberOfMessages > 0) {
      
      PhoneMessage pm = messages[numberOfMessages - 1];
      
      numberOfMessages--;
      
      return pm.toString();
    }
    else
      
      return "No messages";
  }
  
  public int numberOfMessages() {
    return numberOfMessages;
  }
  
  public boolean hasMessages() {
    if (numberOfMessages > 0)
      return true;
    else
      return false;
  }
  
  public String toString() {
  
    String s = "";
    for (int i = 0; i < numberOfMessages; i++)
      s += messages[i].toString() + "\n";
    return s;
  }
  
  public static void main(String[] args) {
  
    AnsweringMachine machine = new AnsweringMachine();
    
    System.out.println("Message: " + machine.lastMessage());
    
    System.out.println("\nMessages: " + machine);
    
    machine.addMessage("Call home");
    
    if(!machine.addMessage("Doctor appt. tomorrow at 9:00"))
      System.out.println("Message NOT added");
    
    if (!machine.addMessage("Party tonight on Brent Road"))
      System.out.println("Message NOT added");
    
    System.out.println("\nYou have " + 
                       machine.numberOfMessages() +
		       " messages");
    
    System.out.println("\nMessages: \n" + machine);
    
    System.out.println("\nMessage: " + 
                       machine.lastMessage());
    System.out.println("\nMessages: \n" + machine);
    
    System.out.println("\nHave messages?: " + 
                        machine.hasMessages());
    System.out.println("\nMessage: " + 
                       machine.lastMessage());
    System.out.println("\nMessage: " + 
                       machine.lastMessage());
    System.out.println("\nMessage: " + 
                       machine.lastMessage());
  }
}
/*OUTPUT:
csc% java AnsweringMachine
eos% java AnsweringMachine
Message: No messages

Messages: 

You have 3 messages

Messages: 
Sat Apr 19 13:46:08 EDT 2003 Call home
Sat Apr 19 13:46:08 EDT 2003 Doctor appt. tomorrow at 9:00
Sat Apr 19 13:46:08 EDT 2003 Party tonight on Brent Road


Message: Sat Apr 19 13:46:08 EDT 2003 Party tonight on Brent Road

Messages: 
Sat Apr 19 13:46:08 EDT 2003 Call home
Sat Apr 19 13:46:08 EDT 2003 Doctor appt. tomorrow at 9:00


Have messages?: true

Message: Sat Apr 19 13:46:08 EDT 2003 Doctor appt. tomorrow at 9:00

Message: Sat Apr 19 13:46:08 EDT 2003 Call home

Message: No messages

*/     

Waiting Room List:
  • adds new patients to the END of the list
  • removes patients from the FRONT of the list

The Computer Science term for this kind of list is a "queue". It is analogous to the "queue (line) in a cafeteria" where courteous people add themselves to the end of the line and the person at the front of the line is served first.

        
	        ______        
	       |      |
 numberOfItems |      |    MAX_SIZE  ________
               |______|
 _______________________________________________________
|                                                       |
|                      WAITING ROOM LIST                |
|_______________________________________________________|
|                               |                       |
|             Name              |      Symptoms         |
|_______________________________|_______________________|
|                               |                       |               
|_______________________________|_______________________|
|                               |                       |
|_______________________________|_______________________|
|                               |                       |                
|_______________________________|_______________________|
|                               |                       |
|_______________________________|_______________________|
|                               |                       |                
|_______________________________|_______________________|
|                               |                       |
|_______________________________|_______________________|
|                               |                       |                
|_______________________________|_______________________|
|                               |                       |               
|_______________________________|_______________________|

Patient Class

public class Patient {

  private String name;
  
  private String symptoms;
  
  public Patient(String name, String symptoms) {
  
    this.name = name;
    this.symptoms = symptoms;
  }
  
  public String toString() {
  
    return name + "\t" + symptoms;
  }
}

Waiting Room List Class

 public class WaitingRoomList {

   private int numberOfPatients;
   
   private Patient[] patients;
   

   public WaitingRoomList(int size) {
   
     patients = new Patient[size];
     
     numberOfPatients = 0;
   }
   
   public int numberOfPatients() {
   
     return numberOfPatients;
   }
   
   public boolean addPatient(String name, String symptoms) {
   
     if (numberOfPatients < patients.length) {
     
       patients[numberOfPatients++] = 
                               new Patient(name, symptoms);
       
       return true;
     }
     return false;
   }
   
   public String removePatient() {
   
     if (numberOfPatients > 0) {
     
       //Get first patient's name and symptoms
       String patientInfo = patients[0].toString();
       
       //Shift the rest of the patients up one spot 
       //(left shift)
       for (int i = 0; i < numberOfPatients - 1; i++)
       
         patients[i] = patients[i+1];
	 
       //Decrement number of patients
       numberOfPatients--;
       
       //Return first patient's name and symptoms
       return patientInfo;
     }
     
     else
     
       return "No patients";
  }
 
  public String toString() {
  
    String s = "";
    for (int i = 0; i < numberOfPatients; i++)
      s += (i+1) + ". " + patients[i].toString() + "\n";
    return s;
  }
  
  public static void main(String[] args) {
  
    WaitingRoomList list = new WaitingRoomList(5);
    
    if (!list.addPatient("Britney Spears", "laryngitis"))
      System.out.println("Patient NOT added");
      
    if (!list.addPatient("Kurt Vonnegut", "writer's cramp"))
      System.out.println("Patient NOT added");
      
    if (!list.addPatient("Joseph Smith", "stomach flu"))
      System.out.println("Patient NOT added");
      
    
    System.out.println("\nPatients: \n" + list);
    
    int numberOfPatients = list.numberOfPatients();
    
    for (int i = 0; i < numberOfPatients; i++)
      System.out.println("Patient: " + list.removePatient()); 
      
    System.out.println("Patient: " + list.removePatient());
  }
}
/* PROGRAM OUTPUT:
eos% java WaitingRoomList

Patients: 
1. Britney Spears	laryngitis
2. Kurt Vonnegut	writer's cramp
3. Joseph Smith	stomach flu

Patient: Britney Spears	laryngitis
Patient: Kurt Vonnegut	writer's cramp
Patient: Joseph Smith	stomach flu
Patient: No patients
*/