CS46A Lab

Working with Existing Classes

Copyright © Cay S. Horstmann, Kathleen O’Brien 2009-2016 Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Instructions

Part A

  1. Start BlueJ and make a new project lab2a in the cs46a/labs folder. Make a class Greeter by clicking the New Class button and typing in the name Greeter. Double-click on it, erase its contents and paste in the contents of this file. Don't look at it—we'll have a close look in a week or so. Just hit Compile and Close.
  2. Now we are going to work with objects on the BlueJ Workbench. Right-click on the class and select new Greeter() from the menu. Click Ok on the dialog. What happens? (scribe)

    When the lab instructions ask you a question about what you did, the scribe needs to enter the answer into the lab report. For example, "got a red blob"

    You have just created a representation of an object on the workbench

  3. Now right-click on the red blob (the object) labeled greeter1. What two methods do you get in the middle of the menu?
  4. Select sayHello. What happens?
  5. Right-click the object and select sayGoodbyeWhat does the sayGoodbye method do?
  6. The Greeter class has two constructors for making objects. Let's make another Greeter using the other constructor. Go to the Greeter class (the rectangle on top), right-click and pick the new Greeter(String name) constructor call. Supply driver's name as the construction parameter. Remember to use quotation marks to specify a string.
  7. Call the sayHello method on this new object. What does it return?
  8. Click on Get, then OK, then Close. What happens?

  9. See the new red blob? It is another String object. Right-click on string1 object. You'll get lots of methods. Call the length method. What does it return?

    If you can not see the length method, click "more methods" at the bottom of the list. You may need to click "more methods" a second time to see all the methods.

  10. Right click on the red blob again. Call the toUpperCase method. What does it return?
  11. Call the replace method ( replace(CharSequence, CharSequence)). The method needs two parameters, both of which will be Strings. (A String is a type of CharSequence). The first String is the String you want to replace and the second is the value you want to use instead. Supply the two String parameters so that the method will return Hetto <driver name>! (if the driver's name is Carlos, the return value should be Hetto Cartos!)What parameters did you supply? (Be careful about quotation marks. Strings need to be surrounded with double quotes)

    While replacing the whole word "Hello" with the word "Hetto" would work, it is not what is wanted here. Just replace the single String "l" with the String "t".

    Interacting with the red blobs is nice because it makes you visualize objects as ... well, red blobs. The point is that an object is something on which you can call methods.

Part B
  1. It's nice that you can explore objects without having to write a whole program with public static void main and all that.

    Here is another way in which you can explore objects in BlueJ. Select View -> Show Code Pad. In the bottom right corner, you get a window into which you can type instructions. That window is pretty small. Drag the borders to make it bigger.

    Type in

    string1.length()

    What happens?

  2. Call toUpperCase() on string1 in the code pad. (You will type: string1.toUpperCase() )Scribe: What was the result?
  3. Type string1 in the code pad which will print string1. Scribe: What result do you get? Why isn't it uppercase? This is important. If you do not know, ask your lab instructor.
  4. Call the replace method in CodePad to replace the l's with t'. Scribe: What was the result?
  5. Type string1 in the code pad which will print string1. Scribe: What result do you get? Why are the l's not replaced with t's? This is important. If you do not know, ask your lab instructor.
  6. Scribe: Right click on string1 red blob. Click Inspect. Then click Inspect again. What do you see?
  7. We can also manipulate the greeter1 object in the Code Pad
  8. Type greeter1.sayHello() Scribe: What result do you get?

Part C

  1. Enough about greetings. Now we are going to work with the Rectangle class of the graphics library in the videos. Launch BlueJ. Make a new project lab2b in your cs46a/labs folder.
  2. Download the lab2 graphics files from this link. Put this file somewhere so you can find it again. You will need it throughout the semester.
  3. Unzip the file. In Mac OS X, you can safely double click on it, and the file will unzip. If you use Windows, look back at Lab 1B and see how to unzip a file. Do what it takes to unzip the graphics directory in the zip file so that it ends up inside cs46a.
  4. In your BlueJ lab2b project, select Project > Import ... and navigate to the graphics directory that you just unzipped. Select the folder and confirm. What happens?
  5. Press compile if any of the classes are not compiled (have stripes)
  6. Now right-click on the Rectangle class and construct a new Rectangle(5, 10, 20, 30), What happens?
  7. Right-click on the red blob and select the draw method. What happens?.
  8. Construct another rectangle, so that both of them are positioned like this:

    How did you construct the second rectangle? (driver)

  9. Call the translate method on the first rectangle with parameters 100 and 50. You may need to downsize some of your windows to find the form with the rectangles. What happens?
  10. Call the grow method on the first rectangle with parameters 50 and 25. What happens?
  11. How can you get the rectangle back to the original size? (Hint: If you add 50 to a number, you have to subtract 50 to get the original number again.)

 

Part D

Enough about rectangles. It's time for robots.

  1. Make a new project lab2c. Import the graphics files as you did in part C. This contains the Horstmann graphic classes plus a Robot class you will need in thist lab. Be sure to download this file. It contains extra files not in the file downloaded in class. Be sure to put this file somewhere so you can find it again. You will need it throughout the semester.

    If your classes have diagonal lines, then click Compile on the workbench.
  2. Right-click on the Robot class on the workbench and select the second constructor:
    new Robot()

    In the Create Object popup, leave the robot name as robot1 and press OK Scribe: What happens?

  3. To make the robot move forward, right-click on the red blob and select the method moveForward().
  4. See if you can figure out how to make the robot turn. Right-click on the red blob and look at the available methods.What method can you use to make the robot turn? Driver: What are the methods?
  5. Make robot move, turn right, move, turn right, and move and turn another two times (total of 4). Scribe: What happens?
  6. This robot looks a little weird when it turns. It would be better to construct the robot with a different image, such as this one:


    To construct a Robot using an image other than robot.png, you have to use a different constructor. A different constructor lets you set the (x,y) location of the Robot as well as specify an image.

    To construct a robot from this image at a different location , right-click on the Robot class in the workbench and select the first constructor

    new Robot(int x, int y, String imageFile) 
    (The image is in the files you downloaded in a previous step. roomba.png)

  7. You get a pop up dialog box. Construct a Robot at location (2 ,1) with the roomba.png image by entering 2, 1, and "roomba.png" in the text boxes.
  8. Make robot move, turn right, move, turn right, and move and turn another two times (total of 4). Scribe: What happens? Does this look better?