Using Java
Write a class called Dog that contains instance data that represents the dog’s name, breed, weight, birth date, and medical history. Define the Dog constructor to accept and initialize instance data (begin the medical history with an empty line). Include accessor and mutator methods every attribute. For the medical history in particular, define the mutator method to simply add strings to the medical history so the user sees a printout of information about the dog (be mindful of adding new line characters). Include a toString method that returns the dog's information in a "nice" format (this is subjective, up to you how you do it). Create a driver class called Vet, whose main method instantiates and updates several Dog objects.
class Dog{
String name,breed,birth_date,medical_history="\n";
int weight;
public Dog(String n,String b,String d,String m,int w){//constructor
of class
this.name=n;
this.breed=b;
this.birth_date=d;
this.weight=w;
this.medical_history+=("\n"+m);//new line character is used here
for medical_history
}
public String getName(){
return this.name;
}
public String getBreed(){//getter and setter methods
return this.breed;
}
public String getBirthDate(){
return this.birth_date;
}
public String getMedicalHistory(){
return this.medical_history;
}
public int getWeight(){
return this.weight;
}
public void setName(String n){
this.name=n;
}
public void getBreed(String d){
this.breed=d;
}
public void getBirthDate(String d){
this.birth_date=d;
}
public void getMedicalHistory(String m){
this.medical_history+=("\n"+m);
}
public void setWeight(int w){
this.weight=w;
}
public String toString(){//string representation of Dog class
return "Name is: "+this.name+" Breed is: "+this.breed+" birth date
is: "+this.birth_date+" Weight is: "+this.weight+" Medical History:
"+this.medical_history;
}
}
public class Vet
{
public static void main(String[] args) {
Dog d1=new
Dog("name1","breed1","date1","Healthy",20);//create a dog object
and print it
System.out.println(d1);
d1.setName("Pinky");//update the
name of dog and print the dog details
System.out.println(d1);
}
}
Screenshots:
The screenshots are attached below for reference.
Please follow them for output.
Please upvote my answer. Thank you.
See that name of the dog is changed to pinky here.
Get Answers For Free
Most questions answered within 1 hours.