I am a beginner Java programmer. I have just finished up an assignment and would appreciate some advice and/or constructive criticism on my program. I am trying to ensure I do not advance my knowledge using bad practices. For context, I have the assignment details, followed by the full code below:
Part 1: Write an abstract Human class that implements the Human interface above. You’ll need to include the interface in your project.
Part 2: Implement the Knight and Wizard classes.
Part 3: Write a test program called LastNameWorld, which:
- Declares 4 Humans
- Instantiates a Knight
- Instantiates a Wizard and moves it
- Clones the Knight and changes its name
- Checks if the Knight clone is not equal to the original Knight, and if so: moves the clone
- Clones the Wizard
- Checks if the Wizard clone is equal to the original Wizard, and if so: moves the clone
- Prints the total number of Humans living in this World
- Prints a description (toString) of all Humans to the console
Part 4: Add a third Human (e.g. Archer, King, Queen, Paladin, etc) type class to your simulation. This class must contain an equals and a clone method. Add code to your test harness to fully test your new class.
Main Class
public class BakosWorld {
public static void main(String[] args) {
// Declare 4 humans
Human[] humans = new Human[4];
// Create original knight and wizard, then move wizard
humans[0] = new Knight("Rob", 36, 100, 6.1, 50, 50, "Grey Wind");
humans[1] = new Wizard("Gandalf", 32, 80, 5.11, 25, 25, 50);
humans[1].move(45, 45);
// Creating a new knight, that is a clone of the first knight
humans[2] = ((Knight) humans[0]).clone();
// Checks to see if the two knights are NOT equal
if (((Knight) humans[0]).equals(((Knight) humans[2])) == false) {
humans[2].move(85, 85);
}
// Creating a new wizard, that is a clone of the first wizard
humans[3] = ((Wizard) humans[1]).clone();
// Checks to see if the two knights ARE equal
if (((Wizard) humans[1]).equals(((Wizard) humans[3]))) {
humans[3].move(60, 60);
}
// Print information
System.out.println("Number of humans: " + Human.getNumHumans());
for (int i = 0; i < humans.length; i++) {
System.out.println(humans[i].toString());
}
// Creating new human, assassin, the clone, and moving the clone
Human arya = new Assassin("Arya", 16, 70, 5.9, 20, 20, 80);
Human aryaClone = ((Assassin) arya).clone();
if (((Assassin) arya).equals((Assassin) aryaClone)) {
aryaClone.move(120, 120);
}
// Gandalf killed (original) Arya's father, so it's time for her to get her revenge
((Assassin) arya).inflictPoison(humans[1]);
System.out.println("\n\n-UPDATED-\n\n" + arya.toString() + "\n"
+ aryaClone.toString() + "\n" + humans[1].toString());
}
}
Interface
public interface HumanInterface {
//return the name of the human
public String getName();
//change the name of the human
public void setName(String name);
//get the age of the human
public int getAge();
//change the age of a human
public void setAge(int a);
//return the height of the human
public double getHeight();
//change the height of a human
public void setHeight(double h);
//return the health of a human
public int getHealth();
//change the health of a human
public void setHealth(int h);
//move a human
public void move(int x, int y);
//get Y position of a human
public int getYPos();
//get X position of a human
public int getXPos();
//return a string representation of a human object
public String toString();
}
Abstract Class
abstract public class Human implements HumanInterface {
protected String name;
protected int age;
protected double height;
protected int health;
protected int xPos;
protected int yPos;
private static int numHumans;
public Human(String n, int a, int hea, double hei, int x, int y) {
name = n;
age = a;
health = hea;
height = hei;
xPos = x;
yPos = y;
numHumans++;
}
public void move(int x, int y) {
xPos = x;
yPos = y;
}
public int getXPos() {
return xPos;
}
public int getYPos() {
return yPos;
}
public void setHealth(int h) {
health = h;
}
public int getHealth() {
return health;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
public void setHeight(double h) {
height = h;
}
public double getHeight() {
return height;
}
public void setNumHumans(int n) {
numHumans = n;
}
public static int getNumHumans() {
return numHumans;
}
// General toString method
public String toString() {
String message;
if (health <= 0) {
message = "\n[HUMAN]"
+ "\n" + name + " is dead.";
} else {
message = "xPos: " + xPos
+ "\nyPos: " + yPos
+ "\nName: " + name
+ "\nAge: " + age
+ "\nHealth: " + health
+ "\nHeight: " + height;
}
return message;
}
}
Sub-Class: Knight
public class Knight extends Human {
private String horseName;
public Knight(String n, int a, int hea, double hei, int xPos, int yPos, String hN) {
super(n, a, hea, hei, xPos, yPos);
horseName = hN;
}
public void setHorseName(String hN) {
hN = horseName;
}
public String getHorseName() {
return horseName;
}
// Clone the object
public Knight clone() {
Knight theClone;
theClone = new Knight(this.name, this.age, this.health, this.height,
this.xPos, this.yPos, horseName);
return theClone;
}
// Check to see if 2 objects are the same
public boolean equals(Knight k) {
return k.age == this.age
&& k.name.equals(this.name)
&& k.health == this.health
&& k.height == this.height
&& k.xPos == this.xPos
&& k.yPos == this.yPos
&& k.horseName.equals(this.horseName);
}
// Knight toString method
public String toString() {
String message;
if (health <= 0) {
message = "\n[KNIGHT]"
+ "\n" + name + " is dead.";
} else {
message = "\n[KNIGHT]"
+ "\nxPos: " + xPos
+ "\nyPos: " + yPos
+ "\nName: " + name
+ "\nAge: " + age
+ "\nHealth: " + health
+ "\nHorse Name: " + horseName
+ "\nHeight: " + height;
}
return message;
}
}
Sub-Class: Wizard
public class Wizard extends Human {
private int magicka;
public Wizard(String n, int a, int hea, double hei, int x, int y, int m) {
super(n, a, hea, hei, x, y);
magicka = m;
}
public void castSpell() {
}
public void setMagicka(int m) {
magicka = m;
}
public int getMagicka() {
return magicka;
}
// Clone the object
public Wizard clone() {
Wizard theClone;
theClone = new Wizard(this.name, this.age, this.health, this.height,
this.xPos, this.yPos, magicka);
return theClone;
}
// Check to see if 2 objects are the same
public boolean equals(Wizard w) {
return w.age == this.age
&& w.name.equals(this.name)
&& w.health == this.health
&& w.height == this.height
&& w.xPos == this.xPos
&& w.yPos == this.yPos
&& w.magicka == this.magicka;
}
// Wizard toString method
public String toString() {
String message;
if (health <= 0) {
message = "\n[WIZARD]"
+ "\n" + name + " is dead.";
} else {
message = "\n[WIZARD]"
+ "\nxPos: " + xPos
+ "\nyPos: " + yPos
+ "\nName: " + name
+ "\nAge: " + age
+ "\nHealth: " + health
+ "\nMagicka: " + magicka
+ "\nHeight: " + height;
}
return message;
}
}
Sub-Class: Assassin
public class Assassin extends Human {
int poison;
public Assassin(String n, int a, int hea, double hei, int x, int y, int p) {
super(n, a, hea, hei, x, y);
poison = p;
}
public void inflictPoison(Human h) {
h.setHealth(0);
poison = this.poison - 20;
}
public void setPoison(int p) {
poison = p;
}
public int getPoison() {
return poison;
}
// Clone the object
public Assassin clone() {
Assassin theClone;
theClone = new Assassin(this.name, this.age, this.health, this.height,
this.xPos, this.yPos, poison);
return theClone;
}
// Check to see if 2 objects are the same
public boolean equals(Assassin a) {
return a.age == this.age
&& a.name.equals(this.name)
&& a.health == this.health
&& a.height == this.height
&& a.xPos == this.xPos
&& a.yPos == this.yPos
&& a.poison == this.poison;
}
// Assassin toString method
public String toString() {
String message;
if (health <= 0) {
message = "\n[ASSASSIN]"
+ "\n" + name + " is dead.";
} else {
message = "\n[ASSASSIN]"
+ "\nxPos: " + xPos
+ "\nyPos: " + yPos
+ "\nName: " + name
+ "\nAge: " + age
+ "\nHealth: " + health
+ "\nPoison: " + poison
+ "\nHeight: " + height;
}
return message;
}
}