Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. Read the compilation error in PetRecord.java and fix it by changing the method declaration 2. Add a public static petsWithNoName variable initialized to 0
1. Read the compilation error in PetRecord.java and fix it by changing the method declaration
2. Add a public static petsWithNoName variable initialized to 0 in PetRecord and update its value in each constructor that does not set the pets name
3. In PetMain.java, follow the TODO instructions to add print statements that show the value of the petsWithNoName variable by direct class access and object access
public class PetMain { public static void main(String[] args) { // TODO: print the value of petsWithNoName by direct access via the class name PetName.java PetRecord[] pet = new PetRecord[10]; pet[0] = new PetRecord(); pet[1] = new PetRecord("Gruff"); pet[2] = new PetRecord(8); pet[3] = new PetRecord(4.6); pet[4] = new PetRecord("Rex", 12, 87.3); pet[5] = new PetRecord(5); pet[6] = new PetRecord(25.3); pet[7] = new PetRecord("Lassie"); pet[8] = new PetRecord("Brownie", 2, 15.6); pet[9] = new PetRecord(); // TODO: print the value of petsWithNoName by direct access via the class name // TODO: print the value of petsWithNoName for each of the 10 PetRecord objects in the array } }
PetRecord.java
public class PetRecord { private String name; private int age;//in years private double weight;//in pounds // TODO: add a public static petsWithNoName variable here and update it in the appropriate constructors below // TODO: this won't compile. fix it and add a comment as to why public static void writeOutput() { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); System.out.println("Weight: " + weight + " pounds"); } public PetRecord(String initialName, int initialAge, double initialWeight) { name = initialName; if ((initialAge < 0) || (initialWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = initialAge; weight = initialWeight; } } public void set(String newName, int newAge, double newWeight) { name = newName; if ((newAge < 0) || (newWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = newAge; weight = newWeight; } } public PetRecord(String initialName) { name = initialName; age = 0; weight = 0; } public void set(String newName) { name = newName; //age and weight are unchanged. } public PetRecord(int initialAge) { name = "No name yet."; weight = 0; if (initialAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = initialAge; } public void set(int newAge) { if (newAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = newAge; //name and weight are unchanged. } public PetRecord(double initialWeight) { name = "No name yet"; age = 0; if (initialWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = initialWeight; } public void set(double newWeight) { if (newWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = newWeight; //name and age are unchanged. } public PetRecord() { name = "No name yet."; age = 0; weight = 0; } public String getName() { return name; } public int getAge() { return age; } public double getWeight() { return weight; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started