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

I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
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...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get started with the constructors, implement the class variables and    // =====   the accessor methods (getX,getY). The tester for the constructors relies on these    //           methods being implemented. After this, move ahead with the constructors          // class variables here       COMPLETE JAVA CODE: /** * Creates the vector <code>(0.0, 0.0)</code>. */ public Vector2() {       COMPLETE...
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...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT