Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that a service class named Dog has been developed. The Dog class includes

ID: 3838151 • Letter: A

Question

Assume that a service class named Dog has been developed. The Dog class includes an attribute named breed and an attribute named weight. The class also includes two constructors and accessor and mutator methods for breed and weight. Use the Dog class to answer the following questions. Write the Java code to declare a Dog object named snoopy. Write the Java code to instantiate the snoopy object using the default constructor. Write the Java code to declare a Dog object named rover. Write the Java code to instantiate the rover object uses the overloaded constructor (the constructor that has a parameter for the breed attribute followed by the parameter for the weight attributel to initialize the breed to "Collie" and the weight to 12. Write the Java code to make a call to the mutator method for weight to change the weight of snoopy to 15. Write the Java code that will use the accessor method for breed to write a statement that will display the words "Snoopy's breed" is followed by the breed of snoopy.

Explanation / Answer

/*
@attribute breed,weight
*/
public class Dog {
private String breed;
private int weight;
/*
Default Constructor
*/
public Dog(){
}
/*
Constructor to instantiate the attributes
breed and weight
*/
public Dog(String breed, int weight){
this.breed = breed;
this.weight = weight;
}

public void setBreed(String breed) {
this.breed = breed;
}

public void setWeight(int weight) {
this.weight = weight;
}

public String getBreed() {
return breed;
}

public int getWeight() {
return weight;
}

  
public static void main(String[] args) {
Dog snoopy; //Declaration of Dog object named snoopy
snoopy = new Dog(); //Instantiation of snoopy object using default constructor
Dog rover; // Declaration of Dog object named rover
rover = new Dog("Collie", 12); // Instantiation of attributes - breed as Collie and weight as 12 in rover object
snoopy.setWeight(15); // calling mutator method to set the weight of snoopy object
System.out.println("Snoopy's breed is "+snoopy.getBreed());
}
  
}