CS 1054: Lab 4

Lab meeting 4: Creating a class and Debugging

This lab is intended to help you write code of your own and using the BlueJ debugger. 

Writing a Class

  1. Open the BlueJ environment. This can be done by clicking on Start -> BlueJ or Start -> All Programs -> BlueJ -> BlueJ.
  2. Create a new project. Go to the Project menu and select "New Project...."
  3. You might want to have a copy of the Chapter 3 notes open in another window (Shift+Click to open in another window).
  4. Add a new class to the project by clicking on the "New Class" button on the left. Name this class "Entry". Delete all the source code present in that class file and copy and paste the code given below.
    class
    
    Entry {
    
    	// Declare instance variables here
    	private String ???
    	private String ???
    	private String ???
    	private String ???
    
    	// Constructor method
    	public Entry(String firstname, String lastname){
    		???
    	}
    	
    	public void printEntry(){
    		System.out.println(last + ", " + first);
    		???
    		???
    	}
    
    	public void setPhone(String phone){
    		???
    	}
    
    	public String getLastFirst(){
    		return ??? + ", " + ???;
    	}
    
    	public String getFirst(){
    		return ???;
    	}
    }
    					
  5. Your task is to complete the Entry class. You will have to decide how to implement the methods of the class that are described below. The Entry class needs to store a person’s first name, last name, phone number, and email address. To do this, declare four private instance variables for these four values. Use String references for the variables.

    Second, complete the constructor. The constructor will need to take two String references as parameters: first and last names. Be careful not to use the same identifiers as your instance variable names or you will hide them. If you do want to use the same names, refer to section 3.12.2 in the textbook to the use of the keyword this. The constructor must initialize the two instance variables for the name.

    Third, you will need to complete or add definitions for the following methods to the class:
    • Entry() – this is the constructor and it takes in a first and last name as parameters
    • setPhone() – takes in phone number as a parameter
    • setEmail() – takes in e-mail address
    • getLastFirst() – returns name in the format of lastName, firstName
    • getFirstLast() – returns name in the format of firstName lastName
    • getFirst() – returns first name only
    • getLast() – returns last name only
    • getPhone() – returns phone number
    • getEmail() – returns e-mail address
    • printEntry() - should print the instance data to the screen in the following format: line 1: last name, first name line 2: e-mail address line 3: phone number
    Remember that when a method returns a value, the prototype must show the return type and the last line of the method should be a return statement. Methods that set variables or print values do not return any value and should have a return type of “void.”
  6. Test your class by creating objects and applying the various methods.
  7. Show your work to the GTA. 

Using the BlueJ debugger

  1. Make a directory on the Z: drive with your university pid as the name. Download the lab4.zip file, unzip it on the Z: drive. This should unzip one project to the Z: drive: mail-system.
  2. Open the mail-system project from the Z: drive by clicking on Project -> Open Project. You should see three rectangles on the screen representing the MailServer, MailClient and MailItem classes. Compile them by right-clicking on the rectangle and selecting compile on the menu. Note: For more detailed instructions on using the BlueJ environment, you can refer to the BlueJ tutorial (Shift + Click to open it in another window).
  3. Do the tasks in exercise 3.21. Familiarize yourself with how this application works. Create a mail server and a few mail clients. Send messages between the clients and use the different methods associated with the client.
  4. Create an object diagram indicated in exercise 3.22 and show it to the GTA.
  5. Do the tasks mentioned in exercises 3.23, 3.24, 3.25, 3.26, 3.27, 3.28 and 3.29. The idea behind these exercises is to familiarize yourself with the working of a basic debugger. The use of a debugger is one of the important skills that you should learn in conjunction with programming.

© Mir Farooq Ali 2003.