149k views
4 votes
Define a method printAll() for class PetData that prints output as follows. Hint: Make use of the base class' printAll() method.

Name: Fluffy, Age: 5, ID: 4444
__________________________________

// ===== Code from file AnimalData.java =====
public class AnimalData {
private int ageYears;
private String fullName;

public void setName(String givenName) {
fullName = givenName;
return;
}

public void setAge(int numYears) {
ageYears = numYears;
return;
}

// Other parts omitted

public void printAll() {
System.out.print("Name: " + fullName);
System.out.print(", Age: " + ageYears);
return;
}
}
// ===== end =====

// ===== Code from file PetData.java =====
public class PetData extends AnimalData {
private int idNum;

public void setID(int petID) {
idNum = petID;
return;
}

// FIXME: Add printAll() member function

/* Your solution goes here */

}
// ===== end =====

// ===== Code from file BasicDerivedOverride.java =====
public class BasicDerivedOverride {
public static void main (String [] args) {
PetData userPet = new PetData();

userPet.setName("Fluffy");
userPet.setAge (5);
userPet.setID (4444);
userPet.printAll();
System.out.println("");

return;
}
}
// ===== end =====

2 Answers

3 votes

Final answer:

To add the ID to the printAll() method in the PetData class, you can inherit the method from the AnimalData class and then add code to print the ID.

Step-by-step explanation:

This question is about defining a method printAll() for the class PetData that prints specific output. In the provided code, the AnimalData class has a printAll() method that prints the name and age of an animal. The goal is to add a printAll() method to the PetData class that also includes the ID of the pet. To achieve this, the PetData class needs to inherit the printAll() method from the AnimalData class and then add code to print the ID.

Here is the solution for the printAll() method in the PetData class:

public void printAll() {
super.printAll();
System.out.println(", ID: " + idNum);
return;
}

This code first calls the printAll() method from the base class using the super keyword. Then we add code to print the ID of the pet. Finally, we add a new line character to separate the outputs.

User Joshua Green
by
5.2k points
4 votes

Answer:

// ===== Code from file AnimalData.java =====

public class AnimalData {

private int ageYears;

private String fullName;

public void setName(String givenName) {

fullName = givenName;

return;

}

public void setAge(int numYears) {

ageYears = numYears;

return;

}

// Other parts omitted

public void printAll() {

System.out.print("Name: " + fullName);

System.out.print(", Age: " + ageYears);

return;

}

}

// ===== end =====

// ===== Code from file PetData.java =====

public class PetData extends AnimalData {

private int idNum;

public void setID(int petID) {

idNum = petID;

return

}

// FIXME: Add printAll() member function

@override

Void printAll()

{

AnimalData a1=new AnimalData();

a1.printall();

System.out.print(“id/;” +idNum);

}

}

// ===== end =====

// ===== Code from file BasicDerivedOverride.java =====

public class BasicDerivedOverride {

public static void main (String [] args) {

PetData userPet = new PetData();

userPet.setName("Fluffy");

userPet.setAge (5);

userPet.setID (4444);

userPet.printAll();

System.out.println("");

return;

}

}

// ===== end =====

Step-by-step explanation:

In the program the main thing is overriding, and this condition mentioned in the program is perfect to explain what the overriding actually means in Java. All object oriented programming language supports overriding. And remember we can also create another class main and call the overriding printAll() method from the child class as its class is being inherited by the PetData class.

User Peter Pajchl
by
6.0k points