1. More OOP A) Write a subclass of Vegetable called \"Carrot\". Include a privat
ID: 3763416 • Letter: 1
Question
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.
Explanation / Answer
//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 +'}';
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.