Question

In: Computer Science

In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We...

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:

  • hitPoints – range 0 to 100
  • strength – range 0 to 18

You will need to provide a default constructor that initializes these attributes as follows:

  • strength – 15
  • hitPoints – 100

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

Solutions

Expert Solution

//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:


Related Solutions

The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.
The purpose of this lab is to practice using inheritance and polymorphism.    We need a set of classes for an Insurance company.   Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc.    Insurance policies have a lot of data and behavior in common.   But they also have differences.   You want to build each insurance policy class so that you can reuse as much code as possible.   Avoid duplicating code.   You’ll save time, have less code to...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way. For example, have a look at the code we’ve included for this problem. We’ve defined Shape as an abstract base class. It doesn’t provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use of interfaces. The program should not take up more than half a page of size A4.
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output)...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs used for processing large amount of data. Daisy is recently hired by a warehouse. One of her job is to keep track the items ordered by all the branches of the company. The company wants to automate the task using a computer program. Being a friend of Daisy, she knows you are a Computer Science...
writing a well documented program with multiple classes and arrays.   Include a pledge and the IDE...
writing a well documented program with multiple classes and arrays.   Include a pledge and the IDE used in the descriptive header of the driver class Create a Student class with Private instance variables for first and last name, three integer scores, the average ,and letter grade A constructor with values Methods to set and get the values of the instance variables Methods to get the average and letter grade Method to calculate the average of the three scores and to...
Which diagram illustrates multiple inheritance?
Which diagram illustrates multiple inheritance? 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT