Write the following problem in Java
“Hi, my name is Lucky. I am a Chiwawa. I am 3 years old, and my color is white.”
JAVA PROGRAM
class Dog{//class Dog
private String name;
private String breed;
private int age;
private String color;
public Dog(){//constructor
name="Max";
breed="Labrador";
age=2;
color="black";
}
public String toString(){//method toString
return "Hi, my name is "+name+". I am a "+
breed+". I am "+age+" years old, and my color is "+
color+".";
}
public void setName(String name){//method setName
this.name=name;
}
public String getName(){//method getName
return name;
}
public void setBreed(String breed){//method setBreed
this.breed=breed;
}
public String getBreed(){//method getBreed
return breed;
}
public void setAge(int age){//method setAge
this.age=age;
}
public int getAge(){//method getAge
return age;
}
public void setColor(String color){//method setColor
this.color=color;
}
public String getColor(){//method getColor
return color;
}
}
public class Main//class Main
{
public static void main(String[] args) {//method
main
Dog d=new Dog(); //Dog class object
System.out.println(d);
d.setName("Lucky"); //call method setName
d.setBreed("Chiwawa");//call method setBreed
d.setAge(3);//call method setAge
d.setColor("white");//call method
System.out.println(d);
}
}
OUTPUT
Get Answers For Free
Most questions answered within 1 hours.