Answered step by step
Verified Expert Solution
Question
1 Approved Answer
java import javafx.geometry.Point2D; public class Wall extends StationaryObject { private int width ; private int height ; public Wall(Point2D loc, int w, int h) {
java
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 '#?} }
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 ArrayListgetPlayers() { 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; } }
import javafx.geometry.Point2D; import javafx.scene.paint.Color; public class Player extends MovableObject { private String name; private Color color; private boolean hasBall; private int score; public Player(String n, Color c, Point2D loc, int dir) { super(dir, 0, loc); name = n; color = c; hasBall = false; score = 0; } // The get/set methods public int getDirection() { return direction; } public int getSpeed() { return speed; } public String getName() { return name; } public Color getColor() { return color; } public boolean hasBall() { return hasBall; } public int getScore() { return score; } public void setDirection(int newDirection) { direction = newDirection; } public void setSpeed(int newSpeed) { speed = newSpeed; } public void setHasBall(boolean newHasBall) { hasBall = newHasBall; } public void setScore(int newScore) { score = newScore; } public String toString() { String s = "Player" + " " + name + " at (" + (int)location.getX() + "," + (int)location.getY() + ") facing " + direction + " degrees"; if (hasBall) s += " with the ball"; return s; } public void draw() { //System.out.println("Player is at (" + (int)location.getX() + "," + (int)location.getY() + ") facing " + direction + " degrees and moving at " + speed + " pixels per second"); } public char appearance() { switch(direction) { case 0: return '>'; case 90: return '^'; case 180: return '; case 270: return 'V'; default: return '.'; } } }
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; 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("--------------------"); } } }
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; public abstract class MovableObject extends GameObject { protected Point2D previousLocation; protected int direction; protected int speed; public abstract void draw(); public void setLocation(Point2D newLocation) { previousLocation = location; location = newLocation; } public MovableObject(int d, int s, Point2D loc) { super(loc); previousLocation = location; direction = d; speed = s; } public void update() { moveForward(); draw(); } public void moveForward() { if (speed > 0) { previousLocation = location; location = location.add((int) (Math.cos(Math.toRadians(direction)) * speed), -(int) (Math.sin(Math.toRadians(direction)) * speed)); } } public Point2D getPreviousLocation() { return previousLocation; } }
public class Store { public static final int MAX_CUSTOMERS = 500; public static int LATEST_ID = 1000000; private String name; private Customer[] customers; private int customerCount; public Store(String n) { name = n; customers = new Customer[MAX_CUSTOMERS]; customerCount = 0; } public void addCustomer(Customer c) { if (customerCount MAX_CUSTOMERS) { customers[customerCount++] = c; c.setId(LATEST_ID++); } } public Customer[] getCustomers() { return customers; } public int getCustomerCount() { return customerCount; } public void listCustomers() { for (int i=0; icustomerCount; i++) System.out.println(customers[i]); } public int averageCustomerAge() { int avg = 0; for (int i=0; icustomerCount; i++) { avg += customers[i].getAge(); } return avg / customerCount; } public char mostPopularGender() { int mCount = 0, fCount = 0; for (int i=0; icustomerCount; i++) { if (customers[i].getGender() == 'M') mCount++; else fCount++; } if (mCount > fCount) return 'M'; return 'F'; } public Customer richestCustomer() { Customer richest = customers[0]; for (int i=1; icustomerCount; i++) { if (customers[i].hasMoreMoneyThan(richest)) richest = customers[i]; } return richest; } public Customer[] getCustomersWithGender(char g) { int matchCount = 0; // Count them first for (int i=0; icustomerCount; i++) { if (customers[i].getGender() == g) matchCount++; } // Now make the array Customer[] matches = new Customer[matchCount]; matchCount = 0; for (int i=0; icustomerCount; i++) { if (customers[i].getGender() == g) matches[matchCount++] = customers[i]; } return matches; } public Customer[] friendsFor(Customer lonelyCustomer) { int friendCount = 0; // Count them first for (int i=0; icustomerCount; i++) { if ((customers[i] != lonelyCustomer) && (customers[i].getGender() == lonelyCustomer.getGender()) && (Math.abs(customers[i].getAge() - lonelyCustomer.getAge()) // Now make the array Customer[] friends = new Customer[friendCount]; friendCount = 0; for (int i=0; icustomerCount; i++) { if ((customers[i] != lonelyCustomer) && (customers[i].getGender() == lonelyCustomer.getGender()) && (Math.abs(customers[i].getAge() - lonelyCustomer.getAge()) customers[i]; } } return friends; } }
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; } }
public class StoreTestProgram { public static void main(String args[]) { Customer[] result; Store walmart; walmart = new Store("Walmart off Innes"); walmart.addCustomer(new Customer("Amie", 14, 'F', 100)); walmart.addCustomer(new Customer("Brad", 15, 'M', 0)); walmart.addCustomer(new Customer("Cory", 10, 'M', 100)); walmart.addCustomer(new Customer("Dave", 5, 'M', 48)); walmart.addCustomer(new Customer("Earl", 21, 'M', 500)); walmart.addCustomer(new Customer("Flem", 18, 'M', 1)); walmart.addCustomer(new Customer("Gary", 8, 'M', 20)); walmart.addCustomer(new Customer("Hugh", 65, 'M', 30)); walmart.addCustomer(new Customer("Iggy", 43, 'M', 74)); walmart.addCustomer(new Customer("Joan", 55, 'F', 32)); walmart.addCustomer(new Customer("Kyle", 16, 'M', 88)); walmart.addCustomer(new Customer("Lore", 12, 'F', 1000)); walmart.addCustomer(new Customer("Mary", 17, 'F', 6)); walmart.addCustomer(new Customer("Nick", 13, 'M', 2)); walmart.addCustomer(new Customer("Omar", 18, 'M', 24)); walmart.addCustomer(new Customer("Patt", 24, 'F', 45)); walmart.addCustomer(new Customer("Quin", 42, 'M', 355)); walmart.addCustomer(new Customer("Ruth", 45, 'F', 119)); walmart.addCustomer(new Customer("Snow", 74, 'F', 20)); walmart.addCustomer(new Customer("Tamy", 88, 'F', 25)); walmart.addCustomer(new Customer("Ulsa", 2, 'F', 75)); walmart.addCustomer(new Customer("Vern", 9, 'M', 90)); walmart.addCustomer(new Customer("Will", 11, 'M', 220)); walmart.addCustomer(new Customer("Xeon", 17, 'F', 453)); walmart.addCustomer(new Customer("Ying", 19, 'F', 76)); walmart.addCustomer(new Customer("Zack", 22, 'M', 35)); System.out.println("Here are the customers: "); walmart.listCustomers(); // Find average Customer age System.out.println(" Average age of customers: " + walmart.averageCustomerAge()); // Find most popular gender System.out.println(" Most popular gender is: " + walmart.mostPopularGender()); // Find richest customer System.out.println(" Richest customer is: " + walmart.richestCustomer()); // Find male customers System.out.println(" Here are all the male customers:"); result = walmart.getCustomersWithGender('M'); for (Customer c: result) System.out.println(c); // Find female customers System.out.println(" Here are all the female customers:"); result = walmart.getCustomersWithGender('F'); for (Customer c: result) System.out.println(c); // Find friends for Amie System.out.println(" Friends for 14 year old female Amie:"); result = walmart.friendsFor(walmart.getCustomers()[0]); for (Customer c: result) System.out.println(c); // Find friends for Brad System.out.println(" Friends for 15 year old male Brad:"); result = walmart.friendsFor(walmart.getCustomers()[1]); for (Customer c: result) System.out.println(c); } }
Tutorial Problems Harmful Object Recall these classes .GameObject GameObject Game MovableObject Player Ball I MovableObject StationaryObject .StationaryObject Wall ra Prize Game Player Ball Wall Trap Prize . GameTestProgram GameBoard GameBoardTestProgram 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 /7 (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: Tutorial Problems Harmful Object Recall these classes .GameObject GameObject Game MovableObject Player Ball I MovableObject StationaryObject .StationaryObject Wall ra Prize Game Player Ball Wall Trap Prize . GameTestProgram GameBoard GameBoardTestProgram 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 /7 (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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started