dumbDODO98

 

CS 1054: Lab 3

Lab meeting 3: Class definitions and using the main method

This lab is supposed to help reinforce the concepts about the internals of a class and also in using the main method in Java.

Understanding class definitions

  1. Make a directory on the Z: drive with your university pid as the name. Download the lab3.zip file, unzip it on the Z: drive. This should unzip one project to the Z: drive: book-exercise.
  2. Open the BlueJ environment. This can be done by clicking on Start -> BlueJ or Start -> All Programs -> BlueJ -> BlueJ.
  3. You might want to have a copy of the Chapter 2 and Java without BlueJ  notes open in another window (Shift+Click to open in another window).
  4. Open the book-exercise project from the Z: drive by clicking on Project -> Open Project. You should see a single rectangle on the screen representing the Book class. Compile the class 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).
  5. If you create an object from this class (by selecting the constructor method on right-click and typing in an author name and a book title), notice that it does not have any methods of its own. Create the methods mentioned in Exercise 2.44 and 2.45 by opening the associated source code of the class. The methods in 2.44 should be accessor methods. For the methods in 2.45, use the System.out.println() method to print out the names of the author and book title.
  6. Do the tasks outlined in Exercises 2.46, 2.47, 2.48, 2.49. Each exercise builds on the previous, so make sure that you implement all specified methods. These methods will also be tested in the next set of tasks.
  7. Create some objects and initialize them with the values given in the table below. You do not need to use the reference number to initialize the objects.

    Table 1: Sample values for initializing objects of class Book.
    Author Title Pages Reference Number
    "Michael Connelly" "The Poet" 528 "123"
    "John Grisham" "The Client" 576 "234"
    "John Irving" "The Hotel New Hampshire" 432 "345"
    "John Steinbeck" "The Grapes of Wrath" 455 "456"

    Invoke the various methods associated with the objects and see if they work correctly. In particular, invoke the setRefNumber() and getRefNumber() methods to make sure that they work correctly.

Using the main method

  1. Create a new class by clicking on the New Class button in BlueJ. Name it testBook.
  2. If you double click on it to view the source code, notice that it contains some default code for a constructor and another method. Delete all that code including the comments and instance variable.
  3. Write a single main method. The signature of the main method is on slide 13 of the "Java without BlueJ" notes. Use that signature exactly including all the words preceding the name of the method.
  4. In the body of the method, create four objects of the Book class that you created earlier. Name those objects book1, book2, book3 and book4. Refer to the notes on slide 15 to see how to create objects. Invoke the constructor method from the Book class carefully by passing the correct number of parameters to it. Use the authors, titles and number of pages shown in Table 1 for the constructor method of the Book class. Remember to surround each string parameter with double quotes (""). One way of creating an object is Book book1 = new Book("John Grisham", "The Client", 576);
  5. Invoke the setRefNumber() method for each object that you created and use the reference number indicated in the table above as the parameter for the method. An example of this for the object book1 is book1.setRefNumber("123");
  6. Now invoke the printDetails() method of each object. This should print out the details of the four books to the terminal window. The terminal window should display something like what is shown below.
  7. Show your work to the GTA.
  8. Remember from the lectures that java can also be used from outside the BlueJ environment. To test this, start a command window by clicking on Start -> Run and typing in cmd . In that window, change to the Z: drive by typing "cd Z:". Change to the book-exercise directory by typing "cd book-exercise". By typing "dir" at the prompt here, you can see the various .java and .class files. Type "java -cp . bookTest" at the prompt. This executes the the main method bookTest class that we created in the BlueJ environment. Your output should look something like what is shown below.
  9. Show your work to the GTA.
  10. If there is time remaining, perform the tasks outlined in Exercises 2.50 and 2.51 from within BlueJ.

© Mir Farooq Ali 2003.