Write a two classes: One class draws a square, and an exception that occurs when
ID: 3568268 • Letter: W
Question
Write a two classes: One class draws a square, and an exception that occurs when the user puts in an invalid dimension.
//LAB20FILES:
//TEXT FILE:
Example Dialog:
Simple PVP Game
Player 1
The player Travis the Immortal!
HP: 100
Strength: 15
Speed: 2.0
Weapon: Board with a nail
Player 2
The player Bob
HP: 100
Strength: 10
Speed: 5.5
Weapon: Rat on a Stick
Travis the Immortal! attacks with Board with a nail
Bob now has 85 HP
Bob attacks with Rat on a Stick
Travis the Immortal! now has 90 HP
Travis the Immortal! attacks with Board with a nail
Bob now has 70 HP
Bob attacks with Rat on a Stick
Travis the Immortal! now has 80 HP
Travis the Immortal! attacks with Board with a nail
Bob now has 55 HP
Bob attacks with Rat on a Stick
Travis the Immortal! now has 70 HP
Travis the Immortal! attacks with Board with a nail
Bob now has 40 HP
Bob attacks with Rat on a Stick
Travis the Immortal! now has 60 HP
Travis the Immortal! attacks with Board with a nail
Bob now has 25 HP
Bob attacks with Rat on a Stick
Travis the Immortal! now has 50 HP
Travis the Immortal! attacks with Board with a nail
Bob now has 10 HP
Bob attacks with Rat on a Stick
Travis the Immortal! now has 40 HP
Travis the Immortal! attacks with Board with a nail
Bob now has -5 HP
The battle is over!
Travis the Immortal! has won!
The player was cloned after the battle and has the following stats
The player Travis the Immortal!
HP: 40
Strength: 15
Speed: 2.0
Weapon: Board with a nail
Explanation / Answer
Here you go :)
//RPGPlayer class
Make sure you keep the player.txt file in the folder where src is present.
import java.util.*;
import java.io.*;
public class RPGPlayer
{
String name,weapon;
private int HP;
private int strength;
private double speed;
public RPGPlayer()
{
this.name="";
this.HP=0;
this.strength=0;
this.speed=0;
this.weapon="";
}
public RPGPlayer(String name,int HP,int strength,double speed,String weapon)
{
this.name=name;
this.HP=HP;
this.setStrength(strength);
this.speed=speed;
this.weapon=weapon;
}
public RPGPlayer(String fileName)
{
try{
this.readPlayerFile(fileName);
}catch(Exception e){System.out.println(e);}
}
public String getName() {
return name;
}
public String getWeapon() {
return weapon;
}
public int getHP() {
return HP;
}
public int getStrength() {
return strength;
}
public double getSpeed() {
return speed;
}
public void setName(String name) {
this.name = name;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public void setHP(int hP) {
HP = hP;
}
public void setStrength(int strength) {
if(strength>=0)
this.strength = strength;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public void printInfo()
{
System.out.println("The Player "+this.name);
System.out.println("HP: "+this.HP);
System.out.println("Strength: "+this.strength);
System.out.println("Speed: "+this.speed);
System.out.println("Weapon: "+this.weapon);
}
public void readPlayerFile(String fileName) throws IOException
{
BufferedReader sc=new BufferedReader(new FileReader(fileName));
String details="";
while((details=sc.readLine())!=null)
{
String[] lines=details.split(" ");
switch(lines[0].toLowerCase())
{
case "name":String s="";for(int i=1;i<lines.length;i++)s+=lines[i]+" ";this.setName(s);break;
case "hp":this.setHP(Integer.parseInt(lines[1]));break;
case "strength":this.setStrength(Integer.parseInt(lines[1]));break;
case "speed":this.setSpeed(Double.parseDouble(lines[1]));break;
case "weapon":s="";for(int i=1;i<lines.length;i++)s+=lines[i]+" ";this.setWeapon(s);break;
}
}
sc.close();
}
public void writePlayerFile(String fileName)
{
try{
BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
String details="Name "+this.name+" HP "+this.HP+" Strength "+this.strength+" Speed "+this.speed+" Weapon "+this.weapon;
bw.write(details);
bw.close();
System.out.println("written");
}catch(Exception e){System.out.println(e);}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.