Programming Project 6.18 asked you to use an array of Strings to store the fruits and vegetables shipped in a BoxOfProduce object for a CSA farm.
Modify your solution further by creating a Produce class. This class should have an instance variable of type String for the name, appropriate constructors, and a public toString() method. Then create a Fruit and a Vegetable class that are derived from Produce. These classes should have constructors that take the name as a String and invoke the appropriate constructor from the base class to set the name.
Next, modify the text file of produce so it indicates whether each item is a fruit or a vegetable. Here is one possible organization, although you can use others:
Broccoli,Vegetable
Tomato,Fruit
Kiwi,Fruit
Kale,Vegetable
Tomatillo,Fruit
Finally, modify the BoxOfProduce class so it creates an array of type Produce instead of type String . The class should read the produce from the text file and create instances of either Fruit or Vegetable , with the appropriate name, in the array. After a box is finished, loop through the contents of the array and output how many fruit and how many vegetables are in the box. The rest of the program should behave the same as the solution to Programming Project 6.18.
Sorry the answer is not available at the moment…
If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.
Spread the 'tradition of sharing'.