Answered step by step
Verified Expert Solution
Link Copied!

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 petsWithNoNamevariable initialized to 0 in PetRecordand update

1.Read the compilation error in PetRecord.java and fix it by changing the method declaration

2.Add a public static petsWithNoNamevariable initialized to 0 in PetRecordand 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 petsWithNoNamevariable by direct class access and object access

/*************************************************** * Class for basic pet records: name, age, and weight. ***************************************************/ 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)

public class PetMain { public static void main(String[] args) { // TODO: print the value of petsWithNoName by direct access via the class name 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 } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions