Description of Balloon Things:
(1) a balloon has:
- color (a string)
- size (a number)
- position (two numbers)
(2) a balloon can be created:
- by giving values for
all of its properties
- by giving values for
some of its properties
and setting others to
chosen default values
(3) a balloon can do:
- tell us its color
- tell us its size
- tell us its X location
- tell us its Y location
- move away by some
given amounts
- draw itself based on
its location, size, color
|
package cs1.ParkApp;
import cs1.app.*;
public class Balloon
{
// the section for the "features/characteristics" ( the data members )
private String color;
private double size;
private double curX;
private double curY;
// the section for the constructors ( special methods )
// * can have as many as needed
// * must have same name as the class
// * no return type (not even void)
public Balloon( String initColor, double initSize, double initX, double initY )
{
color = initColor; // set each "feature" to what user specified
size = initSize;
curX = initX;
curY = initY;
}
public Balloon( String initColor, double initSize )
{
color = initColor;
size = initSize;
curX = canvas.getRandomDouble( 0, canvas.getWidth() );
curY = canvas.getHeight() / 2;
}
// the section for the "actions/behaviors" ( the methods )
public String getColor( )
{
return color;
}
public double getSize( )
{
return size;
}
public double getX( )
{
return curX;
}
public double getY()
{
return curY;
}
public void drift( int dx, int dy )
{
curX = curX + dx; // update location based
curY = curY + dy; // on the given amounts
}
public void draw( )
{
canvas.drawCircle( curX, curY, size, color );
}
}
|