Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

public class Pet { //Declaring instance variables private String name; private String species; private String parent; private String birthday; //Zero argumented constructor public Pet() {

public class Pet { //Declaring instance variables private String name; private String species; private String parent; private String birthday;

//Zero argumented constructor public Pet() { }

//Parameterized constructor public Pet(String name, String species, String parent, String birthday) { this.name = name; this.species = species; this.parent = parent; this.birthday = birthday; }

// getters and setters public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getSpecies() { return species; }

public void setSpecies(String species) { this.species = species; }

public String getParent() { return parent; }

public void setParent(String parent) { this.parent = parent; }

public String getBirthday() { return birthday; }

public void setBirthday(String birthday) { this.birthday = birthday; }

//toString method is used to display the contents of an object inside it @Override public String toString() { return name + ", whose fur-parent is " + parent + " is a " + species + ", born on " + ", birthday=" + birthday; }

}

Pet Hierarchy:

/* * Lab 03 * * Test harness provided to students to test class hierarchy 'Pet'. * * MAKE NO CHANGES TO THIS TEST HARNESS! * * * (c) 2017, Terri Davis */ import java.util.Scanner; // Scanner class to support user input

public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in ); // Set the keyboard as the input device for this process input.useDelimiter( "[\ \ ]" ); // Correction to allow smoother keyboard input here String name, // These String objects will hold the user input to be used species, // when calling the full constructor method of class Pet. parent, // For simplicity, we have used the same names here as the birthday, // names given to the instance variables in the Class definitions. vax, enviro; boolean again = true; // This boolean controls the while loop we will use. String response; // This String holds the user's response re: continuing the loop. int type = 1; // This int will indicate whether the pet is a Mammal or Reptile Pet spot = new Pet( ); // This is simply a 'holding spot' for the object(s) we create. while( again ) // Continue the loop until the user says otherwise... { // Collect necessary and basic Pet input from the user System.out.println( "Please enter the pet's name: " ); name = input.next( ); System.out.println( "Please enter the pet's parent's name: " ); parent = input.next( ); System.out.println( "Please enter the pet's birthday in yyyy/mm/dd format: " ); birthday = input.next( ); // Prompt as to whether this Pet will be a Mammal or a Reptile System.out.println( "Is the pet a Mammal (1) or a Reptile (2)? " ); type = input.nextInt( ); if( type == 2) // Unless we know it's a Reptile, we'll assume Mammal... { // Finish collecting information to instantiate a Reptile object System.out.println( "Please enter the Reptile's species: " ); species = input.next( ); System.out.println( "Please enter a description of the Reptile's required environment: " ); enviro = input.next( ); spot = new Reptile( name, // Actual instantiation of the Reptile object parent, birthday, species, enviro ); } // complete Reptile else { // Finish collecting information to instantiate a Mammal object System.out.println( "Please enter the Mammal's species: " ); species = input.next( ); System.out.println( "Please enter a list of the Mammal's required vaccinations: " ); vax = input.next( ); spot = new Mammal( name, // Actual instantiation of the Mammal object parent, birthday, species, vax ); } // complete Mammal // Using the object named 'spot', call the toString method and output the results System.out.printf( "%n\t\t%s", spot.toString( ) ); // Ask if the user wants to enter another Pet System.out.printf( "%n%nDo you wish to enter another Pet? (Yes or No)" ); response = input.next( ); // Test the user response if( response.equals( "No" ) ) again = false; } // end while loop // Let user know we are finished System.out.printf( "%n%n\t\t Thank you!" ); } // end main } // end TestPetHierarchy

Question:

https://www.chegg.com/homework-help/questions-and-answers/get-help-link-pet-class-pet-hierachy-https-wwwcheggcom-homework-help-questions-answers-pub-q34669581

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_2

Step: 3

blur-text-image_3

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

Case Studies In Business Data Bases

Authors: James Bradley

1st Edition

0030141346, 978-0030141348

More Books

Students explore these related Databases questions