CS46A Lab

Working with Existing Classes

Copyright © Cay S. Horstmann 2009, 2012 Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

See lab 1 for reporting instructions.

  1. Start BlueJ and make a new project. Call it greeter. Make a class Greeter (with an uppercase G). 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 right-click on the class and select new Greeter() from the menu. Click Ok on the dialog. What happens?
  3. Now right-click on the red blob labeled greeter1. What two methods do you get in the middle of the menu?
  4. Select sayHello. What happens?
  5. Click on Get, then Ok, then Close. What happens?

  6. Right-click on string1. You'll get lots of methods. Call the length method. What does it return?
  7. Call the toUpperCase method. What does it return?
  8. Call the replaceAll method. It wants parameters. Supply two parameters so that the method will return "Helloo Woorld!". What parameters did you supply? (Be careful about quotation marks.)
  9. Interacting with the red blobs is nice because it makes you visualize an object as, red blobs. The point is that an object is something on which you can call methods.

    And 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?

  10. Call toUpperCase on string1 in the code pad. What did you do? What was the result?
  11. Type string1. What result do you get? Why isn't it uppercase?
  12. What are the tiny blobs next to the answers good for? Click on one to find out.
  13. Back to the Greeter object.  What does the sayGoodbye method do?
  14. Let's make another greeter. This time, go to the Greeter class (tan rectangle on top), right-click and pick the new Greeter(String) constructor call. Supply your name as the construction parameter. Remember the quotation marks...
  15. Enough about greetings. Your textbook has a section on calling methods on the Rectangle class of the standard library, but it is a bit unsatisfactory that one can't actually see the rectangles. In this set of exercises, you will work with a modification of the Rectangle class that shows the rectangles in a window. Make a new project rectangles. Make a class VisibleRectangle (with uppercase V and R). Double-click on it, erase all code, and paste in this code. Don't look at the code—it contains advanced magic that you won't understand until the end of this semester.
  16. Now right-click on the VisibleRectangle class and construct a new VisibleRectangle(5, 10, 20, 30). What happens?
  17. Construct another rectangle, so that both of them are positioned like this:

    How did you construct the second rectangle?

  18. Call the translate method on the first rectangle with parameters 100 and 50. What happens?
  19. Call the grow method on the first rectangle with parameters 50 and 25. What happens?
  20. How can you get the rectangle back to the original size? (Hint: Look at the API documentation.)
  21. Enough about rectangles. Start Netbeans and pick up where you left off in lab 1, with the hello project. Or just download the project again, following step B6 of lab 1.

    Add this statement below the other import statement in Scene.java:

    import org.lgna.story.resources.prop.CakeResource;

    Add these statements to the end of myFirstMethod:

            Prop cake = new Prop(CakeResource.BIRTHDAY_CAKE_MATERIALS);
            cake.setVehicle(this);
            cake.setPositionRelativeToVehicle(new Position(2, 0, 2));

    Run your program. What happpens?

  22. That's a puny birthday cake. Move it toward you. To figure out how to do this, type cake.move. Watch how NetBeans gives you a list of all move methods. This feature is called autocompletion. The first one, move(MoveDirection, Number, Detail...) is the one that you want. Keep typing cake.move(MoveDirection. and watch some choices for move directions. FORWARD sounds good. For the number, try 1. The details are optional. Don't supply any. What happens?

    In the NetBeans editor, you hit Ctrl+Space to do autocompletion.

  23. Ok, that's still pretty puny. Try the resize method instead. Take a screen capture of your supersized birthday cake and add it to the lab report.