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 this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
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();...
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]+" ");   ...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[]...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[] args) {        // Define the input string. Note that I could have written it all on a        // single line, but I broke it up to match the question.        String input = "Book , Cost , Number\n"                    + "Hamlet , 24.95 , 10\n"                    + "King Lear , 18.42...
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…)
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT