In: Computer Science
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
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.
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)