quhluh

Untitled

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