Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Write a class `Vegetable` and a class `LeafyVegetable` extending from `Vegetable` (i.e.

,
`Vegetable` is a superclass and `LeafyVegetable` is a subclass) with the following
specifications:

Vegetable
- name: String
- pricePerUnit: double
+ Vegetable(String name, double pricePerUnit) – constructor

+ getName(): String - returns the name in lowercase format.


+ getPricePerUnit(): double - returns the price per unit.
+ setName(String name): void - updates the name.
+ setPricePerUnit(double pricePerUnit): void - updates the price per unit.
+ toString(): String - returns the string of format: name, pricePerUnit (the name in
lowercase format and the pricePerUnit is formatted by two decimal places).

LeafyVegetable
- daysToHarvest: int
+ LeafyVegetable(int daysToHarvest, String name, double pricePerUnit) – constructor

+ toString(): String - overrides to include daysToHarvest in the output string. Example:


thanh long, 10.00, 40
+ reducePrice(double reduction): void - decreases the price by a fixed amount.
+ getDaysToHarvest(): int - returns the days to harvest.
+ isSeasonal(): boolean - assumes that if daysToHarvest is less than 60, it's considered
seasonal; otherwise, it's not.

Do not format the result.


The program output might look something like this:

Enter name: Thanh Long Enter name: Thanh Long


Enter price per unit: 10 Enter price per unit: 10
Enter days to harvest: 40 Enter days to harvest: 40

1. Test getName() 1. Test getName()


2. Test reducePrice(reduction:double) 2. Test reducePrice(reduction:double)
3. Test toString() for Vegetable 3. Test toString() for Vegetable
4. Test toString() for LeafyVegetable 4. Test toString() for LeafyVegetable
5. Test isSeasonal() 5. Test isSeasonal()

Enter TC[1|2|3|4|5]: 1 Enter TC[1|2|3|4|5]: 2


OUTPUT: Enter amount to reduce price: 3
thanh long OUTPUT:
7.00
Enter name: Thanh Long Enter name: Thanh Long
Enter price per unit: 10 Enter price per unit: 10
Enter days to harvest: 40 Enter days to harvest: 40

1. Test getName() 1. Test getName()


2. Test reducePrice(reduction:double) 2. Test reducePrice(reduction:double)
3. Test toString() for Vegetable 3. Test toString() for Vegetable
4. Test toString() for LeafyVegetable 4. Test toString() for LeafyVegetable
5. Test isSeasonal() 5. Test isSeasonal()

Enter TC[1|2|3|4|5]: 3 Enter TC[1|2|3|4|5]: 4


OUTPUT: OUTPUT:
thanh long, 10.00 thanh long, 10.00, 40
Enter name: Thanh Long Enter name: Thanh Long
Enter price per unit: 10 Enter price per unit: 10
Enter days to harvest: 40 Enter days to harvest: 70

1. Test getName() 1. Test getName()


2. Test reducePrice(reduction:double) 2. Test reducePrice(reduction:double)
3. Test toString() for Vegetable 3. Test toString() for Vegetable
4. Test toString() for LeafyVegetable 4. Test toString() for LeafyVegetable
5. Test isSeasonal() 5. Test isSeasonal()

Enter TC[1|2|3|4|5]: 5 Enter TC[1|2|3|4|5]: 5


OUTPUT: OUTPUT:
true false

You might also like