2.Write a Java program to create a class called "Dog" with a name and breed attribute. Create two instances of the "Dog" class, set their attributes using the constructor and modify the attributes using the setter methods and print the updated values
class Dog
{
private String Name;
private String Breed;
public Dog(String n,String b)
{
Name=n;
Breed=b;
}
String getName()
{
return Name;
}
String getBreed()
{
return Breed;
}
void setName(String n)
{
Name=n;
}
void setBreed(String b)
{
Breed=b;
}
void print()
{
System.out.println("Name:"+Name);
System.out.println("Breed:"+Breed);
}
public static void main(String st[])
{
Dog obj=new Dog("tommy","Bulldog");
Dog obj2=new Dog("Tuffey","German Shepherd");
System.out.println("\nDog 1:");
obj.print();
System.out.println("\nDog 2:");
obj2.print();
System.out.println("\nUpdated values of Dog 1:");
obj.setName("charlie");
obj.setBreed("Maltese");
obj.print();
System.out.println("\nUpdated values of Dog 2:");
obj2.setName("Julie");
obj2.setBreed("Bulldog");
obj2.print();
}
}
OUTPUT
Dog 1:
Name:tommy
Breed:Bulldog
Dog 2:
Name:Tuffey
Breed:German Shepherd
Updated values of Dog 1:
Name:charlie
Breed:Maltese
Updated values of Dog 2:
Name:Julie
Breed:Bulldog