Write a program that creates Pet objects from data read from the keyboard. Store
ID: 3659511 • Letter: W
Question
Write a program that creates Pet objects from data read from the keyboard. Store these objects into an instance of ArrayList. Then sort the Pet objects into alphabetic order by pet name, and finally display the data in the sorted Pet objects on the screen. It should check for bad input and display an error and repeat the process until it is input correctly. Use a while loop to allow user to put in as many pet records as wanted. public class Pet { private String name; private int age; //in years private double weight; //in pounds /** This main is just a demonstration program. */ public static void main(String[] args) { Pet myDog = new Pet( ); myDog.set("Fido", 2, 5.5); myDog.writeOutput( ); System.out.println("Changing name."); myDog.set("Rex"); myDog.writeOutput( ); System.out.println("Changing weight."); myDog.set(6.5); myDog.writeOutput( ); System.out.println("Changing age."); myDog.set(3); myDog.writeOutput( ); } public void writeOutput( ) { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); System.out.println("Weight: " + weight + " pounds"); } public void set(String newName) { name = newName; //age and weight are unchanged. } public void set(int newAge) { if (newAge <= 0) { System.out.println("Error: illegal age."); System.exit(0); } else age = newAge; //name and weight are unchanged. } public void set(double newWeight) { if (newWeight <= 0) { System.out.println("Error: illegal weight."); System.exit(0); } else weight = newWeight; //name and age are unchanged. } public void set(String newName, int newAge, double newWeight) { name = newName; if ((newAge <= 0) || (newWeight <= 0)) { System.out.println("Error: illegal age or weight."); System.exit(0); } else { age = newAge; weight = newWeight; } } public String getName( ) { return name; } public int getAge( ) { return age; } public double getWeight( ) { return weight; } }Explanation / Answer
public class PetRecord { private String name; private int age;//in years private double weight;//in pounds public String toString( ) { return ("Name: " + name + " Age: " + age + " years" + " 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 setName(String newName) { name = newName; } 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 setAge(int newAge) { if (newAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = newAge; } 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 setWeight(double newWeight) { if (newWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = newWeight; } 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; } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.