Question

1. More OOP A) Write a subclass of Vegetable called "Carrot". Include a private String variable...

1. More OOP

A) Write a subclass of Vegetable called "Carrot". Include a private String variable called "variety". Include a getter and setter for the variety. Include a toString method that returns a string representation of the calories, price, and variety of the carrot.

B)Write a class called Vegetable. Include private double variables called "price". and "calories". Include getter and setter methods for these. Also include a toString method that properly returns a string version of the price and calories.

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

//Answer for part B

Class Vegitable{
private double price;
private double calories;

// Getter for Price
public void getPrice() {
     return price;
}
// Setter for Price
public void setPrice(double price) {
     this.price = price;
}

// Getter for Calories
public void getCalories() {
     return calories;
}
// Setter for Calories
public void setCalories(double calories) {
     this.calories = calories;
}
@Override
public String toString() {
     return "Vegitable{" + "price=" + price + ",calories=" + calories + '}';
}
}

//Answer for part A

Class Carrot extends Vegitable{
    private String variety;
  
    // Getter for Variety
    public void getVariety() {
       return variety;
    }
    // Setter for Variety
    public void setVariety(double variety) {
       this.variety = variety;
    }

    @Override
    public String toString() {
       return "Carrot{" + "price=" + price + ",calories=" + calories + ",variety=" + variety +'}';
    }
}

Add a comment
Know the answer?
Add Answer to:
1. More OOP A) Write a subclass of Vegetable called "Carrot". Include a private String variable...
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
  • 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...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • Write an entire class called Movie that stores an instance variable named title for the title...

    Write an entire class called Movie that stores an instance variable named title for the title of the movie as a string. Write the corresponding getter and setter methods (getTitle, setTitle), and a constructor that takes one parameter (string for the title). Include both the class definition and method definitions! Be sure to use appropriate visibility modifiers. Assume all includes and using namespace std are already in the file. Do not try to make header files.

  • Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

    Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method...

  • ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still...

    ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still an Account class, however it has an additional field and some additional functionality. But it will still retain all the behavior of the Account class. Write a class called GoldAccount. This class will have an additional private field called bonusPoints. This field will require no get or set methods. But it will need to be initialized to 100 in the constructor. Thereafter, after a...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class....

    Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class. The first constructor will take zero input arguments and set the stuff attibute to null. The second will take a String as input and set the shul atbute to input string Getter and Setter Write a public getter method called getsauff that returns the string stored by the sthuff attribute If stulf is null, it should return the string "no stuff in here Write...

  • Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string...

    Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string SSN; The Employee class has ▪ a no-arg constructor (a no-arg contructor is a contructor with an empty parameter list): In this no-arg constructor, use “unknown” to initialize all private attributes. ▪ a constructor with parameters for each of the attributes ▪ getter and setter methods for each of the private attributes ▪ a method getFullName() that returns the last name, followed by a...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

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