Question

java. Question 2: Practice Polymorphism- Food A Assume you have a parent class called Food with...

java.

Question 2: Practice Polymorphism- Food A

Assume you have a parent class called Food with one instance data variable: private String name. The class has one constructor that takes the name as a parameter, getters and setters for name, and a toString that outputs the name.

  • Write a child class called Vegetable with one additional variable describing whether or not the vegetable is green.
  • Write two constructors: one that takes in all information about a vegetable and one that uses a default value to indicate that a vegetable IS green.
  • Write getters and setters, a toString, and an equals method.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Vegetable extends Food {

    private boolean isGreen;

    public Vegetable(String name) {
        this(name, true);
    }

    public Vegetable(String name, boolean isGreen) {
        super(name);
        this.isGreen = isGreen;
    }

    public boolean isGreen() {
        return isGreen;
    }

    public void setGreen(boolean green) {
        isGreen = green;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Vegetable vegetable = (Vegetable) o;
        return getName().equals(vegetable.getName()) && isGreen == vegetable.isGreen;
    }

    @Override
    public String toString() {
        return "Vegetable{" + "name='" + getName() + "'" + "isGreen=" + isGreen + '}';
    }
}
Add a comment
Know the answer?
Add Answer to:
java. Question 2: Practice Polymorphism- Food A Assume you have a parent class called Food with...
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
  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Java program Candidate Class: This class records the information for each candidate that is running for...

    Java program Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are running for the party they represent total votes a boolean value to record if they won Methods: Custom constructor that accepts first and last name, office and party Custom constructor that accepts an instance of another customer Getters for all instance variables and setters for total votes and the boolean variable toString: This method...

  • A class called Author is designed as shown in the class diagram. (JAVA)

    in javaA class called Author is designed as shown in the class diagram. Author name: String email: String gender: char +Author (name :String, email: String, gender char) +getName ():String +getEmail() String +setEmail (email: String) :void +getGender(): char +toString ():String It contains  Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f),  One constructor to initialize the name, email and gender with the given values; public Author (String name, String email, char gender) f......)  public getters/setters: getName(), getEmail setEmail and getGender(); (There are no setters for name and gender,...

  • Create a Java application, that support the following: Create an Employee class, which holds following information:...

    Create a Java application, that support the following: Create an Employee class, which holds following information: Employee First Name Employee Last Name Employee Phone Number Employee Address Employee id Employee Title Employee salary Create an application that uses the Employee class Constructors –Getters and setters A minimum of 3 constructors including default constructor equals() method Helper methods toString() method Create an array of 5 Employee objects Use a Scanner to read in 5 employee information Change 2 employees information (3-items...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Write code to create a Java class called "Student". The class should hold information about a...

    Write code to create a Java class called "Student". The class should hold information about a student: name, id and whether or not the student is currently registered. There should be a constructor that takes name, id and registration status. Add getters and setters for the three properties. Make sure that you use appropriate visibility modifiers (public vs. private).

  • Java: Create a class called Vehicle with the following features: a. It has three private data...

    Java: Create a class called Vehicle with the following features: a. It has three private data members (instance variables): one is the manufacturer’s name (String manufacturer), the second is the number of cylinders in the engine (int cylinder), and the third is the owner (Person owner). The class Person is described above. b. It has three constructors, a no-argument constructor, a constructor with three parameters, and a copy constructor. The no-argument constructor will set the manufacturer to “None”, cylinder to...

  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • JAVA 1336 Problem: Complete Programming Project 1 (pg. 339, Savitch). The defined class will be HotDogStand.java...

    JAVA 1336 Problem: Complete Programming Project 1 (pg. 339, Savitch). The defined class will be HotDogStand.java and will also contain: • A default constructor • An overloaded constructors Getters and Setters A 'copy' constructor, -3 A 'toString' method, An 'equals' method, A finalize' method, Remember to THOROUGHLY test the application in a separate test file, HotDogStandTest.java. Requirements: You will submit the following with this lab (face-to-face sections). Online sections will turn their labs in through Blackboard as usual: a. The...

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