Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Homework 6 - Classes, Inheritance, and Polymorphism in Java Homework 6-Classes,

ID: 3704925 • Letter: H

Question

Homework 6 - Classes, Inheritance, and Polymorphism in Java

Homework 6-Classes, Inheritance, and Polymorphism in Java Due: Thursday, April 12 on the server at 11:59 PM, in directory hw6 1 - Objective Practice OOP concepts with the use of multiple classes and inheritance in the Java environment. 2.1 -Super class character The first class is the base class for the rest of the classes. This class will have four protected variables: String Type, string name, char gender, int hitPoints. You will need to create a default constructor that sets all values to defaults (i.e., Strings- null, char0', int -0.), and a constructor that takes four arguments to set the objects' four variables defined above. This class will contain an override to the toString ) function that is present in every class created in Java. This overwritten function will return the type string.format (. The format function on string will behave in the same way as System.out.format does to create the table headers (please refer to the examples in the end of the instruction about this format function) This class will contain a getType function that will return what subclass type the character is, that is, mob, townsperson, or player, described in the following. 2.2 - Super/Extended class npc This class extends the character class, and has the protected variable: string zone. You will need to create a default constructor that sets all values to defaults(String null), and an constructor that takes five arguments, all the variables from the character as well

Explanation / Answer

package tution;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class MainClass {

public static void main(String[]args) throws NumberFormatException, IOException{

int numInputs;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

numInputs = Integer.parseInt(br.readLine());

character[] inputList = new character[numInputs];

int playerCount=0;

int mobCount=0;

int townsPersonCount=0;

int[]playerPosList = new int[numInputs];

int[]mobPosList = new int[numInputs];

int[]townsPersonPosList = new int[numInputs];

for(int i=0;i<numInputs;i++){

String type = br.readLine();

String name = br.readLine();

char gender= br.readLine().charAt(0);

int hitPoints = Integer.parseInt(br.readLine());

if(type.trim().toLowerCase().equals("player")){

String classType = br.readLine();

String armorType = "Unknown";

if(classType.trim().toLowerCase().equals("mage")){

armorType="Cloth";

}else{

if(classType.trim().toLowerCase().equals("rougue")){

armorType="Leather";

}

else{

if(classType.trim().toLowerCase().equals("Warrior")){

armorType="Platemail";

}

}

}

inputList[i] = new player(type, name, gender, hitPoints, classType, armorType);

playerPosList[playerCount++]=i;

}

if(type.trim().toLowerCase().equals("mob")){

String classType = br.readLine();

String zone = br.readLine();

boolean questMob = Boolean.parseBoolean(br.readLine());

inputList[i]=new mob(type, name, gender, hitPoints, zone, classType, questMob);

mobPosList[mobCount++]=i;

}

if(type.trim().toLowerCase().equals("townsperson")){

String zone = br.readLine();

boolean hasQuest = Boolean.parseBoolean(br.readLine());

inputList[i]=new townsperson(type, name, gender, hitPoints, zone, hasQuest);

townsPersonPosList[townsPersonCount++]=i;

}

}

System.out.println("Player Characters");

System.out.format(" %15s%15s%15s%15s%25s ", "Name","Gender","Hit Points","Class","Equipable Gear");

for(int i=0;i<playerCount;i++){

int pos = playerPosList[i];

System.out.println(inputList[pos].toString());

}

System.out.println(" NPC's-Mobs");

System.out.format(" %15s%15s%15s%25s%15s%15s ", "Name","Gender","Hit Points","Zone","Class","Quest Mob");

for(int i=0;i<mobCount;i++){

int pos = mobPosList[i];

System.out.println(inputList[pos].toString());

}

System.out.println(" NPC's-Towns People");

System.out.format(" %15s%15s%15s%25s%15s ", "Name","Gender","Hit Points","Zone","Has Quest");

for(int i=0;i<townsPersonCount;i++){

int pos = townsPersonPosList[i];

System.out.println(inputList[pos].toString());

}

}

}

character.java

public class character {

protected String Type;

protected String name;

protected char gender;

protected int hitPoints;

public character() {

Type = null;

name = null;

gender = '';

hitPoints=0;

}

public character(String type, String name, char gender, int hitPoints) {

Type = type;

this.name = name;

this.gender = gender;

this.hitPoints = hitPoints;

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return String.format("%15s%15s%15s",name,gender,hitPoints);

}

}

npc.java

public class npc extends character {

protected String zone;

public npc(String type,String name, char gender, int hitPoints,String zone) {

super(type,name,gender,hitPoints);

this.zone = zone;

}

public npc() {

super();

this.zone=null;

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return super.toString()+String.format("%25s",zone);

}

}

mob.java

public class mob extends npc {

private String classType;

private boolean questMob;

public mob(String type, String name, char gender, int hitPoints, String zone, String classType, boolean questMob) {

super(type, name, gender, hitPoints, zone);

this.classType = classType;

this.questMob = questMob;

}

public mob() {

super();

this.classType=null;

this.questMob=false;

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return super.toString()+String.format("%15s%15s",classType,questMob);

}

}

townspersons.java

public class townsperson extends npc {

private boolean hasQuest;

public townsperson() {

super();

this.hasQuest=false;

}

public townsperson(String type, String name, char gender, int hitPoints, String zone,boolean hasQuest) {

super(type, name, gender, hitPoints, zone);

this.hasQuest=hasQuest;

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return super.toString()+String.format("%15s",hasQuest);

}

}

players.java

public class player extends character {

private String classType;

private String armorType;

public player() {

super();

this.classType=null;

this.armorType=null;

}

public player(String type, String name, char gender, int hitPoints,String classType,String armorType) {

super(type, name, gender, hitPoints);

this.classType=classType;

this.armorType=armorType;

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return super.toString()+String.format("%15s%25s",classType,armorType);

}

}

mainclass.java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class MainClass {

public static void main(String[]args) throws NumberFormatException, IOException{

int numInputs;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

numInputs = Integer.parseInt(br.readLine());

character[] inputList = new character[numInputs];

int playerCount=0;

int mobCount=0;

int townsPersonCount=0;

int[]playerPosList = new int[numInputs];

int[]mobPosList = new int[numInputs];

int[]townsPersonPosList = new int[numInputs];

for(int i=0;i<numInputs;i++){

String type = br.readLine();

String name = br.readLine();

char gender= br.readLine().charAt(0);

int hitPoints = Integer.parseInt(br.readLine());

if(type.trim().toLowerCase().equals("player")){

String classType = br.readLine();

String armorType = "Unknown";

if(classType.trim().toLowerCase().equals("mage")){

armorType="Cloth";

}else{

if(classType.trim().toLowerCase().equals("rougue")){

armorType="Leather";

}

else{

if(classType.trim().toLowerCase().equals("Warrior")){

armorType="Platemail";

}

}

}

inputList[i] = new player(type, name, gender, hitPoints, classType, armorType);

playerPosList[playerCount++]=i;

}

if(type.trim().toLowerCase().equals("mob")){

String classType = br.readLine();

String zone = br.readLine();

boolean questMob = Boolean.parseBoolean(br.readLine());

inputList[i]=new mob(type, name, gender, hitPoints, zone, classType, questMob);

mobPosList[mobCount++]=i;

}

if(type.trim().toLowerCase().equals("townsperson")){

String zone = br.readLine();

boolean hasQuest = Boolean.parseBoolean(br.readLine());

inputList[i]=new townsperson(type, name, gender, hitPoints, zone, hasQuest);

townsPersonPosList[townsPersonCount++]=i;

}

}

System.out.println("Player Characters");

System.out.format(" %15s%15s%15s%15s%25s ", "Name","Gender","Hit Points","Class","Equipable Gear");

for(int i=0;i<playerCount;i++){

int pos = playerPosList[i];

System.out.println(inputList[pos].toString());

}

System.out.println(" NPC's-Mobs");

System.out.format(" %15s%15s%15s%25s%15s%15s ", "Name","Gender","Hit Points","Zone","Class","Quest Mob");

for(int i=0;i<mobCount;i++){

int pos = mobPosList[i];

System.out.println(inputList[pos].toString());

}

System.out.println(" NPC's-Towns People");

System.out.format(" %15s%15s%15s%25s%15s ", "Name","Gender","Hit Points","Zone","Has Quest");

for(int i=0;i<townsPersonCount;i++){

int pos = townsPersonPosList[i];

System.out.println(inputList[pos].toString());

}

}

}

OUTPUT

=====

4

Player

Monica

F

250

Warrior

Player

John

M

100

Mage

Mob

Wolf

M

15

Everfrost

Warrior

true

Townsperson

Helga

F

1

Everfrost

true

Player Characters

Name Gender Hit Points Class Equipable Gear

Monica F 250 Warrior Unknown

John M 100 Mage Cloth

NPC's-Mobs

Name Gender Hit Points Zone Class Quest Mob

Wolf M 15 Warrior Everfrost true

NPC's-Towns People

Name Gender Hit Points Zone Has Quest

Helga F 1 Everfrost true