Question

In: Computer Science

How would I get this java code to work and with a main class that would...

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;
    }

}

Solutions

Expert Solution

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:


Related Solutions

how to correct this java code so that i get the correct day of week? and...
how to correct this java code so that i get the correct day of week? and test the year public static void main(String[] args) {        //               Scanner s = new Scanner(System.in); //needed info //year month, day int year, month, dayOfMonth; // add day of week , century yr int dayOfWeek, century, yearOfCentury;    //user inputs year System.out.print("Enter year: (example, 2020):"); year = s.nextInt(); //user inputs month by number System.out.print("Enter month: 1-12:"); month = s.nextInt();...
How would I get this started: Code a logic program that uses a for loop to...
How would I get this started: Code a logic program that uses a for loop to call your method 5 times successively on the values 1 through 5. (i.e. it would display the val and it’s square…)
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
Complete the code to make the following main work public class Time { **put the solution...
Complete the code to make the following main work public class Time { **put the solution here** public static void main(String[] args){ Time a = new Time(15,10,30); Time b = new Time(); // default to 15:00:00(the start time of our class!) System.out.println(a); // should print out: 15:10:30 System.out.println(b); // should print out: System.out.println(a.dist(b)); // print the difference in seconds between the two timestamps } }
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT