Lab Exercise (Adapted from Chapter 1 of the Book)

Total Page:16

File Type:pdf, Size:1020Kb

Lab Exercise (Adapted from Chapter 1 of the Book)

Part 1: Exploring Classes, Objects, and Methods (This lab is adapted from the book, Objects First with Java: A Practical Introduction using BlueJ)

Open BlueJ example project called shapes (In C:\BlueJ\examples)

Notice the 4 classes.

Make a circle object. Call the circle’s makeVisible method

Invoke some of the circle’s methods without parameters. Notice what each of them does.

Invoke some of the methods with a parameter. Notice what they do. Notice what kind of parameters the different methods take (ints or Strings). What happens if you input something that is not an int? That is not a String? What if you forget the quotes? Or only one of the quotes?

Make 2 more circle objects. Make a square. Make a triangle.

Now you have multiple instances of circle class – each can have distinct properties Make one circle big and yellow. Make one circle small and green

From an object’s pop-up window, choose the Inspect function. Notice the kind of information the object inspector shows you. What happens if you change the state of an object while the object inspector is open? (If you don’t know what it means to change the object’s state, ask me…)

Look at the object inspectors for your big yellow circle and your small green circle. Notice that they have the same fields (generic variables from the class that describe what the object knows) but that the values of the fields are specific to a certain object.

Close all the objects you have open. Now create an image of a house and a sun that looks something like the picture that follows. Keep track of what you are doing to achieve this. Now open the C:\BlueJ\examples\picture project

Create an instance of Picture and invoke its draw method. How do you think the Picture class draws the picture?

Open the Picture code to look at it. Change the sun’s color.

Add a sunset to the Picture. (Use slowMoveVertical to help you do this)

If you added sunset to the end of the draw method, change it so it is a separate method called sunset that the user can call.

Part 2: Code Experiment (adapted from exercises in Chapter 1 of the L&L book)

Enter, compile and run the following application in a new class called Test (you will need to delete the extra stuff that BlueJ puts in automatically) public class Test { public void print(){ System.out.println(“An Emergency Test”); } }

Introduce the following errors, one at a time, to the program. Record any error messages that the compiler produces (so you get used to them). Fix the previous error each time before you introduce a new one. If no error messages are produced, think about why (and try running the program). Try to predict what will happen before you make each change. a. Change Test to test b. Change Emergency to emergency c. Remove the first quotation mark in the string d. Remove the last quotation mark in the string e. Change println to bogus f. Remove the semicolon at the end of the println statement g. Remove the last brace in the program h. Remove the () after print

If there is a time, write another void method called learnMore that prints, on separate lines, your name, your birthday, and your favorite movie.

Recommended publications