Question

Code in JAVA

Requirements The provided UML Class Diagrams explain the requirements for the changes to Pet.java and creation of the subclasSugar, whose parent is Gene, was born on 2001/03/18. The Mammal is a(n) Dog. Required vaccinations include Rabies, Distemper,

UML

Pet Attributes private name: String Pets name private owner: String private birthday: String Pets owners (fur-parents) naMammal (subclass of Pet) Attributes private species: String Pets species (NOTE: not breed) Date of annual vaccinations in yyReptile (subclass of Pet) Attributes Pets species (NOTE: not breed) Describes reguired environment: hot, dry /hot, damp /war

//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 );       // 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.
            owner,                                  // 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
    
    String  prompt1  = "Please enter the pet's name: ";
    String  prompt2  = "Please enter the pet's owner's name: ";
    String  prompt3  = "Please enter the pet's birthday in yyyy/mm/dd format: ";
    String  prompt4  = "Is the pet a Mammal (1) or a Reptile (2)? ";
    String  promptR1 = "Please enter the Reptile's species: ";
    String  promptR2 = "Please enter a description of the Reptile's required environment: ";
    String  promptM1 = "Please enter the Mammal's species: ";
    String  promptM2 = "Please enter the date of next required vaccinations in yyyy/mm/dd format: ";
    
    
    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( prompt1 );
      name     = input.next(  );
      System.out.println( prompt2 );
      owner    = input.next( );                       
      System.out.println( prompt3 );
      birthday = input.next( );
      
      // Prompt as to whether this Pet will be a Mammal or a Reptile
      System.out.println( prompt4 );
      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( promptR1 );
        species  = input.next( );
        System.out.println( promptR2 );
        enviro   = input.next( );
        
        spot = new Reptile( name,                  // Actual instantiation of the Reptile object
                            owner,
                            birthday,
                            species,
                            enviro );
      }  // complete Reptile
      else 
      {
        // Finish collecting information to instantiate a Mammal object
        System.out.println( promptM1 );
        species  = input.next(  ); 
        
        System.out.println( promptM2 );
        vax      = input.next( );
        
        spot = new Mammal( name,                   // Actual instantiation of the Mammal object
                           owner,
                           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.equalsIgnoreCase( "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
Requirements The provided UML Class Diagrams explain the requirements for the changes to Pet.java and creation of the subclasses. In addition to the requirements described there, be aware of the requirements included in the Coding Standards. Specifically be aware of the following All set and get methods must use standard naming (i.e. "set" or "get" followed by the instance variable name, adjusted for proper camel casing) All get and set methods must be 'final' methods The only direct access to instance variable values is through the set and get methods; no other direct access is every permitted. Expected toString Output Your output need not be identical to that shown here, but you MUST include all instance values in the output, in the order shown. Highlighted portions of the output originate in the superclass toString method
Sugar, whose parent is Gene, was born on 2001/03/18. The Mammal is a(n) Dog. Required vaccinations include Rabies, Distemper, Parvo Rosco, whose parent is Bobby, was born on 206/07/18. The Reptile is a (n) Monitor Lizard. Its required environment is Dry, sunny, extra warm
Pet Attributes private name: String Pet's name private owner: String private birthday: String Pet's owner's (fur-parent's) name Pet's birthday in yyyy/mm/dd format: 2019/02/28 Operations Constructor» Pet (): Pet constructor» Pet (name: String, Default Constructor accepting three (3) parameters, all of type String. Returns a fully formed and fully functional Pet object reference null constructor owner: String, birthday: String ) : Pet public setName( aName: String): void public setOwner(anOwner: String): void public setBirthday( aBDay: String ): void public getName(): String Accepts a String object (local name: aName) and assigns it to the instance variable name Accepts a String object (local name: an0wner) and assigns it to the instance variable owner Accepts a String object (local name: aBDay) and assigns it to the instance variable birthday Returns the current value of instance variable name Returns the current value of instance variable parent Returns the current value of instance variable birthday public getParent): String public getBirthday): String public toString(): String Returns a formatted String containing a 'state report', that is, the current values of each of the object's instance variables NOTE: The instance variable species has been "demoted" to the subclasses.
Mammal (subclass of Pet) Attributes private species: String Pet's species (NOTE: not breed) Date of annual vaccinations in yyyy/mm/dd format private vax: String Operations «constructor» Mammal () Mammal constructor» Mammal (name: String, Default or null constructor Constructor accepting five (5) parameters, all of type String. Returns a fully formed and fully functional Mammal object reference. parent: String birthday: String, pecies: String, vax: String Mammal public setSpecies( aSpecies: String ): void Accepts a String object (local name: aSpecies) and assigns it to the instance variable species Accepts a String object (local name: aDate) and assigns it to the instance variable vax Returns the current value of instance variable species Returns the current value of instance variable vax public setVax( aDate: String): void public getSpecies): String public getVax: String public toString): String Returns a formatted String containing a 'state report', that is, the current values of each of the object's instance variables; calls superclass toString method for first portion of formatted output.
Reptile (subclass of Pet) Attributes Pet's species (NOTE: not breed) Describes reguired environment: hot, dry /hot, damp /warm, water, etc. private species: String ico Strine oriuate private enviro: String Operations constructor» Reptile () Reptile «constructor» Reptile (name: String, Default or null constructor Constructor accepting five (5) parameters, all of type String. Returns a fully formed and fully functional Reptile object reference. parent: String, birthday: String, species: String, enviro: String ): Reptile public setSpecies( aSpecies: String ): void public setEnviro( anEnviro: String): void public getSpecies(): String public getEnvir): String Accepts a String object (local name: aSpecies) and assigns it to the instance variable species Accepts a String object (local name: anEnviro) and assigns it to the instance variable enviro Returns the current value of instance variable species Returns the current value of instance variable enviro public toString(): String Returns a formatted String containing a 'state report', that is, the current values of each of the object's instance variables; calls superclass toString method for first portion of formatted output
0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA PROGRAMS

Please find below the 3 classes that are used In the test harness class given (the given main class is not written here):

Pet class

package pet;

/**
class Pet
*
*/
public class Pet { //parent class
  
   private String name;
   private String owner;
   private String birthday;
   public Pet() {
       //default constructor
   }
  
   //parametrized
   public Pet(String name, String owner, String birthday) {
       this.name = name;
       this.owner = owner;
       this.birthday = birthday;
   }
   public final String getName() {
       return name;
   }
   public final void setName(String aName) {
       this.name = aName;
   }
   public final String getOwner() {
       return owner;
   }
   public final void setOwner(String anOwner) {
       this.owner = anOwner;
   }
   public final String getBirthday() {
       return birthday;
   }
   public final void setBirthday(String aBDay) {
       this.birthday = aBDay;
   }
   //parent's toString() method
   @Override
   public String toString() {
       return this.name+ ", whose parent is "+ this.owner+", was born on "+this.birthday+".";
   }
  
}

Mammal class

package pet;
/**
class Mammal exends Pet
*
*/
public class Mammal extends Pet{
   private String species;
   private String vax;
  
   //default
   public Mammal(){
       super();
   }
  
   //parametrized
   public Mammal(String name, String owner, String birthday,String species,String vax){
       super(name,owner,birthday);
       this.species = species;
       this.vax = vax;
   }

   public String getSpecies() {
       return species;
   }

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

   public String getVax() {
       return vax;
   }

   public void setVax(String aDate) {
       this.vax = aDate;
   }

   //overridden toString
   @Override
   public String toString() {
       return super.toString()+ " The Mammal is a(n) \n"+this.species+"."+ " Required vaccination date is "+this.vax+".";
   }
  
  
}

Reptile class

package pet;

//class Reptile extending Pet
public class Reptile extends Pet {
   private String species;
   private String enviro;
   //default
   public Reptile() {
       super();
   }
  
   //parametrized
   public Reptile(String name, String owner, String birthday,String species,String enviro){
       super(name,owner,birthday);
       this.species=species;
       this.enviro = enviro;
   }

   public final String getSpecies() {
       return species;
   }

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

   public final String getEnviro() {
       return enviro;
   }

   public final void setEnviro(String anEnviro) {
       this.enviro = anEnviro;
   }

   //overridden toString()
   @Override
   public String toString() {
       return super.toString()+ " The Reptile is \n"+this.species+". "+"Its required environment is "+this.enviro;
   }
  
  
}

OUTPUT

Please enter the pet's name:
Sugar
Please enter the pet's owner's name:
Gene
Please enter the pet's birthday in yyyy/mm/dd format:
2011/01/11
Is the pet a Mammal (1) or a Reptile (2)?
1
Please enter the Mammal's species:
dog
Please enter the date of next required vaccinations in yyyy/mm/dd format:
2019/09/10

       Sugar, whose parent is Gene, was born on 2011/01/11. The Mammal is a(n)
dog. Required vaccination date is 2019/09/10.

Do you wish to enter another Pet? (Yes or No)yes
Please enter the pet's name:
Rosco
Please enter the pet's owner's name:
Bobby
Please enter the pet's birthday in yyyy/mm/dd format:
2012/03/12
Is the pet a Mammal (1) or a Reptile (2)?
2
Please enter the Reptile's species:
turtle
Please enter a description of the Reptile's required environment:
damp,water

       Rosco, whose parent is Bobby, was born on 2012/03/12. The Reptile is
turtle. Its required environment is damp,water

Do you wish to enter another Pet? (Yes or No)no


       Thank you!

Add a comment
Know the answer?
Add Answer to:
Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...
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
  • 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;   ...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • *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 ); } //...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • 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...

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
ADVERTISEMENT