java exercise review lesson 26: exercise 4. exercise #4 – a sample solution 1.write the psudocode...

9
Java exercise review Lesson 26: Exercise 4

Upload: spencer-oneal

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} The new Ball.java file The old Ball.java file Removed section

TRANSCRIPT

Java exercise review

Lesson 26: Exercise 4

Exercise #4 – a sample solution

1. Write the psudocode and the deployment diagram for what you are planning to do

2. 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’).

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.

“Rather than testing whether or not the ball has hit the wall in our main program, we could have used inheritance to provide a specialized form of ball. Create a class called “BoundedBall” that inherits from class Ball. The constructor for this class should provide height and width of the window, which should subsequently be maintained in as data fields in the class. Rewrite the move function so that is the ball moves outside the bounds it automatically reverses direction. Finally rewrite the BallWorld class to use an instance of BoundedBall rather than an ordinary Ball, and eliminate the bunds test in the main program.”

import java.awt.*;public class Ball {

protected Rectangle location;protected double dx, dy;protected Color color;public void setColor (Color newColor){ color = newColor; }

public void setMotion (double ndx, double ndy){ dx = ndx; dy = ndy; }

public int radius (){ return location.width / 2; }

public int x (){ return location.x + radius(); }

public int y (){ return location.y + radius(); }

public double xMotion (){ return dx; }

public double yMotion (){ return dy; }

public Rectangle region () { return location; }

public void moveTo (int x, int y){ location.setLocation(x, y); }

public void move (){ location.translate ((int) dx, (int) dy); }

public void paint (Graphics g) {g.setColor (color);g.fillOval (location.x, location.y, location.width, location.height);

}}

The new Ball.java file The old Ball.java file

Removed section

import java.awt.*;

public class BoundedBall extends Ball {

private static final int FrameWidth = 700;private static final int FrameHeight = 500;

public BoundedBall (int x, int y, int r) {location = new Rectangle(x-r, y-r, 2*r, 2*r);dx = 0;dy = 0;color = Color.blue;

}public int Width () {

return FrameWidth;}public int Height () {

return FrameHeight;}public void move () {

location.translate ((int) dx, (int) dy);if ((x() < 0) || (x() > FrameWidth))

setMotion (-xMotion(), yMotion());if ((y() < 0) || (y() > FrameHeight))

setMotion (xMotion(), -yMotion());}

}

The new BoundedBall.java file The old Ball.java file

Removed section

Since we are going to use Ball as the definition class and use special cases as the actual object definition (which allows for extensions), we must place the constructor in the class where the objects are created instead of leaving it in the “master” definition class.

import java.awt.*;

public class BoundedBall extends Ball {

private static final int FrameWidth = 700;private static final int FrameHeight = 500;

public BoundedBall (int x, int y, int r) {location = new Rectangle(x-r, y-r, 2*r, 2*r);dx = 0;dy = 0;color = Color.blue;

}public int Width () {

return FrameWidth;}public int Height () {

return FrameHeight;}public void move () {

location.translate ((int) dx, (int) dy);if ((x() < 0) || (x() > FrameWidth))

setMotion (-xMotion(), yMotion());if ((y() < 0) || (y() > FrameHeight))

setMotion (xMotion(), -yMotion());}

}

The new BoundedBall.java file The old BallWorld.java file

Moved sectionSince the framewidth are fixed in BoundedBall.class, there is no longer a need to test for location (the users cannot change the private, static final variables)

import java.awt.*;

public class BoundedBall extends Ball {

private static final int FrameWidth = 700;private static final int FrameHeight = 500;

public BoundedBall (int x, int y, int r) {location = new Rectangle(x-r, y-r, 2*r, 2*r);dx = 0;dy = 0;color = Color.blue;

}public int Width () {

return FrameWidth;}public int Height () {

return FrameHeight;}public void move () {

location.translate ((int) dx, (int) dy);if ((x() < 0) || (x() > FrameWidth))

setMotion (-xMotion(), yMotion());if ((y() < 0) || (y() > FrameHeight))

setMotion (xMotion(), -yMotion());}

}

The new BoundedBall.java file The old BallWorld.java file

Moved sectionThe accessor functions allows the users in BallWorld to request the size of the frame (window) that is defined in BoundedBall

import java.awt.*;

public class BallWorld extends Frame {

public static void main (String [] args) {BallWorld world = new BallWorld (Color.red);world.show();

}private BoundedBall aBoundedBall;private int counter = 0;

private BallWorld (Color ballColor) {aBoundedBall = new BoundedBall (10, 15, 5);setSize (aBoundedBall.Width(),

aBoundedBall.Height());setTitle ("BallWorld");aBoundedBall.setColor (ballColor);aBoundedBall.setMotion (3.0, 6.0);

}

public void paint (Graphics g) {aBoundedBall.paint(g);aBoundedBall.move();counter = counter + 1;if (counter < 2000) repaint();else System.exit(0);

}}

The new BallWorld.java file The old BallWorld.java file

The accessor functions allows the users in BallWorld to request the size of the frame (window) that is defined in BoundedBall, and use the function called “setSize” inherited from the Ball.class to set the size of the frame (window)

Exercise 4 illustrates the importance of inheritance through extensions, the need for clear functional delineation of the program (as may be provided by a UseCase diagram), as well as the importance of deployment diagrams and a detailed class library that tells the programmer what is available.

End result

Extended byU

sed in

End result

Java Files

Compiling

Class Files

Output