quhluh

Dog

Dec 9th, 2023
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. public class Dog {
  2.     // Instance variables
  3.     private String name;
  4.     private int age;
  5.     private String breed;
  6.  
  7.     // Constructor
  8.     public Dog(String name, int age, String breed) {
  9.         this.name = name;
  10.         this.age = age;
  11.         this.breed = breed;
  12.     }
  13.  
  14.     // Method to bark
  15.     public void bark() {
  16.         System.out.println("Woof! Woof!");
  17.     }
  18.  
  19.     // Method to describe the dog
  20.     public void describe() {
  21.         System.out.println("Name: " + name);
  22.         System.out.println("Age: " + age + " years");
  23.         System.out.println("Breed: " + breed);
  24.     }
  25.  
  26.     // Getter and Setter methods for name, age, and breed
  27.     public String getName() {
  28.         return name;
  29.     }
  30.  
  31.     public void setName(String name) {
  32.         this.name = name;
  33.     }
  34.  
  35.     public int getAge() {
  36.         return age;
  37.     }
  38.  
  39.     public void setAge(int age) {
  40.         this.age = age;
  41.     }
  42.  
  43.     public String getBreed() {
  44.         return breed;
  45.     }
  46.  
  47.     public void setBreed(String breed) {
  48.         this.breed = breed;
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         // Creating an instance of the Dog class
  53.         Dog myDog = new Dog("Buddy", 3, "Golden Retriever");
  54.  
  55.         // Using the methods to interact with the dog
  56.         myDog.bark();
  57.         myDog.describe();
  58.  
  59.         // Updating the dog's information using setters
  60.         myDog.setAge(4);
  61.         myDog.setBreed("Labrador");
  62.  
  63.         // Displaying the updated information
  64.         myDog.describe();
  65.     }
  66. }
  67. This is a simple representation of a Dog class with some basic attributes (name, age, breed) and methods (bark, describe). In the main method, an instance of the Dog class is created, and its methods are called to interact with the dog object. You can customize and expand this class based on your specific requirements.
Advertisement
Add Comment
Please, Sign In to add comment