// Class DeclarationpublicclassDog {// Instance VariablesprivateString name;privateString breed;privateString size;privateint age;privateString color;//constructorDog(String my_name,String breed,String size,int age,String my_color) {this.breed=breed; name=my_name;this.age= age;this.size=size; color= my_color; }//copy constructorDog(Dog other) { breed =other.breed; name =other.name; age =other.age; size =other.size; color =other.color; }// non static methodpublicvoidsleep() {System.out.println(name +" is sleeping"); }//static methodpublicstaticvoidwhoIsSleeping(Dog dog){System.out.println(dog.name+" is sleeping"); }//getters and setters// === ADDED THIS ===publicStringgetName() {return name; }publicvoidsetName(String name) {this.name= name; }publicStringgetBreed() {return breed; }publicvoidsetBreed(String breed) {this.breed= breed; }publicStringgetSize() {return size; }publicvoidsetSize(String size) {this.size= size; }publicintgetAge() {return age; }publicvoidsetAge(int age) {this.age= age; }publicStringgetColor() {return color; }publicvoidsetColor(String color) {this.color= color; }publicStringtoString() {return ("Name : "+name+"\nBreed is: "+breed+"\nSize is: "+size+"\nAge is: "+age+"\nColor is: "+color+"\n"); }}
Snowball
An array of Dogs 🐶🐩🐕
publicclassMain {publicstaticvoidmain(String[] args) {String[] names = {"fluffy","snowball","shugi","cookie"};Dog[] dogs =newDog[4];for (int i =0; i <dogs.length; i++) dogs[i] =newDog(names[i],"Maltese","Small",2,"white");for (int i =0; i <dogs.length; i++)System.out.println(dogs[i]); }}
Name : fluffy
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white
Name : snowball
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white
...