Question: Need help with modifying this program, code is below and instrutions are here: Now we will add code that will generate a CollisionException when a

Need help with modifying this program, code is below and instrutions are here: Now we will add code that will generate a CollisionException when a Player collides with a wall. Of course, your code should attempt to prevent the Player from colliding with a wall, but in the off-chance that there is a bug in your code or that something unexpected occurs in the game, your player may end up colliding with the wall. Create and compile a CollisionException class as follows: public class CollisionException extends Exception { public CollisionException() { super("Player collided with wall !"); } }  Note that any new exceptions that you define must extend the Exception class or one of its subclasses. Once this code is saved and compiled, you are able to generate such an exception. Copy and then complete the code (i.e., it is not finished so you have to write it) for the following method in the Wall class: public boolean contains(Point2D p) { // return true if p.x is in the range from "this.location.x to // (this.location.x+this.width-1)" and if p.y is in the range from // "this.location.y to (this.location.y+this.height-1)" // otherwise return false }  Add the following methods to the Game class so that we can get the Players and the Walls from the Game: public ArrayList getPlayers() { ArrayList result = new ArrayList(); for (GameObject g: gameObjects) { if (g instanceof Player) result.add((Player)g); } return result; } public ArrayList getWalls() { ArrayList result = new ArrayList(); for (GameObject g: gameObjects) { if (g instanceof Wall) result.add((Wall)g); } return result; }  Append the following piece of code to the end of the updateObjects() method in the Game class which will compare all players with all walls to see if a collision occurs and will then throw our newly defined exception if it has indeed occurred: ArrayList players = getPlayers(); ArrayList walls = getWalls(); for (Wall w: walls) { for (Player p: players) { if (w.contains(p.location)) throw new CollisionException(); } }  The code will no longer compile. You should see an error stating: Unhandled exception: CollisionException. JAVA is telling us that we need to tell everyone that this updateObjects() method may now generate a CollisionException. Add an appropriate throws clause to the method definition as follows and then re-compile: public void updateObjects() throws CollisionException { ... }  The GameTestProgram and GameBoardTestProgram will both generate the same compile error because we are calling the updateObjects() method from here and we now must decide what to do when the exception is generated. For now, we are not sure what to do and we just want to see if we can generate our exception. So, to simply satisfy the compiler for now, add throws CollisionException to the main methods as follows, and then re-compile: public static void main(String args[]) throws CollisionException { ... }  Run the GameTestProgram. You should see the exception generated as follows (although your code's line numbers may differ): Exception in thread "main" CollisionException: Player collided with wall ! at Game.updateObjects(Game.java:43) at GameTestProgram.main(GameTestProgram.java:53) ... at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Congratulations ... you have just thrown a brand new exception. Notice that it occurs right where we threw it ... from the updateObjects() method in the Game class. Run the GameBoardTestProgram. You should see the exception occur just before the Player collides with the wall above him/her:  Now we need to catch and handle our exception properly. First, let us catch it. But where do we catch it ? Recall that we have added some throws clauses to our methods and eventually the main method threw the exception and the Java Virtual Machine caught it, and hence stopped the program. Change the following code from the main method in GameBoardTestProgram:  for (int i=0; i into this:  for (int i=0; i Remove the throws CollisionException clause that we added from the main method's definition and then re-run the code. Your program should run to completion because we caught the exception and ignored it. Let's see how many times the exception actually occurred. Add the following line inside the catch block, then re-run the code again:  e.printStackTrace(); ? Scroll through the program output. Did you see how many times the exception occurred ? I think you will notice it occurred 2 times, so the Player hit 2 walls as it moved (because the player does not stop when it hits the walls, it goes right through them). Obviously in a real game this would be serious. For now, let us simply stop the player when it hits a wall. We do this by setting its speed to 0. So we need to add the following to the catch block instead of printing the stack trace:  System.out.println("*** " + e.getMessage() + " ... setting speed to 0"); player.speed = 0;  Re-run the code. Notice that the player no longer goes through the wall. You may also notice that the exception occurs a LOT more now! What is going on ? Do you understand ? Print out the player's location from within the catch block. Think about it. Ask the TA if you do not understand why the exception occurs a lot now. CODE: import javafx.geometry.Point2D; public class Ball extends MovableObject implements Harmful{ private boolean isBeingHeld; public Ball(Point2D loc) { super(0, 0, loc); isBeingHeld = false; } // The get/set methods  public int getDirection() { return direction; } public int getSpeed() { return speed; } public boolean isBeingHeld() { return isBeingHeld; } public void setDirection(int newDirection) { direction = newDirection; } public void setSpeed(int newSpeed) { speed = newSpeed; } public void setIsBeingHeld(boolean newHoldStatus) { isBeingHeld = newHoldStatus; } public String toString() { return "Ball" + " at (" + (int)location.getX() + "," + (int)location.getY() + ") facing " + direction + " degrees going " + speed + " pixels per second"; } public void draw() { //System.out.println("Ball is at (" + (int)location.getX() + "," + (int)location.getY() + ") facing " + direction + " degrees and moving at " + speed + " pixels per second");  } public void update() { moveForward(); draw(); if (speed > 0) speed -= 1; } public int getDamageAmount() { return -200; } public char appearance() { return 'O';} }
import javafx.geometry.Point2D; import javafx.scene.paint.Color; public class GameTestProgram { public static void main(String args[]) { Game g; g = new Game(); // Add some walls  g.add(new Wall(new Point2D(0,0), 10, 200)); g.add(new Wall(new Point2D(10,0), 170, 10)); g.add(new Wall(new Point2D(180,0), 10, 200)); g.add(new Wall(new Point2D(10,190), 170, 10)); g.add(new Wall(new Point2D(80,60), 100, 10)); g.add(new Wall(new Point2D(10,90), 40, 10)); g.add(new Wall(new Point2D(100,100), 10, 50)); // Add some prizes  g.add(new Prize(new Point2D(165,25), 1000)); g.add(new Prize(new Point2D(65,95), 500)); g.add(new Prize(new Point2D(145,165), 750)); // Add some traps  g.add(new Trap(new Point2D(125,35))); g.add(new Trap(new Point2D(145, 145))); // Add some players  //g.add(new Player("Blue Guy", Color.BLUE, new Point2D(38,156), 90));  //g.add(new Player("Yellow Guy", Color.YELLOW, new Point2D(55,37), 270));  //g.add(new Player("Green Guy", Color.GREEN, new Point2D(147,116), 0));   // Add the ball  //g.add(new Ball(new Point2D(90, 90)));   System.out.println("Here are the Game Objects:"); g.displayObjects(); // Test out some Player and Ball movement  System.out.println("--------------------------------------------------------"); Player player = new Player("Red Guy", Color.RED, new Point2D(100,100), 0); player.speed = 10; player.direction = 0; g.add(player); Ball ball = new Ball(new Point2D(100,100)); ball.speed = 10; ball.direction = 0; g.add(ball); // Make some updates  for (int i=0; i// Get the harmful objects  System.out.println(" Here are the Harmful Objects:"); Harmful[] dangerousStuff = g.harmfulObjects(); for (Harmful d: dangerousStuff) System.out.println(" " + d); // Assess the current amount of danger  System.out.println(" Current Danger Assessment:"); System.out.println(g.assessDanger()); } }
import javafx.geometry.Point2D; public abstract class GameObject { protected Point2D location; public GameObject(Point2D loc) { location = loc; } public Point2D getLocation() { return location; } public void setLocation(Point2D newLocation) { location = newLocation; } public abstract void update(); public abstract char appearance(); } 

import javafx.geometry.Point2D; import javafx.scene.paint.Color; public class GameBoardTestProgram { public static void main(String args[]) { Game g = new Game(); // Add some walls  g.add(new Wall(new Point2D(0,0), 20, 1)); g.add(new Wall(new Point2D(0,0), 1, 15)); g.add(new Wall(new Point2D(19,0), 1, 15)); g.add(new Wall(new Point2D(0,14), 20, 1)); g.add(new Wall(new Point2D(1,6), 4, 1)); g.add(new Wall(new Point2D(9,4), 10, 1)); g.add(new Wall(new Point2D(12,7), 1, 4)); // Add some prizes  g.add(new Prize(new Point2D(17,1), 1000)); g.add(new Prize(new Point2D(6,6), 500)); g.add(new Prize(new Point2D(15,12), 750)); // Add some traps  g.add(new Trap(new Point2D(13,2))); g.add(new Trap(new Point2D(15,10))); // Add a Player  Player player = new Player("Red Guy", Color.RED, new Point2D(3, 12), 0); player.speed = 1; player.direction = 90; g.add(player); // Add a Ball  Ball ball = new Ball(new Point2D(3, 11)); ball.speed = 5; ball.direction = 0; g.add(ball); // Make some updates, displaying the board each time ...  for (int i=0; iout.println("--------------------"); } } } 
public class GameBoard { public static final int WIDTH = 20; public static final int HEIGHT = 25; private char[][] grid; public GameBoard() { grid = new char[WIDTH][HEIGHT]; for (int i=0; iWIDTH; i++) for (int j=0; jHEIGHT; j++) grid[i][j] = ' '; } //This method prints out the board  public void display(Game g) { // Erase the stuff at the old player and ball locations  for(int go=0; goif (g.getGameObjects()[go] instanceof MovableObject) { MovableObject m = (MovableObject)g.getGameObjects()[go]; if ((m.getPreviousLocation().getX() WIDTH) && (m.getPreviousLocation().getX() >= 0) && (m.getPreviousLocation().getY() HEIGHT) && (m.getPreviousLocation().getY() >= 0)) grid[(int)m.getPreviousLocation().getX()][(int)m.getPreviousLocation().getY()] = ' '; } } // Draw the game objects on the board now  for(int go=0; goif (g.getGameObjects()[go] instanceof Wall) { Wall w = (Wall)g.getGameObjects()[go]; for (int i=0; ifor (int j=0; jgrid[(int)w.getLocation().getX()+i][(int)w.getLocation().getY()+j] = w.appearance(); } else { if ((g.getGameObjects()[go].getLocation().getX() WIDTH) && (obj.getLocation().getX() >= 0) && (obj.getLocation().getY() HEIGHT) && (obj.getLocation().getY() >= 0)) grid[(int)obj.getLocation().getX()][(int)obj.getLocation().getY()] = obj.appearance(); } } // Now display it  for (int r=0; rHEIGHT; r++) { for (int c=0; cWIDTH; c++) System.out.print(grid[c][r]); System.out.println(); } } } 

import java.lang.*; import java.util.ArrayList; public class Game { public static final int MAX_GAME_OBJECTS = 1000; GameObject[] gameObjects; int objectCount; GameBoard gameBoard; public Game() { gameObjects = new GameObject[MAX_GAME_OBJECTS]; objectCount = 0; gameBoard = new GameBoard(); } public void add(GameObject obj) { if (objectCount MAX_GAME_OBJECTS) gameObjects[objectCount++] = obj; } // The get methods  public GameObject[] getGameObjects() { return gameObjects; } public int getObjectCount() { return objectCount; } public String toString() { return "Game with " + objectCount + " objects"; } public void displayObjects() { for (int i=0; iobjectCount; i++) System.out.println(gameObjects[i]); } public void updateObjects() { for (int i=0; iobjectCount; i++) gameObjects[i].update(); } // Return an array of all Harmful objects in the game  Harmful[] harmfulObjects() { // First find out how many objects are Harmful  int count = 0; for (GameObject g: gameObjects) if (g instanceof Harmful) count++; // Now create the array and fill it up  Harmful[] badGuys = new Harmful[count]; count = 0; for (GameObject g: gameObjects) if (g instanceof Harmful) badGuys[count++] = (Harmful)g; return badGuys; } public int assessDanger() { int harmOutThere = 0; Harmful[] dangerousStuff = harmfulObjects(); for (Harmful d: dangerousStuff) harmOutThere += d.getDamageAmount(); return harmOutThere; } public void displayBoard() { gameBoard.display(this); } }

public class Customer { private int id; private String name; private int age; private char gender; private float money; // A simple constructor  public Customer(String n, int a, char g, float m) { name = n; age = a; gender = g; money = m; id = -1; } // Return a String representation of the object  public String toString() { String result; result = "Customer " + name + ": a " + age + " year old "; if (gender == 'F') result += "fe"; return result + "male with $" + money; } public boolean hasMoreMoneyThan(Customer c) { return money > c.money; } // Get methods  public String getName() { return name; } public int getAge() { return age; } public char getGender() { return gender; } public int getId() { return id; } // Set Methods  public void setId(int newID) { id = newID; } } 
import javafx.geometry.Point2D; public class Wall extends StationaryObject { private int width; private int height; public Wall(Point2D loc, int w, int h) { super(loc); width = w; height = h; } // The get/set methods  public Point2D getLocation() { return location; } public int getWidth() { return width; } public int getHeight() { return height; } public void setLocation(Point2D newLocation) { location = newLocation; } public String toString() { return "Wall" + " at (" + (int)location.getX() + "," + (int)location.getY() + ") with width " + width + " and height " + height; } public char appearance() { return '#'; } }
public interface Harmful { public int getDamageAmount(); } 

Exception in thread "main" CollisionException: Player collided with wal1l! at Game.updateobjects (Game.java:43) at GameBoardTestProgram.main (GameBoardTestProgram.java:40) at com.intellij.rt.execution.application.AppMain.main (AppMain.java:144) Exception in thread "main" CollisionException: Player collided with wal1l! at Game.updateobjects (Game.java:43) at GameBoardTestProgram.main (GameBoardTestProgram.java:40) at com.intellij.rt.execution.application.AppMain.main (AppMain.java:144)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!