Question

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

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( "[\\n\\r]" );               // 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
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 parent, String species, 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;
    }

}

class Mammal extends Pet{
    String Vaccination="";
    //parameterized Constructor with five arguments
    public Mammal(String name, String parent, String species, String birthday, String Vaccination) {
        //Here i am accessing the super class Constructor and setting the variables.Because Since the 4 variable are
        //common in between parent class and child class we directly access the variable if all the variable are public.
        //since all the variable are private we cannot access these in child class so we are calling the super class constructor to set those variables.
        super(name,parent,species, birthday);
        this.Vaccination=Vaccination;
    }

    public String getVaccination() {
        return Vaccination;
    }

    public void setVaccination(String Vaccination) {
        this.Vaccination = Vaccination;
    }

    //this method will override the super class method.Here since all the variables are private we are calling
    //super class tostring method and return a string from the super class method and append the remaining details
    //in th child class method
    @Override
    public String toString() {
        return super.toString()+". This Mammal requires following vaccinations "+ Vaccination;
    }
}
//Reptile class
class Reptile extends Pet{

    String environment="";
    //Here i am accessing the super class Constructor and setting the variables.Because Since the 4 variable are
    //common in between parent class and child class we directly access the variable if all the variable are public.
    //since all the variable are private we cannot access these in child class so we are calling the super class constructor to set those variables.
    public Reptile(String name, String parent, String species , String birthday, String environ) {
        super(name,parent,species, birthday);
        this.environment=environ;
    }

    public String getEnvironment() {
        return environment;
    }

    public void setEnvironment(String environment) {
        this.environment = environment;
    }

    //this method will override the super class method.Here since all the variables are private we are calling
    //super class tostring method and return a string from the super class method and append the remaining details
    //in th child class method
    @Override
    public String toString() {
        return super.toString()+". The Reptile's required environment is "+ environment;
    }
}

                        // 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( "[\\n\\r]" );               // 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,
                            species,
                            birthday,
                            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,
                            species,
                            birthday,
                            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
}
Add a comment
Know the answer?
Add Answer to:
public class Pet {    //Declaring instance variables    private String name;    private String species;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS 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...

  • I need help with this Mammal: public class Mammal extends Pet{     String Vaccin...

    I need help with this Mammal: public class Mammal extends Pet{     String Vaccination="";     public Mammal(String name, String parent, String species, String birthday, String Vaccination) {           //Accessing the super class Constructor and setting the variables.         super(name,parent,species, birthday);         this.Vaccination=Vaccination;     } // end Mammal     public String getVaccination() {         return Vaccination;     } // end getVaccination     public void setVaccination(String Vaccination) {         this.Vaccination = Vaccination;     } // end setVaccination     @Override     public String toString() {         return super.toString()+". This Mammal requires following vaccinations "+ Vaccination;     } // end...

  • *PLEASE HELP ASAP* Pet.java /* * Class Pet * * * */ public class Pet {...

    *PLEASE HELP ASAP* Pet.java /* * Class Pet * * * */ public class Pet { /* * Instance variables */ private String name; private String species; private String parent; private String birthday; /* * Constructors */ public Pet( ) { // This is a null or default constructor } //e end null constructor public Pet( String aName, String aSpecies, String aParent, String aBDay ) { setName( aName ); setSpecies( aSpecies ); setParent( aParent ); setBirthday( aBDay ); } //...

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...

    GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

  • class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i...

    class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i = i;} public void set(Upper n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(int i){ super(i+1); name = "Middle"; this.j = i;} public void set(Upper n){ j = n.show();} public int show(){return j;} } class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT