Question

In: Computer Science

JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A...

JAVA programming Classwork- please answer all prompts as apart of one java programming project

Part A

Add to your project this class Position, which has x and y coordinates.

Create an abstract class GameElt, which has a String name, an int health (keep it in the range 0 to 100) and a Position pos.

For GameElt, include a default constructor that starts pos at (0, 0), and a parameterized constructor that takes x and y coordinates and a name. health should always start at 100.

Create a class BigDarnHero which inherits from GameElt. Override toString so that when we print a hero, we see something like "Thondar the Hero (3, 17)" based on the name and the position. Other than toString and two constructors taking the same parameters as in GameElt, you do not need to add anything else for this class at this time.

Part B

☑ Create an interface MoveStrategy which requires methods

  • void move(Position p, String direction)
  • boolean chanceToFall()

The idea is that moving in a given direction will change the given position, and chanceToFall will return true if a fall happened or false otherwise. How this happens will depend on the implementation in the actual classes.

Moving North or South will change the Y coordinate (vertical movement) moving East or West will change the X coordinate (horizontal movement). Direction will be passed as "N", "S", "E", or "W".

☑ Write a class WalkMoveStrategy which implements MoveStrategy. When walking, the position changes by 1 in the direction chosen, and a message like "Walking from (7,1) to (7, 2)." should also be printed out.

People who are walking have a 1 in 20 chance of falling. In chanceToFall choose a random number and use it to determine if chanceToFall returns true.

You do not need to add anything other than the methods to implement the strategy, not even a constructor

☑ Change GameElt so that it also has an instance variable moveStrat of type MoveStrategy.

Add to GameElt a method move(direction) which calls moveStrat's move method, passing it the direction given and GameElt's pos instance variable. Then call chanceToFall and if the GameElt fell while moving, print a statement about this and reduce health by 5.

☑ In all constructors for BigDarnHero, set the MoveStrategy to a new instance of WalkMoveStrategy.

☑ In a main program, create a BigDarnHero named "Mighty Thog" at (5, 3) and have them walk three moves north and one west. Print the hero before they move, and again after they have moved.

Part C

☑ Add another class RunMoveStrategy which implements MoveStrategy, and whose move method changes the position by 5 in the direction given, as well as printing something like "Running from (9, 3) to (4, 3). Boy my mighty thews are tired."

People who run have a 1 in 10 chance of falling.

☑ To BigDarnHero, add a method speedUp() which changes the hero's MoveStrategy to a RunMoveStrategy, and a method slowDown which changes the MoveStrategy to a WalkMoveStrategy.

☑ In the main program, have Mighty Thog move around, speed up, move around, slow down, and move around again.

Part D

☑ Add another strategy for movement, RandomCursedMoveStrategy, which changes the position by a random amount in a random direction, no matter what direction is passed in, and prints out something like "Truly, I am accursed and shall never get to class on time".

People moving by this strategy have a 50/50 chance of falling and hurting themselves.

In the main program, create a hero, set this as their movement strategy, and add some movement for them.

Solutions

Expert Solution

Below are your codes. As it was a really long question, I have tried providing everything. If you think I am missing something, Please let me know in comments I'll surely help

PART A

Position.java

public class Position {
        private int x;
        private int y;

        public Position(int x, int y) {
                this.x = x;
                this.y = y;
        }

        public Position(Position obj) {
                this.x = obj.x;
                this.y = obj.y;
        }

        public int getX() {
                return x;
        }

        public void setX(int x) {
                this.x = x;
        }

        public int getY() {
                return y;
        }

        public void setY(int y) {
                this.y = y;
        }

        public String toString() {
                return "(" + x + "," + y + ")";
        }
}

GameElt.java

public abstract class GameElt {
        private String name;
        private int health;
        private Position pos;

        public GameElt(int x, int y, String name) {
                this.name = name;
                this.pos = new Position(x, y);
                this.health = 100;
        }

        public GameElt() {
                this.pos = new Position(0, 0);
                this.name = "";
                this.health = 100;
        }

        public Position getPos() {
                return pos;
        }

        public void setPos(Position pos) {
                this.pos = pos;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getHealth() {
                return health;
        }

        public void setHealth(int health) {
                if (health < 0) {
                        this.health = 0;
                } else if (health > 100) {
                        this.health = 100;
                } else {
                        this.health = health;
                }
        }

}

BigDarnHero.java

public class BigDarnHero extends GameElt {

        public BigDarnHero(int x, int y, String name) {
                super(x, y, name);
        }

        public BigDarnHero() {
                super();
        }

        public String toString() {
                return this.getName() + " the Hero " + this.getPos();
        }
}

PART B

GameElt.java

public abstract class GameElt {
        private String name;
        private int health;
        private Position pos;
        public MoveStrategy moveStrat;

        public GameElt(int x, int y, String name) {
                this.name = name;
                this.pos = new Position(x, y);
                this.health = 100;
        }

        public GameElt() {
                this.pos = new Position(0, 0);
                this.name = "";
                this.health = 100;
        }

        public Position getPos() {
                return pos;
        }

        public void setPos(Position pos) {
                this.pos = pos;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getHealth() {
                return health;
        }

        public void setHealth(int health) {
                if (health < 0) {
                        this.health = 0;
                } else if (health > 100) {
                        this.health = 100;
                } else {
                        this.health = health;
                }
        }

        public void move(String direction) {
                this.moveStrat.move(this.pos, direction);
                if (this.moveStrat.chanceToFall()) {
                        System.out.println("Fall Happened. ");
                        this.setHealth(this.getHealth() - 5);
                }
        }

}

BigDarnHero.java

public class BigDarnHero extends GameElt {

        public BigDarnHero(int x, int y, String name) {
                super(x, y, name);
                this.moveStrat = new WalkMoveStrategy();
        }

        public BigDarnHero() {
                super();
                this.moveStrat = new WalkMoveStrategy();
        }

        public String toString() {
                return this.getName() + " the Hero " + this.getPos();
        }
}

MoveStrategy.java

public interface MoveStrategy {
        void move(Position p, String direction);

        boolean chanceToFall();
}

WalkMoveStrategy.java

import java.util.Random;

public class WalkMoveStrategy implements MoveStrategy {

        @Override
        public void move(Position p, String direction) {
                Position copy = new Position(p);
                switch (direction) {
                case "N":
                case "S":
                        p.setY(p.getY() + 1);
                        break;
                case "E":
                case "W":
                        p.setX(p.getX() + 1);
                        break;
                default:
                        break;
                }
                System.out.println("Walking from " + copy + " to " + p);
        }

        @Override
        public boolean chanceToFall() {
                Random rand = new Random();
                int num = rand.nextInt(100 - 1 + 1) + 1;
                if (num <= 5) {
                        return true;
                } else {
                        return false;
                }
        }

}

Main.java


public class Main {
        public static void main(String[] args){
                BigDarnHero  hero = new BigDarnHero(5, 3, "Mighty Thog");
                System.out.println(hero);
                hero.move("N");
                System.out.println(hero);
                hero.move("N");
                System.out.println(hero);
                hero.move("N");
                System.out.println(hero);
                hero.move("W");
                System.out.println(hero);
        }
}

Output

Mighty Thog the Hero (5,3)
Walking from (5,3) to (5,4)
Mighty Thog the Hero (5,4)
Walking from (5,4) to (5,5)
Mighty Thog the Hero (5,5)
Walking from (5,5) to (5,6)
Mighty Thog the Hero (5,6)
Walking from (5,6) to (6,6)
Mighty Thog the Hero (6,6)

PART C

RunMoveStrategy.java

import java.util.Random;

public class RunMoveStrategy implements MoveStrategy {

        @Override
        public void move(Position p, String direction) {
                Position copy = new Position(p);
                switch (direction) {
                case "N":
                case "S":
                        p.setY(p.getY() + 5);
                        break;
                case "E":
                case "W":
                        p.setX(p.getX() + 5);
                        break;
                default:
                        break;
                }
                System.out.println("Running from " + copy + " to " + p
                                + ". Boy my mighty thews are tired.");
        }

        @Override
        public boolean chanceToFall() {
                Random rand = new Random();
                int num = rand.nextInt(100 - 1 + 1) + 1;
                if (num <= 10) {
                        return true;
                } else {
                        return false;
                }
        }

}

BigDarnHero.java

public class BigDarnHero extends GameElt {

        public BigDarnHero(int x, int y, String name) {
                super(x, y, name);
                this.moveStrat = new WalkMoveStrategy();
        }

        public BigDarnHero() {
                super();
                this.moveStrat = new WalkMoveStrategy();
        }

        public String toString() {
                return this.getName() + " the Hero " + this.getPos();
        }
        
        public void speedUp() {
                this.moveStrat = new RunMoveStrategy();
        }
        public void slowDown() {
                this.moveStrat = new WalkMoveStrategy();
        }
}

Main.java


public class Main {
        public static void main(String[] args){
                BigDarnHero  hero = new BigDarnHero(5, 3, "Mighty Thog");
                System.out.println(hero);
                hero.move("N");
                System.out.println(hero);
                hero.speedUp();
                hero.move("N");
                System.out.println(hero);
                hero.move("N");
                System.out.println(hero);
                hero.slowDown();
                hero.move("W");
                System.out.println(hero);
        }
}

Output

Mighty Thog the Hero (5,3)
Walking from (5,3) to (5,4)
Mighty Thog the Hero (5,4)
Running from (5,4) to (5,9). Boy my mighty thews are tired.
Mighty Thog the Hero (5,9)
Running from (5,9) to (5,14). Boy my mighty thews are tired.
Mighty Thog the Hero (5,14)
Walking from (5,14) to (6,14)
Mighty Thog the Hero (6,14)

PART D

RandomCursedMoveStrategy.java

import java.util.Random;

public class RandomCursedMoveStrategy implements MoveStrategy {

        @Override
        public void move(Position p, String direction) {
                Random rand = new Random();
                int randDirection = rand.nextInt(4 - 1 + 1) + 1;
                int randAmount = rand.nextInt(100 - 1 + 1) + 1;
                switch (randDirection) {
                case 1:
                case 4:
                        p.setY(p.getY() + randAmount);
                        break;
                case 2:
                case 3:
                        p.setX(p.getX() + randAmount);
                        break;
                default:
                        break;
                }
                System.out
                                .println("Truly, I am accursed and shall never get to class on time");
        }

        @Override
        public boolean chanceToFall() {
                Random rand = new Random();
                int num = rand.nextInt(100 - 1 + 1) + 1;
                if (num <= 50) {
                        return true;
                } else {
                        return false;
                }
        }

}

GameElt.java

public abstract class GameElt {
        private String name;
        private int health;
        private Position pos;
        public MoveStrategy moveStrat;

        public GameElt(int x, int y, String name) {
                this.name = name;
                this.pos = new Position(x, y);
                this.health = 100;
        }

        public GameElt() {
                this.pos = new Position(0, 0);
                this.name = "";
                this.health = 100;
        }

        public Position getPos() {
                return pos;
        }

        public void setPos(Position pos) {
                this.pos = pos;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getHealth() {
                return health;
        }

        public MoveStrategy getMoveStrat() {
                return moveStrat;
        }

        public void setMoveStrat(MoveStrategy moveStrat) {
                this.moveStrat = moveStrat;
        }

        public void setHealth(int health) {
                if (health < 0) {
                        this.health = 0;
                } else if (health > 100) {
                        this.health = 100;
                } else {
                        this.health = health;
                }
        }

        public void move(String direction) {
                this.moveStrat.move(this.pos, direction);
                if (this.moveStrat.chanceToFall()) {
                        System.out.println("Fall Happened. ");
                        this.setHealth(this.getHealth() - 5);
                }
        }

}

Main.java


public class Main {
        public static void main(String[] args){
                BigDarnHero  hero = new BigDarnHero(5, 3, "Mighty Thog");
                hero.setMoveStrat(new RandomCursedMoveStrategy());
                System.out.println(hero);
                hero.move("N");
                System.out.println(hero);
                hero.move("S");
                System.out.println(hero);
                hero.move("E");
                System.out.println(hero);
                hero.move("W");
                System.out.println(hero);
        }
}

Output

Mighty Thog the Hero (5,3)
Truly, I am accursed and shall never get to class on time
Mighty Thog the Hero (99,3)
Truly, I am accursed and shall never get to class on time
Mighty Thog the Hero (99,29)
Truly, I am accursed and shall never get to class on time
Fall Happened.
Mighty Thog the Hero (171,29)
Truly, I am accursed and shall never get to class on time
Mighty Thog the Hero (221,29)


Related Solutions

JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
JAVA programming- please answer all prompts as apart of the same java project Enums enumeration values()...
JAVA programming- please answer all prompts as apart of the same java project Enums enumeration values() and other utilities Part A Create an enumeration AptType representing several different types of apartment. Each type of apartment has a description, a size (square footage), and a rent amount. In the enumeration, provide public final instance variables for these. Also write a parameterized constructor that sets the values of these variables (doing this is just part of seeing up the enum) The apartment...
JAVA code - please answer all prompts as apart of the same java program with Polymorphism...
JAVA code - please answer all prompts as apart of the same java program with Polymorphism Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
Java Part 2 of 4 - Amusement Park Programming Project MUST BE COMPATIBLE WITH PART 1...
Java Part 2 of 4 - Amusement Park Programming Project MUST BE COMPATIBLE WITH PART 1 https://www.chegg.com/homework-help/questions-and-answers/java-part-1-4-amusement-park-programming-project-requirements-use-java-selection-construct-q40170145?trackid=ERIFssNL Requirements: Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Proper error handling. Class: Merchandise – models merchandise available in the gift shop such as t-shirts, sweatshirts, and stuffed animals. Instance Fields: id : long – to identify the specific merchandise item. category : String – to...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs...
Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Proper error handling. Class: Ticket – models admission tickets. Instance Fields: number : long – identify the unique ticket. category : String – store the category of the ticket. holder : String – store the name of the person who purchased the ticket. date...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT