In: Computer Science
In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We will be creating a base class for our game character. The base class will be LifeForm. We will have derived classes of Human, Dragon, and Unicorn.
LifeForm will have the following attributes:
You will need to provide a default constructor that initializes these attributes as follows:
You will need to provide a getter and setter for each of these variables. When the attribute has a range, the setter method should not allow a value outside that range. For example, if the setHitPoints(points) is called with points = -1, the attribute should NOT be changed. The variables should be private. Please provide a toString method which will return a String containing the values in these variables.
In your Human, Dragon and Unicorn classes you will need variables to hold the character name, weapon, and magic values. Values will be as follow:
Weapon |
Magic Amount |
|
Human |
Sword or Dagger |
0-50 |
Dragon |
Fire or Ice |
0-100 |
Unicorn |
Horn or Charm |
100-500 |
You will need to provide getters and setters for each of these private variables. Again, they should not allow values not in the table. You also need to provide a constructor for all of the variables, both the ones in the derived class and the ones in the base class.
Please provide a toString method that returns the type of character, the name, the weapon, the magic amount and the values from base class, call the toString() from the base class.
Create a driver class that allows the user to create a random set of characters. These should be of type Human, Dragon and Unicorn. These should be stored in an ArrayList of LIfeForm. You should prompt the user for entries and create three characters of each class type and store them in the ArrayList. Once you have added the three characters. Print the characters from the ArrayList using your toString methods.
Example output: (Italics indicate user input)
Enter Lifeform 1:
Enter Lifeform Type: human
Enter Lifeform Name: Jon Snow
Enter hit points: 10
Enter strength: 5
Enter weapon: sword
Enter magic: 25
Enter Lifeform 2:
Enter Lifeform Type: unicorn
Enter Lifeform Name: Sparkles
Enter hit points: 0
Enter strength: 18
Enter weapon: charm
Enter magic: 300
Enter Lifeform Type: dragon
Enter Lifeform Name: Smaug
Enter hit points: 0
Enter strength: 18
Enter weapon: ice
Enter magic: 99
The available life forms are: ↵
Lifeform [hitPoints=10, strength=5, type=human]Human [name=jon snow, weapon=sword, magic=25]↵
Lifeform [hitPoints=0, strength=18, type=unicorn]Unicorn [name=sparkles, weapon=charm,magic=300]↵
Lifeform [hitPoints=0, strength=18, type=dragon]Dragon [name=smaug, weapon=ice, magic=99]↵
Grading Criteria (total 100 points):
Follows coding standards |
10 |
Properly inputs all data |
20 |
Properly creates minimum of 4 separate files using inheritance correctly |
20 |
Properly creates ArrayList of Lifeform |
15 |
Properly uses polymorphism to call toString from the ArrayList |
20 |
Outputs are neat and easily read |
15 |
//LifeForm.java
public class LifeForm {
private int hitPoints;
private int strength ;
public LifeForm()
{
hitPoints = 100;
strength = 15;
}
public void setHitPoints(int hitPoints)
{
if(hitPoints >= 0 && hitPoints <=100)
this.hitPoints = hitPoints;
}
public void setStrength(int strength)
{
if(strength >= 0 && strength <=18)
this.strength = strength;
}
public int getHitPoints()
{
return hitPoints;
}
public int getStrength()
{
return strength;
}
public String toString()
{
return("hitPoints="+hitPoints+", strength="+strength);
}
}
//end of LifeForm.java
//Human.java
public class Human extends LifeForm{
private String name;
private String weapon;
private int magic;
public Human(int hitPoints, int strength, String name, String weapon, int magic)
{
super();
setHitPoints(hitPoints);
setStrength(strength);
setName(name);
setWeapon(weapon);
setMagic(magic);
}
public void setName(String name)
{
this.name = name.toLowerCase();
}
public void setWeapon(String weapon)
{
if(weapon.equalsIgnoreCase("sword") || weapon.equalsIgnoreCase("dagger"))
this.weapon = weapon.toLowerCase();
}
public void setMagic(int magic)
{
if(magic >=0 && magic <=50)
this.magic = magic;
}
public String getName()
{
return name;
}
public String getWeapon()
{
return weapon;
}
public int getMagic()
{
return magic;
}
public String toString()
{
return("Lifeform ["+super.toString()+", type=human] Human [name="+name+", weapon="+weapon+", magic="+magic+"]");
}
}
//end of Human.java
//Dragon.java
public class Dragon extends LifeForm{
private String name;
private String weapon;
private int magic;
public Dragon(int hitPoints, int strength, String name, String weapon, int magic)
{
super();
setHitPoints(hitPoints);
setStrength(strength);
setName(name);
setWeapon(weapon);
setMagic(magic);
}
public void setName(String name)
{
this.name = name.toLowerCase();
}
public void setWeapon(String weapon)
{
if(weapon.equalsIgnoreCase("fire") || weapon.equalsIgnoreCase("ice"))
this.weapon = weapon.toLowerCase();
}
public void setMagic(int magic)
{
if(magic >=0 && magic <=100)
this.magic = magic;
}
public String getName()
{
return name;
}
public String getWeapon()
{
return weapon;
}
public int getMagic()
{
return magic;
}
public String toString()
{
return("Lifeform ["+super.toString()+", type=dragon] Dragon [name="+name+", weapon="+weapon+", magic="+magic+"]");
}
}
//end of Dragon.java
//Unicorn.java
public class Unicorn extends LifeForm{
private String name;
private String weapon;
private int magic;
public Unicorn(int hitPoints, int strength, String name, String weapon, int magic)
{
super();
setHitPoints(hitPoints);
setStrength(strength);
setName(name);
setWeapon(weapon);
setMagic(magic);
}
public void setName(String name)
{
this.name = name.toLowerCase();
}
public void setWeapon(String weapon)
{
if(weapon.equalsIgnoreCase("horn") || weapon.equalsIgnoreCase("charm"))
this.weapon = weapon.toLowerCase();
}
public void setMagic(int magic)
{
if(magic >=100 && magic <=500)
this.magic = magic;
}
public String getName()
{
return name;
}
public String getWeapon()
{
return weapon;
}
public int getMagic()
{
return magic;
}
public String toString()
{
return("Lifeform ["+super.toString()+", type=unicorn] Unicorn [name="+name+", weapon="+weapon+", magic="+magic+"]");
}
}
//end of Unicorn.java
// LifeFormDriver.java
import java.util.ArrayList;
import java.util.Scanner;
public class LifeFormDriver {
public static void main(String[] args) {
ArrayList<LifeForm> characters = new ArrayList<LifeForm>();
String name, weapon, type;
int hitPoints, strength, magic;
Scanner scan = new Scanner(System.in);
for(int i=0;i<9;i++)
{
System.out.println("Enter Lifeform "+(i+1)+" : ");
System.out.print("Enter Lifeform Type: ");
type = scan.nextLine();
System.out.print("Enter Lifeform Name: ");
name = scan.nextLine();
System.out.print("Enter hit points: ");
hitPoints = scan.nextInt();
System.out.print("Enter strength: ");
strength = scan.nextInt();
scan.nextLine();
System.out.print("Enter weapon: ");
weapon = scan.nextLine();
System.out.print("Enter magic: ");
magic = scan.nextInt();
scan.nextLine();
if(type.equalsIgnoreCase("human"))
characters.add(new Human(hitPoints,strength, name,weapon,magic));
else if(type.equalsIgnoreCase("dragon"))
characters.add(new Dragon(hitPoints,strength, name,weapon,magic));
else if(type.equalsIgnoreCase("unicorn"))
characters.add(new Unicorn(hitPoints,strength, name,weapon,magic));
}
System.out.println("The available life forms are:");
for(int i=0;i<characters.size();i++)
System.out.println(characters.get(i));
scan.close();
}
}
//end of LifeFormDriver.java
Output: