Scroll All the Way Down
||
||
VV
Test Non-Drawing Methods
create two balloons
display information of FIRST balloon
display information of SECOND balloon
ask FIRST balloon to move ...
... and display information again
ask SECOND balloon to move ...
... and display information again
|
package cs1.app;
import cs1.android.*;
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
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 = 0; // default value
curY = 0; // default value
}
// 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 );
}
public static void main(String[] args)
{
Balloon b1 = new Balloon( "red", 40, 130, 90 ); // red balloon of size 40 at (130, 90)
Balloon b2 = new Balloon( "green", 80 ); // green balloon of size 80 at (0, 0)
System.out.println( "balloon color: " + b1.getColor() ); // should display "red"
System.out.println( "balloon size: " + b1.getSize() ); // should display 40
System.out.println( "balloon x pos: " + b1.getX() ); // should display 130
System.out.println( "balloon y pos: " + b1.getY() ); // should display 90
System.out.println( "balloon color: " + b2.getColor() ); // should display "green"
System.out.println( "balloon size: " + b2.getSize() ); // should display 80
System.out.println( "balloon x pos: " + b2.getX() ); // should display 0
System.out.println( "balloon y pos: " + b2.getY() ); // should display 0
b1.drift( 20, -10 );
System.out.println( "balloon x pos: " + b1.getX() ); // should display 150
System.out.println( "balloon y pos: " + b1.getY() ); // should display 80
b2.drift( -15, 30 );
System.out.println( "balloon x pos: " + b2.getX() ); // should display -15
System.out.println( "balloon y pos: " + b2.getY() ); // should display 30
}
}
|