1.
Write code to
create a new Text object containing the word “Go” at a location half the width
of the canvas and half the height of the canvas.
2.
Given a Location
variable called clickPoint and a
properly instantiated FilledOval called button,
write an if-statement that would display “clicked” if clickPoint is inside the FilledOval.
3.
Arrays
a.
Write a declaration
for an array of FilledRects.
b.
Write a line of
code to declare an integer constant with the value 5.
c.
Write a line of
code to size the array using the constant declared in (b).
d.
Write code to
initialize the last cell of the array to have width 300, height 200 and be
located at location (0,0).
4.
Write the html
tag that would run Java applet BalloonRide in a canvas 600 wide and 400 high.
BalloonRide relies on objectdraw.
5.
Declare a
variable and load the resource whose file name is “froggy.gif”.
6.
Examine the
following method.
int whatHappens (int [] list)
{
int s = 0;
for (int i=0;
i<list.length - 1; i++)
{
s = s +
list[i];
}
return s;
}
If
list were initialized with the following code.
int [] list = {10, 20, 30, 40, 50};
Which
of the following represents the result of method whatHappens?
A) 0
B) 150
C) 100
D) 50
E) 60
7.
Which of the
following is NOT a coding practice style used in our programs.
A) Class names are capitalized.
B) The first letter of the first word of an
identifier name is lower case.
C) Constant identifiers are all upper case
letters.
D) The first letter of the first word in a
method name is capitalized.
E) The second letter of the second word of an
identifier name is capitalized.
8.
Examine the
following method:
public int doStuff (FilledRect [] list, int test)
{
int c = 0;
for (int j=0; j<list.length; j++)
{
if (list[j].getWidth()
> test)
{
c = j;
}
}
return c;
}
What
is the intent of this code segment?
A) To count the number of FilledRects in array
list whose width is greater than the test value.
B) To find the first FilledRect in the array
list whose width is greater than the test value.
C) To find the index of the first FilledRect in
the array list whose width is greater than the test value.
D) To find the last FilledRect in the array
list whose width is greater than the test value.
C) To find the index of the last FilledRect in
the array list whose width is greater than the test value.
9.
The rest of the
test is based on a problem we will call “A lot of hot air”. This problem is based on a problem from your
textbook, Java: An Eventful Approach. Program BalloonRide simulates the movement of
hot air balloons. A balloon’s movement
is affected in the horizontal direction by the wind (simulated by the mouse
dragging the balloon). A balloon’s
vertical movement is affected by how inflated the balloon is. The user interacts with the balloons in two ways. When the user selects a balloon’s basket and
drags it around the screen the balloon moves horizontally, but also rises or
falls depending on how inflated it is. The user may also inflate a balloon by
clicking on the balloon. Balloon’s look
like this:

Balloons will need methods to
handle the following tasks:
constructor
This method has three
parameters, the x and y coordinate and the drawing canvas.
deflate
This method has no parameters
and no return values. It decreases the
width of the circle that represents the balloon by 5 pixels so long as the
width of the balloon is greater than the width of the basket.
inflate
This method has no parameters
and no return values. It increases the
width of the circle that represents the balloon by 10 pixels so long as the
balloon does not touch the basket.
Sorry, you can’t blow up balloons J
drift
This method has no return
value and one parameter representing the amount to move all of the balloon
parts in the x-direction. This method also moves the balloon parts in the
y-direction. This amount will be
determined by the following formula:
change in y = current width of balloon – 10. The last thing done by drift is to deflate
the balloon.
basketContains()
This method takes one
Location parameter and returns true or false depending on whether the location
is inside the basket part of the balloon.
balloonContains()
This method takes one
Location parameter and returns true or false depending on whether the location
is inside the oval representing the balloon.
Write the complete class
definition for the Balloon. This is not
an ActiveObject since the user interacts with the balloon to move it. You should use the picture of the balloon to
determine what instance variables are necessary. Other than parts of the balloon, there is one
other instance variable necessary. Implement methods deflate, drift and basketContains. You should leave the bodies of the other
methods blank. This test contains a
summary list of methods for drawable objects – you may find one or more of
these useful in writing the methods.
Location(double x, double y)
Creates a new Location object
Text(String s, double x, double y,
DrawingCanvas c)
Creates a new Text object.
Line(double startx, double starty, double endx,
double endy, DrawingCanvas c)
Creates a new Line
object.
FilledRect(double x, double y, double width,
double height, DrawingCanvas c)
Creates a new
FilledRect object.
FilledOval(double x, double y, double width,
double height, DrawingCanvas c)
Creates a new
FilledRect object.
FilledArc(double x, double y, double width,
double height, double startangle, double arcangle,
DrawingCanvas c)
Creates a new
FilledArc object.
FilledRoundedRect(double x, double y, double width,
double height, double arcwidth, double archeight,
DrawingCanvas c)
Creates a new
FilledRoundedRect object.
FramedRect(double x, double y, double width,
double height, DrawingCanvas c)
Creates a new
FilledRect object.
FramedOval(double x, double y, double width,
double height, DrawingCanvas c)
Creates a new
FilledRect object.
FramedArc(double x, double y, double width,
double height, double startangle, double arcangle,
DrawingCanvas c)
Creates a new FilledArc
object.
FramedRoundedRect(double x, double y, double width,
double height, double arcwidth, double archeight,
DrawingCanvas c)
Creates a new
FilledRoundedRect object.
double getWidth() // returns the width of the
canvas
double getHeight() // returns the height of
the canvas
void move (double dx, double dy); // moves
the object to right dx and down dy
void moveTo (double x, double y); // moves the
object to the coordinate (x,y)
double getX() // returns the x coordinate
double getY() // returns the y coordinate
boolean contains (Location p) // returns true
if p is inside the object’s bounding rectangle
void setWidth(double dist) //Sets the width of the object's bounding rectangle void setHeight(double dist) //Sets the height of the object's bounding rectangle