Question

Using Java Write a class called Dog that contains instance data that represents the dog’s name,...

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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create java class with name Dog. Instructions for Dog class:              This class is modeled after...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after a Dog. You should have instance variables as follows: The Dog’s name The number of tricks known by the Dog. The color*of the Dog’s coat (fur). *: For the Dog’s fur color you should use the Color class from the java.awt package. You should choose proper types and meaningful identifiers for each of these instance variables. Your class should contain two constructor methods. A...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of type String, custName of type String and checkingBalance of type double and savingBalance of type double 2)Write the constructor for CustomerAccount class. The constructor takes parameters to initialize custID, custName, checkingBalance and savingBalance. 3)Write the mutator method for all attributes of the class CustomerAccount. then Write the toString method for class CustomerAccount.
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT