Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Lab 2

Laboratory 2 - Modifying and Examining Object State

Goals:
To understand how selection statements can be used to control the behavior of graphics objects; To create/modify accessor, mutator, and general methods; To experiment with the interaction of graphics objects that are controlled by a "driver class"; To create a BlueJ project from scratch; To use print statements for elementary debugging.
Getting started:
The Steps of the Lab:
  1. Work through exercises 2.72 and 2.73 in your textbook. (When you create the heater project, save it in the Lab2 folder on your I: drive)
  2. Close the heater-exercise project and open the shapes project.
    Read Open the editor for the Circle class. Read through the class definition making note of the fields and the methods. Do the same for the Triangle class.
    Modify

    If you invoke any of the move methods for Circle and Triangle objects repeatedly the objects will eventually move out of their display window and disappear.

    You can prevent this from happening by modifying the moveHorizontal and moveVertical methods of the circle and triangle classes so that those objects will never move off of the Canvas. Why will modifying only these two methods cause some of the other move methods to have the desired result? Which ones will not have the desired result?

    NOTE: Do not be concerned that the objects stop moving precisely when they are at an edge. Being close to an edge (or even slightly over the edge) is good enough.

    You will need to write a selection statement (if-else) for each of the methods, moveHorizontal and moveVertical in each class, Circle and Triangle. Each method should determine the position of the figure and either move the figure or not move it if it is too close to a boundary. Again, you decide just how close the figure may come to the boundary; it does not need to be precisely at the edge.

    The left boundary of the canvas in the horizontal direction is 0 and the top boundary in the vertical direction is 0. You can use the getWidth() and getHeight() methods of the canvas field in each class of object to determine window boundaries. (Note that canvas is one of the fields for a Circle and a Triangle.) So to get the dimensions of the canvas use:

        canvas.getWidth();
        canvas.getHeight();
                                   
    

    Both of these methods return int values.

    Drawing a picture of an object near a screen boundary should help you decide when to not move the object because it is too close to one of the boundaries.

    Make sure you thoroughly test by trying to move an object off of the screen before moving to the next step of the lab.

    Write

    Create a new method, moveDirection, for the Circle and Triangle classes which has the signature:

    public void moveDirection(int distanceX, int distanceY)
                                   
    

    There are several ways to do this, but we will examine only one of them.

    Using moveHorizontal and moveVertical: Simply invoke each of these methods in the body of moveDirection, giving each the appropriate parameter -- distanceX to moveHorizontal and distanceY to moveVertical.

    Test moveDirection before going on to the next step. Invoke it several times, and carefully watch the shape move on the canvas. How could moveDirection be written differently to make it work better? (You do not need to write a different version now; just think about how you would do it.)

    Write

    Write a new method, displayState(), that prints all of the fields of a circle object in a terminal window. Note that this is the same information that is displayed by the BlueJ inspector window (right-click on an object in BlueJ and choose "Inspect").

    Test the method by invoking it, changing a circle state variable, invoking displayState() again, and so on.

    Modify

    Open the driver class. Note its fields and what the constructor does. Create an instance of the driver class, observing where the ball and rocket are displayed. Add code to the moveAway() method to call moveDirection on the circle and triangle to cause them to move away from each other. See the comments for the moveAway() method to find hints for how this should work.

    Test your code, and if it is working, ask to be checked out of the lab.

Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University, Computer Science Department, Spring 2005
Maintained by Brian Howard (bhoward@depauw.edu). Last updated