Answered step by step
Verified Expert Solution
Question
1 Approved Answer
TestPetHierarchy Test Harness /* * Lab 03 * * Test harness provided to students to test class hierarchy 'Pet'. * * MAKE NO CHANGES TO
TestPetHierarchy Test Harness
/* * 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 %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 Thank you!" ); } // end main } // end TestPetHierarchy
Sample output
Lab 03 SuperClasses and SubClasses General Information Use an existing class (Pet) as a superclass; code two subclasses (Mamma1 and Reptile) to the superclass. Use basic polymorphic processing to test the class hierarchy. Instructions Use your (complete & correct) copy of Pet from Lab 02 as a starting point for this lab. Create two new subclasses, Mammal and Reptile, based on the superclass Pet, according to the UML Class Diagrams provided. Zip the two (2) source files (Mammal.java, and Reptile.java) into folder named Lab03 Submission. Include source code for Mammal and Reptile only in the zipped folder. DO NOT include the test harness, Pet.java, edit/backup versions of the source code, or compiled code. Submit only the zipped folder Lab03 Submission as an attachment to this assignment. Submission in any format other than a zipped folder will result in a grade of zero (0). Provided to You The following resources are provided: These instructions The UML Class Diagrams for the Pet hierarchy, including Mammal and Reptile classe:s . Source code for a test harness to use in testing your code . Sample output from the provided test harness Requirements Code the Mammal and Reptile subclasses per the provided UML Follow Basic Coding Standards, as published on Blackboard In the subclass tostring methods, DO NOT "hard code" the class name! Retrieve the class name in each instance from the system. (Hint: Remember, an object always knows its class.) Pet Attributes private name: String private species: String private parent: String private birthday: String Pet's name Pet's species (NOTE: not breed) Pet's parent (owner) name Pet's birthday in yyyy/mm/dd format: 2017/02/28 Operations public Pet ): Pet public > Pet (name: String, Default or null constructor; returns a reference to a fully functional Pet object Constructor accepting four (4) parameters, all of type String. Returns areference to a fully formed and functional Pet object species: String, parent: String birthday: String): Pet public toString):Strin Returns a formatted String containing a verbose description of the Pet object NOTE: Set and get methods are assumed; further, standard naming is assumed for these methods. Sample Test Harness Output Legend Prompts User Input Output to Console Interactions Please enter the pet's name: Cocoa Please enter the pet's parent's name Jean Davis Please enter the pet's species: Dog Please enter the pet's birthday in yyyy/mm/dd format: 2012/07/12 Is the pet a Mammal (1) or a Reptile (2)? Please enter a list of the Mammal's required vaccinations: Rabies, Distemper, Parvo Cocoa, whose fur-parent is Jean Davis is a(n) Dog born on 2012/07/12. This Mammal requires the following vaccinations: Rabies, Distemper, Parvo. Do you wish to enter another Pet? (Yes or No) yes Please enter the pet's name Scooter Please enter the pet's parent's name: Gina DeLeon Please enter the pet's species: Sulkata Tortoise Please enter the pet's birthday in yyyy/mm/dd format: 1985/08/26 Is the pet a Mammal (1) or a Reptile (2)? Please enter a description of the Reptile's required environment: Warm, dry, large space Scooter, whose fur-parent is Gina DeLeon, is a Sulkata Tortoise born on 1985/08/26. The Reptile's required environment is Warn, dry, large space. Do you wish to enter another Pet? (Yes or No) no Thank youStep 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