COMP 110-003 Fall 2008

Lab 5

25 points

Assigned: Monday, October 6
Due: Tuesday, October 14 by 2:00pm

Description

This description is long (read it thoroughly!), but this lab should not take you too long if you understand Lab 3 and the in-class exercise on methods. As always, if you have questions, ask early.

Lab 5 will revisit the smiley face from Lab 3, but this time we will put most of the functionality inside separate methods. You have been given two skeleton program files, SmileyMain.java, and UpgradedSmiley.java. In this lab, you will have to fill in the code to make the program work as desired (much as in the in-class exercise on methods). Copy both programs to the same directory on your computer and open them in jGRASP. Compile SmileyMain.java (this should also compile UpgradedSmiley.java for you). Do each of the steps below one at a time, and test your program after each step.

  1. Run the program. You will get a NullPointerException. The reason for the exception is that the program is trying to use an object before it has been created. The first thing you must do is create the object by creating a new instance of the class UpgradedSmiley. In SmileyMain.java, note that you have a private (and static, but don't worry about that for now) variable named smiley, of type UpgradedSmiley:

        private static UpgradedSmiley smiley = null;

    Inside the initializeSmiley method of SmileyMain.java, you will see a comment that says:

            // ::: SET THE VARIABLE smiley TO BE A NEW INSTANCE OF
            //     UpgradedSmiley HERE
    

    Do what the comment says. Now that you have a valid instance of UpgradedSmiley to work with, recompile and run the program. It won't do much yet, but it should bring up a window with no errors.

    There are comments beginning with ::: like the above comment all over the code in SmileyMain.java and UpgradedSmiley.java. You must fill in the appropriate code at these locations, based on the code that you wrote in Lab 3. You will have to write some code in the initializeSmiley method of SmileyMain.java, and you will have to fill in several methods in UpgradedSmiley.java. The specifics of what you need to do are described in the next few steps.

  2. Add the drawing code to the draw methods in UpgradedSmiley.java. Find the corresponding drawing code in Lab 3 and paste the correct code into the correct methods. The code for the drawFace method has already been filled in for you as an example.

  3. Once you have added the drawing code, you will have to call these draw methods to draw the face, nose, eyes, and mouth, from inside the paintComponent method (see pp. 317-323, 4th edition, or pp. 284-289, 5th edition for more information on calling methods within a method). There is a comment beginning with ::: telling you where to put this code. Once this has been done, you should be able to run the program and draw the basic smiley face.

  4. We want to provide the user a Smiley where they control the appearance, as we did before. Using the Scanner class, you need to ask the user for the eye color (pick 3 that you like), the size of the nose, and whether they want the Smiley to smile or frown. This code must go in the designated part of the initializeSmiley method of SmileyMain.java. Before you write the code to read input from the user, be sure you understand the Gotcha in the book on p. 95 (4th edition) or p. 89 (5th edition) about the next and nextLine methods.

    Note that there are methods (setMouth, setEyeColor, and setNoseSize) in UpgradedSmiley.java that modify the Smiley based on their parameters; they are currently empty. You will have to call these methods on your instance of UpgradedSmiley from the initializeSmiley method in SmileyMain.java, based on the user's input. Make sure you call the methods with the correct arguments! Use the variables that have already been declared for you in initializeSmiley.

  5. You now need to fill in the remaining methods in UpgradedSmiley.java (setMouth, setEyeColor, and setNoseSize). Most of the code that goes in these methods can come straight from your own Lab 3 assignment. Read (and do not change) the comments above each method to be sure your code does what is intended.

Here is an example run of the program:

What eye color would you like? (red, green, or blue)
green
How big would you like the nose? (1-4)
3
Would you like the smiley to smile? (yes, no)
yes

How to turn in the assignment

Grading

You can also lose points for incorrectly handing in the assignment, not using appropriate white space, not using conventional names for any variables you declare, and not commenting your code where appropriate.