CS46A Lab

Working with Existing Classes

Copyright © Cay S. Horstmann 2009 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. Back to the Greeter object. (The objects are the red things on the bottom...) What does the sayGoodbye method do?
  10. 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...
  11. 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.
  12. Now right-click on the VisibleRectangle class and construct a new VisibleRectangle(5, 10, 20, 30). What happens?
  13. Construct another rectangle, so that both of them are positioned like this:

    How did you construct the second rectangle?

  14. Call the translate method on the first rectangle with parameters 100 and 50. What happens?
  15. Call the grow method on the first rectangle with parameters 50 and 25. What happens?
  16. How can you get the rectangle back to the original size? (Hint: Look at the API documentation.)
  17. Enough about rectangles. Start Netbeans and pick up where you left off in lab 1, with the helloworld1 project. Check that the run method in MyScene.java has the code
    this.chair.say("Hello, World!");
    this.chair.move(MoveDirection.LEFT, 1);
  18. Add this statement below the other import statement in MyScene.java:
    import org.alice.apis.moveandturn.gallery.kitchen.Toaster;

    Add these statements to the end of the run method:

    Toaster fred = new Toaster();
    addComponent(fred);

    Run your program. What happpens?

  19. That's a puny toaster. Move it toward you. To figure out how to do this, type fred.move. Watch how NetBeans gives you a list of all move methods. This feature is called autocompletion. Unlike BlueJ, NetBeans wants to help you. (Believe it or not, the inventors of BlueJ don't implement this because some instructors want students to memorize methods instead of learning more computer science.) The first one, move(MoveDirection, Number) is the one that you want. Keep typing fred.move(MoveDirection. and watch some choices for move directions. FORWARD sounds good. For the number, try 5. What happens?
  20. Ok, that's still pretty puny. Try the resize method instead. Take a screen capture of your supersized toaster and add it to the lab report.