Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java exercise review Lesson 25: Exercise 1, 2 and 3.

Similar presentations


Presentation on theme: "Java exercise review Lesson 25: Exercise 1, 2 and 3."— Presentation transcript:

1 Java exercise review Lesson 25: Exercise 1, 2 and 3

2 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.setMotion (Math.random()*15.0, Math.random()*15.0); } public void paint (Graphics g) { aBall.paint(g); aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Exercise #1 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; }

3 The code for exercise#1

4 The Result for exercise#1

5 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 < BallArraySize; i++) { ballArray[i] = new Ball(10, 15, 5); pickColor = Math.random()*10.0; if (pickColor < 3.3) ballArray[i].setColor (Color.green); else if (pickColor >= 3.3 && pickColor < 6.6) ballArray[i].setColor (Color.red); else if (pickColor >= 6.6 && pickColor < 10.0) ballArray[i].setColor (Color.blue); ballArray[i].setMotion (3.0+i, 6.0-i); }} public void paint (Graphics g) { for (int i = 0; i < BallArraySize; i++) { ballArray[i].paint(g); // then move it slightly ballArray[i].move(); if ((ballArray[i].x() FrameWidth)) ballArray[i].setMotion (-ballArray[i].xMotion(), ballArray[i].yMotion()); if ((ballArray[i].y() FrameHeight)) ballArray[i].setMotion (ballArray[i].xMotion(), -ballArray[i].yMotion()); } counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Exercise #2 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 if…else if statements to set the color using the setColor function from Ball.class: public void setColor (Color newColor) { color = newColor; }

6 The code for exercise#2

7 The Result for exercise#2

8 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 < BallArraySize; i++) { ballArray[i] = new Ball(10, 15, (i+5)); pickColor = Math.random()*10.0; if (pickColor < 3.3) ballArray[i].setColor (Color.green); else if (pickColor >= 3.3 && pickColor < 6.6) ballArray[i].setColor (Color.red); else if (pickColor >= 6.6 && pickColor < 10.0) ballArray[i].setColor (Color.blue); ballArray[i].setMotion (3.0+i, 6.0-i); }} public void paint (Graphics g) { for (int i = 0; i < BallArraySize; i++) { ballArray[i].paint(g); // then move it slightly ballArray[i].move(); if ((ballArray[i].x() FrameWidth)) ballArray[i].setMotion (-ballArray[i].xMotion(), ballArray[i].yMotion()); if ((ballArray[i].y() FrameHeight)) ballArray[i].setMotion (ballArray[i].xMotion(), -ballArray[i].yMotion()); } counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Exercise #3 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) { ……… }

9 The code for exercise#3

10 The result for exercise#3

11 In-class assignment 1.The core of the exercise is to move the bounds test into the BoundedBall class which should be an extended class of Ball (don’t copy the code from ‘Ball’). 2.Write the psudocode and the deployment diagram for what you are planning to do 3.Store the height and length of the window in the BoundedBall class. Through accessor functions call it in BallWorld when the window is created. 4.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.


Download ppt "Java exercise review Lesson 25: Exercise 1, 2 and 3."

Similar presentations


Ads by Google