Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I implement this interface into the following code. Here is new Code: GameObject.java import javafx.geometry.Point2D; public abstract class GameObject { protected Point2D location;

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

How can I implement this interface into the following code.

Here is new Code:

GameObject.java

import javafx.geometry.Point2D; public abstract class GameObject { protected Point2D location; public GameObject(Point2D location) { this.location = location; } //New abstract method that must override by child class. public abstract void update(); } 

MovableObject.java

import javafx.geometry.Point2D; //Abstract class that is child class of GameObject. public abstract class MovableObject extends GameObject { protected int direction; protected int speed; public MovableObject(int d, int s, Point2D loc) { //Calling constructor of super class i.e. GameObject(loc) super(loc); direction = d; speed = s; } //Override method from GameObject @Override public void update() { moveForward(); draw(); } public void moveForward() { if (speed > 0) { location = location.add( (int) (Math.cos(Math.toRadians(direction)) * speed), (int) (Math.sin(Math.toRadians(direction)) * speed) ); } } //abstract method that must be override by child class public abstract void draw(); } 

StationaryObject.java

import javafx.geometry.Point2D; public abstract class StationaryObject extends GameObject { public StationaryObject(Point2D location) { //Override its parent class. super(location); } //override update() method of parent class. @Override public void update() { } } 

Player.java

import javafx.geometry.Point2D; import javafx.scene.paint.Color; public class Player extends MovableObject { //Moved speed, direction to MovableObject class. private String name; private Color color; private boolean hasBall; private int score; public Player(String n, Color c, Point2D loc, int dir) { //Initialize constructor of super class i.e. MovableObject super(dir, 0, loc); name = n; color = c; hasBall = false; score = 0; } public String getName() { return name; } public Color getColor() { return color; } public boolean hasBall() { return hasBall; } public int getScore() { return score; } 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; } //Override draw method. @Override public void draw() { System.out.println(String.format("Player is at (%d,%d) facing %d degrees and moving at %d pixels per second.", (int) location.getX(), (int) location.getY(), direction, speed)); } } 

Ball.java

import javafx.geometry.Point2D; public class Ball extends MovableObject { private boolean isBeingHeld; //Moved speed, direction to MovableObject class. public Ball(Point2D loc) { //Initialize constructor of super class i.e. MovableObject super(0, 0, loc); isBeingHeld = false; } // The get/set methods public boolean isBeingHeld() { return isBeingHeld; } 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(String.format("Ball is at (%d,%d) facing %d degrees and moving at %d pixels per second.", (int) location.getX(), (int) location.getY(), direction, speed)); } //Override method update, that is optional to override. //but its require for decrease speed. @Override public void update() { super.update(); //check if speed is more than 0 i.e. 1 or more. //then decrease it by -1. if (speed > 0) speed -= 1; } } 

Prize.java

import javafx.geometry.Point2D; //Prize, Trap, Wall now inherited StationaryObject. //Now these class have all method and attributes that defined in StationaryObject //and StationaryObject's parent class (expect private method/attributes.). public class Prize extends StationaryObject { private int value; public Prize(Point2D loc, int val) { super(loc); value = val; } // The get/set methods public int getValue() { return value; } public String toString() { return "Prize" + " at (" + (int) location.getX() + "," + (int) location.getY() + ") with value " + value; } } 

Trap.java

import javafx.geometry.Point2D; public class Trap extends StationaryObject { public Trap(Point2D loc) { super(loc); } // The get method public String toString() { return "Trap" + " at (" + (int) location.getX() + "," + (int) location.getY() + ")"; } } 

Wall.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 int getWidth() { return width; } public int getHeight() { return height; } public String toString() { return "Wall" + " at (" + (int) location.getX() + "," + (int) location.getY() + ") with width " + width + " and height " + height; } } 

Game.java

public class Game { public static final int MAX_GAME_OBJECTS = 1000; //Changed from Object to game object. GameObject[] gameObjects; int objectCount; public Game() { //Changed from Object to GameObject gameObjects = new GameObject[MAX_GAME_OBJECTS]; objectCount = 0; } public void add(GameObject obj) { if (objectCount MAX_GAME_OBJECTS) gameObjects[objectCount++] = obj; } // The get methods public Object[] getGameObjects() { return gameObjects; } public int getObjectCount() { return objectCount; } public String toString() { return "Game with " + objectCount + " objects"; } public void displayObjects() { for (int i = 0; i out.println(gameObjects[i]); } //Added new updateObjects() methods. public void updateObjects() { for (int i = 0; i  

GameTestProgram.java

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(); //-------------Change in code. for (int i=0;iRED,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); player.update(); player.update(); player.update(); ball.update(); ball.update(); ball.update(); } }
8) Now we will add functionality to allow some game objects (i.e., Ball and Trap) to be harmful to the players of the game. We will do this by making use of a JAVA interface definition called Harmful. This interface will be implemented by all objects that are harmful to the players. That means, we will be able to ask an object how harmful it actually is by asking for its damage amount so that we can update our score. Harmful Object Game Object Game MovableObject StationaryObject Player Ball Wall Trap Prize A. Create an interface called Harmful. To do this, create a new Java class as you normally would, but replace the 'class' keyword with 'interface'. Add the method signature for the getDamageAmount method, which should return an int. Your Harmful interface code should look like: public interface Harmful { public int get DamageAmount (); } Create New Class Name: Harmful 11 Kind: Class Class 1 Interface E Enum @ Annotation JavaFXApplication Singleton 9 COMP 1006/1406B - Winter 2021 - Tutorial #4/#5 Due Monday, February 22nd, 11:59pm B. Add a minor change to the Ball and Trap class definitions so that they both implement this interface (i.e., add implements Harmful to the class definition). Once you do this, these two classes will not compile. That's because in order to implement an interface, a class must have code for the methods in that interface. The Ball and Trap classes are now forced to write a method called getDamage Amount() and your code will not compile until you do so. C. Write a getDamage Amount() method in the Ball class so that balls cause a -200 damage amount (i.e., just return -200 when asked for the damage amount) and in the Trap class so that traps have only a -50 damage amount. You must put the word public before the method's return type, since interfaces require us to write publicly accessible methods. D. Write the following method in the Game class which returns an array of all objects that implement the Harmful interface: // Return an array of all Harmful objects in the game public 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[] bad = new Harmful [count]; count = 0; for (GameObject g: gameObjects) if (g instanceof Harmful) bad[count++] = (Harmful) g; return bad; } Examine the code to make sure that you understand it. The one aspect of the code that should be new to you is the type-casting of g to (Harmful) when we add it to the bad list. This is necessary in order for the compiler to allow you to put general Game Objects into a list that expects only Harmful objects. What we are trying to do is valid because we just checked (by means of the instanceof) that the object was indeed Harmful, but the compiler does not realize that we did this "check" on the previous line. E. Add the following to the end of your Game TestProgram class and compile/run it: E. Add the following to the end of your Game Test Program class and compile/run it: // Get the harmful objects System.out.println(" Here are the Harmful Objects:"); Harmful[] dangerous Stuff = g.harmfulObjects(); for (Harmful d: dangerous Stuff) System.out.println(" " + d); You should see the following: Here are the Harmful Objects: Trap at (125, 35) Trap at (145, 145) Ball at (155,100) facing 0 degrees going 0 pixels per second 10 COMP 1006/1406B - Winter 2021 - Tutorial #4/#5 Due Monday, February 22nd, 11:59pm Now that we have the harmful objects, we can search through them and ask each one of them what damage amount they have. F. Write a method in the Game class called assessDanger() that uses the harmfulObjects() method that you just wrote and returns the integer total sum of damage amounts for all the harmful objects. When you write the code that iterates through the harmful objects, you should notice that you do not need to know what kind of actual object it is (i.e., Trap or Ball). Instead, you just need to call the getDamage Amount() method for the object. F. Write a method in the Game class called assessDanger() that uses the harmfulObjects() method that you just wrote and returns the integer total sum of damage amounts for all the harmful objects. When you write the code that iterates through the harmful objects, you should notice that you do not need to know what kind of actual object it is (i.e., Trap or Ball). Instead, you just need to call the getDamage Amount() method for the object. Interestingly, you will notice as well that we did not create a damageAmount attribute for these Harmful objects. We could not create such a shared attribute without altering the class hierarchy. Why? Ask a friend and/or a TA if you do not know. Of course, by having the getDamage Amount() method, which returns an appropriate value, we kind of "fake" having such an attribute. G. Add the following to the end of your Game TestProgram class and compile/run it: // Assess the current amount of danger System.out.println(" Current Danger Assessment:"); System.out.println(g.assess Danger()); You should see a value of -300 (from the one ball and two traps in the game). 9) Look through the toString() methods in the classes and make use of inheritance by eliminating duplicate code. You should notice that all GameObjects have a similar portion of their toString() method in common (i.e., they all display their class name and location) and that all MovableObjects also have toString() code in common that shows the direction that they are facing as well as their speed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is operatiing system?

Answered: 1 week ago