CSE115 Fall 2007
 Fall 2007 CSE115 Introduction to Computer Science For Majors I  
CSE115 Fall 2007 - Navigation Menus
CSE115 Fall 2007 - Lab #6

CSE115 Fall 2007 - Lab #6

Working from home

If you are working from home you must download a current copy of the Classlibs.jar file
WINDOWS: download and place in your C:\projects\CSE115\Classlibs\Fall2007 directory
OS X/Linux/Unix/etc: download and place in your /projects/CSE115/Classlibs/Fall2007 directory


Introduction

The Birds and Bees from lab 5 have flown away. Now we are left to play with some bouncy circles and squares. In this lab your job is to implement an interface in various ways to build circles and squares with different behaviors.

Objectives

The high-level objectives of this lab are to have you:
  • define a class which implements an interface
  • implement an interface in different ways
  • work more independently in solving a programming task than in past labs
You'll notice that we give you less step-by-step guidance in this lab. This is intentional. However, if you need extra help, don't hesitate to ask your lab TA or attend office hours: that's what they're there for!

Lab Design

In this lab you will define a number of classes whose instances appear as circles and squares. They are to move around in a window in different ways. You will, in how you define these classes, specify their behaviors. Your TA will show you a demo of one possible solution.

In order for the classes that you define to work with the graphical framework that we have set up for you, they must implement the IUpdatable interface:

package lab6lib;

/**
 * An IUpdatable is an object which can be updated.
 * 
 * Created: Oct 2007
 * 
 * @author Carl Alphonce
 */

public interface IUpdatable {

    /**
     * Update the object in whatever way you want.
     */
    public void update();
}
Each class that you define as implementing this interface must, of course, define the update method specified in the interface. How you define this method will determine your objects' behavior.

Of course nothing worth doing is as easy as we'd like. In addition to implementing the IUpdatable interface, each class that you define must have (in the sense of the composition relationship) an lab6lib.IGraphic, which will serve as its graphical representation. IGraphic is an interface, so you can't instantiate it. However, you don't need to define classes which implement it, because we have already done that: the two classes lab6lib.CircleGraphic and lab6lib.SquareGraphic implemenet lab6lib.IGraphic.

Since these two classes implement the lab6lib.IGraphic interface, they provide the following method definitions:

package lab6lib;

/**
 * An IGraphic is a graphic whose color, location, dimension and orientation
 * properties can be queried and set.
 * 
 * Created: Sun Oct 10, 2003
 * 
 * @author Carl Alphonce
 * 
 */

public interface IGraphic {

    /**
     * Sets the color of this IGraphic.
     * @param c - the new color
     */
    public void setColor(java.awt.Color c);

    /**
     * @return the current color of this IGraphic
     */
    public java.awt.Color getColor();

    /**
     * Sets the location of this IGraphic.
     * @param p - the new location
     */
    public void setLocation(java.awt.Point p);

    /**
     * @return the current location of this IGraphic
     */
    public java.awt.Point getLocation();

    /**
     * Sets the dimension of this IGraphic.
     * @param d - the new dimension
     */
    public void setDimension(java.awt.Dimension d);

    /**
     * @return the current dimension of this IGraphic
     */
    public java.awt.Dimension getDimension();

    /**
     * Sets the orientation (rotation) of this IGraphic.
     * @param d - the new orientation, in degrees
     */
    public void setOrientation(lab6lib.Degree d);

    /**
     * @return the current orientation (rotation) of this IGraphic, in degrees
     */
    public lab6lib.Degree getOrientation();
    
    /**
     * Make the IGraphic move.
     */
    public void move();
}

To help you get started, here's a skeletal class definition along the lines of what you're aiming for:

package lab6;

public class MyExample implements IUpdatable
{
   private IGraphic _graphic;

   public TurningSquare (lab6lib.Window w) {
	   _graphic = new lab6lib.SquareGraphic(w);
   }

   public void update() {
	   _graphic.move();
   }

}
Notice a few things:
  1. The constructor for both lab6lib.SquareGraphic and lab6lib.CircleGraphic takes a lab6lib.Window as an argument.
  2. In the update method you will define the class' behavior...but don't forget to call move() on the IGraphic, otherwise it will just sit in the same position in the window.
In the Driver class, you will need to add your IUpdatable objects to the Window by calling the addIUpdatable method on the Window.
package lab6;

public class Driver {
    public Driver() {
        lab6lib.Window w = new lab6lib.Window();
        w.addIUpdatable(new MyExamble(w));
    }
}

You will find the lab6lib.Random class useful too. It provides the following methods:

public java.awt.Dimension randomDimension()
A java.awt.Dimension object represents a dimension (a width and a height). Note that the setDimension method of the IGraphic interface declares a parameter of type java.awt.Dimension.
public graphics.colors.Color randomColor()
A java.awt.Color object represents a color. Note that the setColor method of the IGraphic interface declares a parameter of type java.awt.Color.
public lab6lib.Degree randomDegree()
A lab6lib.Degree object represents a degree (as in 1/360th the way around a circle). Note that the setOrientation method of the IGraphic interface declares a parameter of type lab6lib.Degree.
public lab6lib.SineMaker randomSineMaker()
A lab6lib.SineMaker is a class whose instances provide the method next(), which returns a java.util.Dimension. Both the width and height of the returned dimension are the same, and lie along a sine curve. Successive calls to next() return points along the sine curve, at regular intervals.

The lab6lib.Degree class provides the following method:

public lab6lib.Degree turn()
When called on a given Degree object, it turns a new Degree object, turned two degrees.


Lab requirements

You must define three distinct classes which implement the IUpdatable interface, each of which updates one of its IGraphics's properties.

You must also define one additional class, also implementing the IUpdatable interface, which has the combined behaviors of the other three classes you defined.


Grading (rough, and subject to change)

As a rough guideline, you can expect 30 points to come from your JavaDocs, 60 points to come from coding, and 10 points to come from proper submission.


What you hand in

Once you are ready to submit, export your solution as a jar file. Name your jar file Lab6.jar

Use the electronic submission command that corresponds to your recitation. For instance students in section Z8 will submit by typing,

submit_cse115z8 Lab6.jar
at the Linux prompt.


Due dates

You have one week from the meeting of your lab to submit your solution. For example, if your recitation meets on Wednesday, October 17th then you must submit the lab by 11:59 PM on Tuesday, October 23rd . The due dates are summarized in the table below.  If you submit more than once, the later submission will simply overwrite the previous one.

Date of lab Due date for electronic submission
Tuesday, October 16 Monday, October 22
Wednesday, October 17 Tuesday, October 23
Thursday, October 18 Wednesday, October 24
Friday, October 19 Thursday, October 25

Notice

This lab was designed and written by Carl Alphonce.


CSE115 Fall 2007

 

Page maintained by Carl Alphonce
tel: (716) 645-3180 x 115 • fax: (716) 645-3464 • e-mail: alphonce (at) cse dot buffalo dot edu