Title: Java exercise review
1Java exercise review Lesson 25 Exercise 1, 2
and 3
2Exercise 1
import java.awt. public class BallWorld extends
Frame public static void main (String
args) BallWorld world new BallWorld
(Color.red) world.show() public static
final int FrameWidth 600 public static final
int FrameHeight 400 private Ball
aBall private int counter 0 private
BallWorld (Color ballColor) setSize
(FrameWidth, FrameHeight) setTitle ("Ball
World") aBall new Ball (10, 15,
5) aBall.setColor (ballColor) aBall.setMotio
n (Math.random()15.0, Math.random()15.0) pu
blic void paint (Graphics g) aBall.paint(g)
aBall.move() if ((aBall.x() lt 0)
(aBall.x() gt FrameWidth)) aBall.setMotion
(-aBall.xMotion(), aBall.yMotion()) if
((aBall.y() lt 0) (aBall.y() gt
FrameHeight)) aBall.setMotion
(aBall.xMotion(), -aBall.yMotion()) counter
counter 1 if (counter lt 2000)
repaint() else System.exit(0)
- Using Math.random modify the program so that the
ball initially moves in a random direction
Since Ball.Class accepts only double as an
argument, we must force the result into a double
format by using a decimal point From the
Ball.class public void setMotion (double ndx,
double ndy) dx ndx dy ndy
3The code for exercise1
4The Result for exercise1
5Exercise 2
import java.awt. public class MultiBallWorld
extends Frame public static void main (String
args) MultiBallWorld world new
MultiBallWorld () world.show() public
static final int FrameWidth 600 public static
final int FrameHeight 400 private Ball
ballArray private double pickColor private
int counter 0 private static final int
BallArraySize 8 private MultiBallWorld ()
setSize (FrameWidth, FrameHeight) setTitle
("Ball World") ballArray new Ball
BallArraySize for (int i 0 i lt
BallArraySize i) ballArrayi new
Ball(10, 15, 5) pickColor
Math.random()10.0 if (pickColor lt 3.3)
ballArrayi.setColor (Color.green) el
se if (pickColor gt 3.3 pickColor lt
6.6) ballArrayi.setColor (Color.red) els
e if (pickColor gt 6.6 pickColor lt
10.0) ballArrayi.setColor
(Color.blue) ballArrayi.setMotion (3.0i,
6.0-i) public void paint (Graphics g)
for (int i 0 i lt BallArraySize i)
ballArrayi.paint(g) // then move it
slightly ballArrayi.move() if
((ballArrayi.x() lt 0) (ballArrayi.x() gt
FrameWidth)) ballArrayi.setMotion
(-ballArrayi.xMotion(), ballArrayi.yMotion())
if ((ballArrayi.y() lt 0)
(ballArrayi.y() gt FrameHeight)) ballArrayi
.setMotion (ballArrayi.xMotion(),
-ballArrayi.yMotion()) counter counter
1 if (counter lt 2000) repaint() else
System.exit(0)
- Using Math.random modify the program so that the
balls have different colors
A variable is used (pickColor) to store the
randomized value. The value is increased by 10 to
get a double and conditioned through ifelse if
statements to set the color using the setColor
function from Ball.class public void
setColor (Color newColor) color
newColor
6The code for exercise2
7The Result for exercise2
8Exercise 3
import java.awt. public class MultiBallWorld
extends Frame public static void main (String
args) MultiBallWorld world new
MultiBallWorld () world.show() public
static final int FrameWidth 600 public static
final int FrameHeight 400 private Ball
ballArray private double pickColor private
int counter 0 private static final int
BallArraySize 8 private MultiBallWorld ()
setSize (FrameWidth, FrameHeight) setTitle
("Ball World") ballArray new Ball
BallArraySize for (int i 0 i lt
BallArraySize i) ballArrayi new
Ball(10, 15, (i5)) pickColor
Math.random()10.0 if (pickColor lt 3.3)
ballArrayi.setColor (Color.green) el
se if (pickColor gt 3.3 pickColor lt
6.6) ballArrayi.setColor (Color.red) els
e if (pickColor gt 6.6 pickColor lt
10.0) ballArrayi.setColor
(Color.blue) ballArrayi.setMotion (3.0i,
6.0-i) public void paint (Graphics g)
for (int i 0 i lt BallArraySize i)
ballArrayi.paint(g) // then move it
slightly ballArrayi.move() if
((ballArrayi.x() lt 0) (ballArrayi.x() gt
FrameWidth)) ballArrayi.setMotion
(-ballArrayi.xMotion(), ballArrayi.yMotion())
if ((ballArrayi.y() lt 0)
(ballArrayi.y() gt FrameHeight)) ballArrayi
.setMotion (ballArrayi.xMotion(),
-ballArrayi.yMotion()) counter counter
1 if (counter lt 2000) repaint() else
System.exit(0)
- Using Math.random modify the program so that the
balls have different radiuses and colors
Notice that the constructor for ball needs to be
in format of X,Y coordinate and then the radius.
All messages must be integers since this is the
way ball.java was defined public Ball (int x,
int y, int r)
9The code for exercise3
10The result for exercise3
11In-class assignment
- The core of the exercise is to move the bounds
test into the BoundedBall class which should be
an extended class of Ball (dont copy the code
from Ball). - Write the psudocode and the deployment diagram
for what you are planning to do - Store the height and length of the window in the
BoundedBall class. Through accessor functions
call it in BallWorld when the window is created. - The end result should be three files called
BallWorld.java (the new one), BoundedBall.java
(the new one) and Ball.java (unchanged). You do
not have to upload the unchanged file Ball.java.