quhluh

Rabit

Dec 8th, 2023
1,082
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. Creating a Rabbit entity in Java involves defining a class that represents the properties and behavior of a rabbit. Here's a simple example of a Rabbit class:
  2.  
  3. java
  4. Copy code
  5. public class Rabbit {
  6.    // Properties
  7.    private String name;
  8.    private int age;
  9.    private String color;
  10.  
  11.    // Constructor
  12.    public Rabbit(String name, int age, String color) {
  13.        this.name = name;
  14.        this.age = age;
  15.        this.color = color;
  16.    }
  17.  
  18.    // Getters and setters
  19.    public String getName() {
  20.        return name;
  21.    }
  22.  
  23.    public void setName(String name) {
  24.        this.name = name;
  25.    }
  26.  
  27.    public int getAge() {
  28.        return age;
  29.    }
  30.  
  31.    public void setAge(int age) {
  32.        this.age = age;
  33.    }
  34.  
  35.    public String getColor() {
  36.        return color;
  37.    }
  38.  
  39.    public void setColor(String color) {
  40.        this.color = color;
  41.    }
  42.  
  43.    // Behavior: hopping
  44.    public void hop() {
  45.        System.out.println(name + " is hopping!");
  46.    }
  47.  
  48.    // Behavior: eating
  49.    public void eat() {
  50.        System.out.println(name + " is eating carrots.");
  51.    }
  52.  
  53.    // Display information about the rabbit
  54.    public void displayInfo() {
  55.        System.out.println("Name: " + name);
  56.        System.out.println("Age: " + age + " years");
  57.        System.out.println("Color: " + color);
  58.    }
  59.  
  60.    // Main method for testing
  61.    public static void main(String[] args) {
  62.        // Create a Rabbit object
  63.        Rabbit myRabbit = new Rabbit("Bugs", 2, "Gray");
  64.  
  65.        // Display information about the rabbit
  66.        myRabbit.displayInfo();
  67.  
  68.        // Perform some behaviors
  69.        myRabbit.hop();
  70.        myRabbit.eat();
  71.    }
  72. }
  73. In this example, the Rabbit class has properties like name, age, and color. It also has methods to set and get these properties, as well as some behaviors like hopping and eating. The main method is used for testing the Rabbit class by creating an instance of it and calling its methods.
  74.  
  75. Feel free to customize the class according to your specific requirements.
Advertisement
Comments
Add Comment
Please, Sign In to add comment