public class Dog {
// Instance variables
private String name;
private int age;
private String breed;
// Constructor
public Dog(String name, int age, String breed) {
this.name = name;
this.age = age;
this.breed = breed;
}
// Method to bark
public void bark() {
System.out.println("Woof! Woof!");
}
// Method to describe the dog
public void describe() {
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Breed: " + breed);
}
// Getter and Setter methods for name, age, and breed
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public static void main(String[] args) {
// Creating an instance of the Dog class
Dog myDog = new Dog("Buddy", 3, "Golden Retriever");
// Using the methods to interact with the dog
myDog.bark();
myDog.describe();
// Updating the dog's information using setters
myDog.setAge(4);
myDog.setBreed("Labrador");
// Displaying the updated information
myDog.describe();
}
}
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.
Comments