In: Computer Science
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than the user. You should use appropriate exception handling when reading from the file. The text file that you will read is provided.
-------------------- Modify GaveDriver2.java --------------------
-----GameDriver2.java -----
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Class: GameDriver
*
* This class provides the main method for Homework 2 which reads in
a
* series of Wizards, Soldier and Civilian information from the
user
* and creates the objects
*
*/
public class GameDriver2
{
/**
* Main method- Starting point of program
*
* @param args
*/
public static void main(String[] args)
{
// create needed variables
ArrayList characters = new ArrayList();
// YOU CHANGE THIS LINE to opening file Characters.txt - use
appropriate exception handling
// don't
// just throw the exception
Scanner in = new Scanner(System.in);
// End first change
// HERE IS CODE THAT READS A LINE AND CREATES APPROPRIATE CHARACTER
while (in.hasNextLine())
{
// read in a line and parse into various attributes
String line = in.nextLine();
// parse based on comma
String[] data = line.split(", ");
String type = data[0];
String name = data[1];
int strength = Integer.parseInt(data[2]);
String weapon = data[3];
int magic = Integer.parseInt(data[4]);
// use this data to build appropriate GameCharacter based on type
and add to ArrayList
// YOU ADD THIS CODE
}
// print array before hits or healing
for (GameCharacter g : characters)
{
System.out.println(g);
}
System.out.println();
System.out.println("After soldier takes a hit and civilian is
healed");
// print contents of arrayList
for (int i = 0; i < characters.size(); i++)
{
GameCharacter g = characters.get(i);
// if a civilian - heal them
if (g instanceof Civilian)
{
g.heal(5);
}
// else if a soldier - takes a hit
else if (g instanceof Soldier)
{
g.takeHit(5);
}
System.out.println(g);
}
}
}
----- GameCharacter.java -----
/**
* Class: GameCharacter
*
* This class provides a parent class for characters to be created
for
* our game.
*
*/
public class GameCharacter {
// attributes for all GameCharacters
private String name;
private int strength;
/**
* Name Setter
*
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Strength setter method
*
* Makes sure strength is a legit value. Ignores
otherwise
*
* @param strength the strength to set
*/
public void setStrength(int strength) {
if (strength > 0 &&
strength <= 18) {
this.strength =
strength;
} else {
this.strength =
12; // default number I made up
}
}
/**
* All arg constructor
*
* @param name
* @param strength
*/
public GameCharacter(String name, int strength)
{
super();
this.name = name;
setStrength(strength);
}
/**
* No-arg (default) constructor
*/
public GameCharacter() {
this.strength = 15;
this.name = "TBD";
}
/**
* Getter method for name
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Getter method for strength
*
* @return the strength
*/
public int getStrength() {
return strength;
}
/**
* method: takeHit() Method called when character is
attacked and has lost
* strength
*
* @param int amount
*/
public void takeHit(int amount) {
strength = strength - amount;
if (strength < 0) {
strength =
0;
}
}
/**
* Method: heal()
*
* Method called as character heals from a hit
*
* @param int amount
*
*/
public void heal(int amount) {
strength = strength + amount;
if (strength > 18) {
strength =
18;
}
}
}
----- Characters.txt -----
Soldier, Sol, 14, Sword, 50
Wizard, Wiz, 5, Staff, 444
Civilian, Civ, 18, Words, 3
Soldier, Joe, 3, Gun, 0
Wizard, Gandalf, 10, Wand, 500
Civilian, Frodo, 16, Money, 40
I can provide the 3 child classes (Soldier, Wizard, Civilian) if needed as the code exceed the text limits.
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import game.GameCharacter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class GameDriver2
{
public static void main(String[] args) throws IOException
{
ArrayList<GameCharacter> characters = new ArrayList<>();
File file = null;
FileReader fr=null;
BufferedReader br=null;
try
{
file = new File("C:\\Users\\Gaurang\\Desktop\\Characters.txt");
fr = new FileReader(file);
br=new BufferedReader(fr);
String line;
while ((line=br.readLine())!=null)
{
// parse based on comma
String[] data = line.split(", ");
String type = data[0];
String name = data[1];
int strength = Integer.parseInt(data[2]);
String weapon = data[3];
int magic = Integer.parseInt(data[4]);
// use this data to build appropriate GameCharacter based on type and add to ArrayList
// YOU ADD THIS CODE
if(type=="Soldier")
{
Soldier soldier = new Soldier(name,strength);
characters.add(soldier);
}
else if(type=="Civilian")
{
Civilian civ = new Civilian(name,strength);
characters.add(civ);
}
else if(type=="Wizard")
{
Wizard wiz = new Wizard(name,strength);
characters.add(wiz);
}
}
}
catch(FileNotFoundException exc)
{
if(!file.exists())
{
System.out.println("The Given File Does Not Exists");
}
}
finally
{
fr.close();
br.close();
}
//print array before hits or healing
for (GameCharacter g : characters)
{
System.out.println(g);
}
System.out.println();
System.out.println("After soldier takes a hit and civilian is healed");
// print contents of arrayList
for (int i = 0; i < characters.size(); i++)
{
GameCharacter g = characters.get(i);
// if a civilian - heal them
if (g instanceof Civilian)
{
g.heal(5);
}
// else if a soldier - takes a hit
else if (g instanceof Soldier)
{
g.takeHit(5);
}
System.out.println(g);
}
}
}