Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

HERE IS THE CODE NEEDED TO MODIFY WHEN MAKING TEXT FILE. import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class GameDriver { /** * Method: main

image text in transcribed

HERE IS THE CODE NEEDED TO MODIFY WHEN MAKING TEXT FILE.

import java.util.ArrayList;

import java.util.InputMismatchException;

import java.util.Scanner;

public class GameDriver

{

/**

* Method: main

* Starting point of the program

* @param args

*/

public static void main(String[] args)

{

// declare common variables

ArrayList charList = new ArrayList();

Scanner in = new Scanner(System.in);

String keepLooping = "y";

// loop and add bank accounts until user is done

do

{

// ask the user what kind of character to create

System.out.println("Are you creating a human, robot or animal?");

String choice = in.nextLine();

// get name

System.out.println("Enter the character's name");

String name = in.nextLine();

// get strength

System.out.println("How much strength should the character have? (0-18 are valid values");

String strengthString = in.nextLine();

int strength = Integer.parseInt(strengthString);

// get remaining attributes based on type of character

if (choice.equalsIgnoreCase("human"))

{

// get weapon

System.out.println("Enter weapon= sword or dagger");

String weapon = in.nextLine();

// get magic

System.out.println("Enter magic- humans have between 0-50 magic points");

String magicStreing = in.nextLine();

int magic = Integer.parseInt(magicStreing);

// create Human and add to arrayList

Human human = new Human(name, strength, weapon, magic);

charList.add(human);

} else if (choice.equalsIgnoreCase("robot"))

{

// get weapon

System.out.println("Enter weapon= fire or ice");

String weapon = in.nextLine();

// get magic

System.out.println("Enter magic- humans have between 0-100 magic points");

String magicStreing = in.nextLine();

int magic = Integer.parseInt(magicStreing);

// create Human and add to arrayList

Robot robot = new Robot(name, strength, weapon, magic);

charList.add(robot);

} else if (choice.equalsIgnoreCase("animal"))

{

// get weapon

System.out.println("Enter weapon= horn or charm");

String weapon = in.nextLine();

// get magic

System.out.println("Enter magic- humans have between 100- 500 magic points");

String magicStreing = in.nextLine();

int magic = Integer.parseInt(magicStreing);

// create Human and add to arrayList

Animal animal = new Animal(name, strength, weapon, magic);

charList.add(animal);

} else

{

System.out.println("That was an invalid character type");

}

// ask the user if they want to continue;

System.out.println("Do you want to continue - y or n");

keepLooping = in.nextLine();

} while (keepLooping.equalsIgnoreCase("y"));

System.out.println("Your game characters are:");

// print the ArrayList

for (GameCharacter g : charList)

{

System.out.println(g);

}

// get first character and call hit and refill

GameCharacter g = charList.get(0);

g.hit(10);

g.refill();

// print arraylist again to show changes

System.out.println();

System.out.println("The charactesr after hit and refill are:");

for (GameCharacter g2 : charList)

{

System.out.println(g2);

}

}

}

ANIMAL CLASS :

* Class: Animal * * This class provides the Animal character which inherits from GameCharacter and has unique setter methods to * limit the inputs for weapon and magic. It also overrides toString method. * */ public class Animal extends GameCharacter {

private String weapon;

private int magic;

/** * default (no-arg) constructor */ public Animal() { super(); // default values weapon = "horn"; magic = 250; }

/** * All arg constructor including parent class * * @param name * @param strength * @param weapon * @param magic */ public Animal(String name, int strength, String weapon, int magic) { super(name, strength); setWeapon(weapon); setMagic(magic); }

/** * Getter method for weapon * * @return the weapon */ public String getWeapon() { return weapon; }

/** * Setter method for weapon- limits to valid inputs * * @param weapon the weapon to set */ public void setWeapon(String weapon) { if (weapon.equalsIgnoreCase("horn") || weapon.equalsIgnoreCase("charm")) { this.weapon = weapon; } else { // set to default this.weapon = "horn"; } }

/** * Getter method for magic * * @return the magic */ public int getMagic() { return magic; }

/** * Setter method for magic - limits to valid inputs * * @param magic the magic to set */ public void setMagic(int magic) { if (magic >= 100 && magic

/** * Overridden toSTring method provides attribute values in a readable fashion * @return String */ @Override public String toString() { return "Animal [weapon=" + weapon + ", magic=" + magic + ", getName()=" + getName() + ", getStrength()=" + getStrength() + "]"; }

}

ROBOT CLASS:

* Class: Robot */ public class Robot1 extends GameCharacter {

private String weapon;

private int magic;

/** * default (no-arg) constructor */ public Robot1() { super(); // default values weapon = "ice"; magic = 50; }

/** * All arg constructor including parent class * * @param name * @param strength * @param weapon * @param magic */ public Robot1(String name, int strength, String weapon, int magic) { super(name, strength); setWeapon(weapon); setMagic(magic); }

/** * Getter method for weapon attribute * * @return the weapon */ public String getWeapon() { return weapon; }

/** * Setter method for weapon attribute. Prevents bad inputs * * @param weapon the weapon to set */ public void setWeapon(String weapon) { if (weapon.equalsIgnoreCase("ice") || weapon.equalsIgnoreCase("fire")) { this.weapon = weapon; } else { // set to default this.weapon = "ice"; } }

/** * Getter method for magic attribute * * @return the magic */ public int getMagic() { return magic; }

/** * Setter method for magic attributes- limits setting to acceptable values * * @param magic the magic to set */ public void setMagic(int magic) { if (magic >= 0 && magic

/** * Overridden toSTring method provides attribute values in a readable fashion * @return String */ @Override public String toString() { return "Robot [weapon=" + weapon + ", magic=" + magic + ", getName()=" + getName() + ", getStrength()=" + getStrength() + "]"; }

}

Human Class:

public class Human1 extends GameCharacter

{

// unique attributes for human

private String weapon;

private int magic;

/**

* default (no-arg) constructor

*/

public Human1()

{

super();

// default values

weapon = "Sword";

magic = 25;

}

/**

* All arg constructor including parent class

*

* @param name

* @param strength

* @param weapon

* @param magic

*/

public Human1(String name, int strength, String weapon, int magic)

{

super(name, strength);

setWeapon(weapon);

setMagic(magic);

}

/**

* Getter method for weapon

*

* @return the weapon

*/

public String getWeapon()

{

return weapon;

}

/**

* Setter method for weapon- limits to valid inputs

*

* @param weapon the weapon to set

*/

public void setWeapon(String weapon)

{

if (weapon.equalsIgnoreCase("Sword") || weapon.equalsIgnoreCase("dagger"))

{

this.weapon = weapon;

} else

{

// set to default

this.weapon = "sword";

}

}

/**

* Getter method for magic

*

* @return the magic

*/

public int getMagic()

{

return magic;

}

/**

* Setter method for magic

*

* @param magic the magic to set

*/

public void setMagic(int magic)

{

if (magic >= 0 && magic

{

this.magic = magic;

} else

{

// set to default

this.magic = 25;

}

}

/**

* Overridden toSTring method provides attribute values in a readable fashion

* @return String

*/

@Override

public String toString()

{

return "Human [weapon=" + weapon + ", magic=" + magic + ", getName()=" + getName() + ", getStrength()="

+ getStrength() + "]";

}

}

File Reading Assignment - Due Feb 28, 2020 11:30 PM Intermediate Programming Section 05 Spring 2020 CO Modify HW 2 to read the Game Character information from a text file instead of the user. You may use your HW2 or my HW2 answer that will be posted on Tuesday night. Turn in all files needed to compile and make sure to create and include a text file with at least one Human, one Animal and one Robot (more is okay too). File Reading Assignment - Due Feb 28, 2020 11:30 PM Intermediate Programming Section 05 Spring 2020 CO Modify HW 2 to read the Game Character information from a text file instead of the user. You may use your HW2 or my HW2 answer that will be posted on Tuesday night. Turn in all files needed to compile and make sure to create and include a text file with at least one Human, one Animal and one Robot (more is okay too)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions