In: Computer Science
How would I get this java code to work and with a main class that would demo the rat class?
class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability() { return specialAbility; } public void sethealth(int TH) { TotalHealth=TH; } public int gethealth() { return TotalHealth; } public void setsh(int SH) { shieldedHealth=SH; } public int getsh() { return shieldedHealth; } public void setcd(int cd) { cooldown=cd; } public int getcd() { return cooldown; } public String toString() { return name+" "+specialAbility+" "+TotalHealth+" "+shieldedHealth+" "+cooldown; } }
Java Program:
class rat {
private String name;
private String specialAbility;
private int TotalHealth;
private int shieldedHealth;
private int cooldown;
public rat()
{
}
public rat(String n,String SA,int TH,int SH,int cd)
{
name=n;
specialAbility=SA;
TotalHealth=TH;
shieldedHealth=SH;
cooldown=cd;
}
public void setname(String n)
{
name=n;
}
public String getname()
{
return name;
}
public void setability(String SA)
{
specialAbility=SA;
}
public String getability()
{
return specialAbility;
}
public void sethealth(int TH)
{
TotalHealth=TH;
}
public int gethealth()
{
return TotalHealth;
}
public void setsh(int SH)
{
shieldedHealth=SH;
}
public int getsh()
{
return shieldedHealth;
}
public void setcd(int cd)
{
cooldown=cd;
}
public int getcd()
{
return cooldown;
}
public String toString()
{
return name+" "+specialAbility+" "+TotalHealth+" "+shieldedHealth+"
"+cooldown;
}
}
//Demo class
class RatDemo
{
//Main method
public static void main(String[] args)
{
//Creating a rat object
rat r1 = new rat("ABC", "Yes", 100,
80, 40);
//Printing rat object
System.out.println(r1);
//Modifying name of rat
object
r1.setname("PQR");
//Modifying cooldown of rat
object
r1.setcd(10);
//Printing using Getter
Methods
System.out.println("\nrat Name: " +
r1.getname());
System.out.println("Special
Ability: " + r1.getability());
System.out.println("Total Health: "
+ r1.gethealth());
System.out.println("Shielded
Health: " + r1.getsh());
System.out.println("Cool Down: " +
r1.getcd() + "\n");
}
}
__________________________________________________________________________________________
Sample Run: