Question

LAB: Pet information (derived classes)

LAB: Pet information (derived classes)

The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:

  • create a generic pet and print information using printInfo().

  • create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.

Ex. If the input is:

Dobby
2
Kreacher
3
German Schnauzer

the output is:

Pet Information: 
   Name: Dobby
   Age: 2
Pet Information: 
   Name: Kreacher
   Age: 3
   Breed: German Schnauzer


PetInformation.java

import java.util.Scanner;

public class PetInformation {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      Pet myPet = new Pet();
      Dog myDog = new Dog();
      
      String petName, dogName, dogBreed;
      int petAge, dogAge;

      petName = scnr.nextLine();
      petAge = scnr.nextInt();
      scnr.nextLine();
      dogName = scnr.next();
      dogAge = scnr.nextInt();
      scnr.nextLine();
      dogBreed = scnr.nextLine();
      
      // TODO: Create generic pet (using petName, petAge) and then call printInfo       
      
      // TODO: Create dog pet (using dogName, dogAge, dogBreed) and then call printInfo
      
      // TODO: Use getBreed(), to output the breed of the dog
      
   }
}


Pet.java

public class Pet {

   protected String petName;
   protected int petAge;

   public void setName(String userName) {
      petName = userName;
   }

   public String getName() {
      return petName;
   }

   public void setAge(int userAge) {
      petAge = userAge;
   }

   public int getAge() {
      return petAge;
   }

   public void printInfo() {
      System.out.println("Pet Information: ");
      System.out.println("   Name: " + petName);
      System.out.println("   Age: " + petAge);
   }

}


Dog.java

public class Dog extends Pet {
   private String dogBreed;

   public void setBreed(String userBreed) {
      dogBreed = userBreed;
   }

   public String getBreed() {
      return dogBreed;
   }
}


2 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

PetInformation.java

import java.util.Scanner;

public class PetInformation {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      Pet myPet = new Pet();
      Dog myDog = new Dog();
      
      String petName, dogName, dogBreed;
      int petAge, dogAge;

      petName = scnr.nextLine();
      petAge = scnr.nextInt();
      scnr.nextLine();
      dogName = scnr.next();
      dogAge = scnr.nextInt();
      scnr.nextLine();
      dogBreed = scnr.nextLine();
      
      // TODO: Create generic pet (using petName, petAge) and then call printInfo
      
      myPet.setName(petName);
      myPet.setAge(petAge);
      myPet.printInfo();
      
      // TODO: Create dog pet (using dogName, dogAge, dogBreed) and then call printInfo
      
      myDog.setName(dogName);
      myDog.setAge(dogAge);
      myDog.printInfo();
      
      // TODO: Use getBreed(), to output the breed of the dog
      
      myDog.setBreed(dogBreed);
      myDog.getBreed();
      System.out.println("   Breed: " + dogBreed);
      
   }
}


Pet.java

public class Pet {

   protected String petName;
   protected int petAge;

   public void setName(String userName) {
      petName = userName;
   }

   public String getName() {
      return petName;
   }

   public void setAge(int userAge) {
      petAge = userAge;
   }

   public int getAge() {
      return petAge;
   }

   public void printInfo() {
      System.out.println("Pet Information: ");
      System.out.println("   Name: " + petName);
      System.out.println("   Age: " + petAge);
   }

}


Dog.java

public class Dog extends Pet {
   private String dogBreed;

   public void setBreed(String userBreed) {
      dogBreed = userBreed;
   }

   public String getBreed() {
      return dogBreed;
   }
}


> Can you kindly write this code in python

kokoniyen Wed, Apr 7, 2021 8:31 AM

Add a comment
Know the answer?
Add Answer to:
LAB: Pet information (derived classes)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • LAB: Instrument information (derived classes)

    10.14 LAB: Instrument information (derived classes)Given main() and the Instrument class, define a derived class, StringInstrument, for string instruments.Ex. If the input is:Drums Zildjian 2015 2500 Guitar Gibson 2002 1200 6 19the output is:Instrument Information:     Name: Drums    Manufacturer: Zildjian    Year built: 2015    Cost: 2500 Instrument Information:     Name: Guitar    Manufacturer: Gibson    Year built: 2002    Cost: 1200    Number of strings: 6    Number of frets: 19InstrumentInformation.javaimport java.util.Scanner; public class InstrumentInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Instrument myInstrument = new Instrument();       StringInstrument myStringInstrument = new StringInstrument();       String instrumentName, manufacturerName, stringInstrumentName, stringManufacturer;       int yearBuilt, cost, stringYearBuilt, stringCost, numStrings, numFrets;       instrumentName = scnr.nextLine();       manufacturerName = scnr.nextLine();       yearBuilt = scnr.nextInt();       scnr.nextLine();       cost = scnr.nextInt();       scnr.nextLine();       stringInstrumentName = scnr.nextLine();       stringManufacturer = scnr.nextLine();       stringYearBuilt = scnr.nextInt();       stringCost = scnr.nextInt();       numStrings = scnr.nextInt();       numFrets = scnr.nextInt();       myInstrument.setName(instrumentName);       myInstrument.setManufacturer(manufacturerName);       myInstrument.setYearBuilt(yearBuilt);       myInstrument.setCost(cost);       myInstrument.printInfo();       myStringInstrument.setName(stringInstrumentName);       myStringInstrument.setManufacturer(stringManufacturer);       myStringInstrument.setYearBuilt(stringYearBuilt);       myStringInstrument.setCost(stringCost);       myStringInstrument.setNumOfStrings(numStrings);       myStringInstrument.setNumOfFrets(numFrets);       myStringInstrument.printInfo();       System.out.println("   Number of strings: " + myStringInstrument.getNumOfStrings());       System.out.println("   Number of frets: " + myStringInstrument.getNumOfFrets());    } }Instrument.javapublic class Instrument {     protected String instrumentName;     protected String instrumentManufacturer;     protected int yearBuilt, cost;     public void setName(String userName) {...

  • LAB: Book information (overriding member methods)

    10.15 LAB: Book information (overriding member methods)Given main() and a base Book class, define a derived class called Encyclopedia. Within the derived Encyclopedia class, define a printInfo() method that overrides the Book class' printInfo() method by printing not only the title, author, publisher, and publication date, but also the edition and number of volumes.Ex. If the input is:The Hobbit J. R. R. Tolkien George Allen & Unwin 21 September 1937 The Illustrated Encyclopedia of the Universe James W. Guthrie Watson-Guptill 2001 2nd 1the output is:Book Information:     Book Title: The Hobbit    Author: J. R. R. Tolkien    Publisher: George Allen & Unwin    Publication Date: 21 September 1937 Book Information:     Book Title: The Illustrated Encyclopedia of the Universe    Author: James W. Guthrie    Publisher: Watson-Guptill    Publication Date: 2001    Edition: 2nd    Number of Volumes: 1Note: Indentations use 3 spaces.BookInformation.javaimport java.util.Scanner; public class BookInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Book myBook = new Book();...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • LAB: Plant information (ArrayList)

    10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10  flower Hydrangea 30 false lilac  flower Rose 6 false white plant Mint 4...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • 9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...

    9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...

  • solve it in c++ 10 note: please do not give me same answer like this 1....

    solve it in c++ 10 note: please do not give me same answer like this 1. Define a Pet class that stores the pet's name, age, and weight. Add appropriate constructors, accessor functions, and mutator functions. Also define a function named getLifespan that returns a string with the value "unknown lifespan." Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add...

  • [c++] Given the following simplified classes, which of the statements are not legal? Explain your answer...

    [c++] Given the following simplified classes, which of the statements are not legal? Explain your answer your answer in detail. class Pet { public:     virtual void print();     string name; private: }; class Dog: public Pet { public:    void print();    string breed; }; //code below is in the main() Dog dog; Pet   pet; dog.name = "rover"; dog.breed = "Collie"; a. pet = dog; cout << dog.name; b. pet = dog; cout << dog.breed; c. pet = dog;...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

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