The goal of this project is to write a suit of classes for simulating
the growth, maintenance, and harvesting of Mango trees.
The project is divided into two parts. The first part is to write
the code for class Mango, which is designed for a Mango tree that
grows and whose fruits appreciate overtime.
The second part is to write the code for class MangoSim, which conducts
a simulation of growing various mango trees for a number of years, and
produces information as to which mango tree will be the most profitable.
Class Mango
We envision that each Mango tree has the following field variables:
what kind it is, as a String,
how tall the tree is, as an int (in inches),
how much a fruit sells for, as an int (in cents).
There should be a constructor that takes values for the kind the
height, and the price as parameters and sets these to instance
variables.
There should be methods for accessing the kind, the height, and
the price: getKind(), getHeight(), and getPrice().
Each year the price appreciates by 1 percent.
Each year a tree grows in percentage ranging uniformly between
5 percent and 15 percent. After growing if the tree becomes too
tall, since it will go beyond the reach of pickers, the tree is
trimmed down to precisely 150 inches. The reach of pickers is
260 inches.
After the price and the height updates, fruits can be picked from
the tree and sold. The amount harvested from a tree is two times
the inches in height of the tree times the price.
There should be a method by the name of process() for executing the
two updates and returns as boolean whether the tree has been
trimmed, which should consist of two parts:
appreciate() for updating the price and
grow() for growing and, if necessary, trimming the tree.
The latter method should return whether the tree has been trimmed.
There should also be a method by the name of getHarvest() for
obtaining the number of fruits harvested from the tree according
to its height.
The above idea has been defined in an interface MangoInterface.java.
Your code should implement this interface.
Class MangoTest
This is not part of the entire scheme, but can be used to test
whether the class Mango correctly implements MangoInterface.
For the second part, class MangoSim is an object class whose constructor
takes no parameter.
Using one of its field variables,
an object of this class maintains an array of mangos.
To this array of mangos, the growth simulation will be run.
The class has the following instance methods:
public void add( Mango m ):
this method adds a Mango object m
at the end of the array.
You need to write this method.
You can refer to the lecture on arrays no.3 to figure out
how to perform insertion.
The method required here is simpler, since the new element
is added at the end.
public void delete( int index ):
this method removes the element at position
index from theTrees.
You need to write this method.
You can refer to the lecture on arrays no.3 to figure out
how to perform removal.
public void readData( String fileName ):
this method adds data from the File specified by
fileName.
You need to write this method.
In a mango data file, the first line is the number of mango trees
to be simulated and the ensuing lines are the kind, the height, and
the yield of each mango. You can use this file to test the code:
mangoData.txt .
To execute this action, do the following:
Create a scanner from the file specified.
Read the number of elements from the file first.
Call this quantity d.
As in the case of insertion,
create a new array whose number of elements is the number of elements
in theTrees plus d.
Store all the elements of theTrees to
the new array exactly at their current position.
Read the information of the d mangos from
the file and store them after the elements from
theTrees (if you start use index values starting
from 0 for the new mangos, the “offset” is the number of
elements in theTrees).
public void saveData( String fileName ):
this method writes the information about the Mango objects in the
array to the File specified by fileName.
The code for this method is already given.
public void printData():
this method prints the list of mangos on the screen.
The code for this method is already given.
public void simulate( int n ):
this method performs simulation for the number of years specified by
n.
You need to write this method.
Store this number n to the field variable
nYears
for future references.
To conduct the simulation,
use a two dimensional int array by the name of theTotalHarvests.
The first dimension (the rows) of this array represents the mangos,
while the second (the columns) the years. The second dimension has
length 1 + the number of years specified by the user, and each
column represents the cumulative profits of each tree.
This table is created each time a simulation is run, because the
numner of mangos and/or the number of years may be different from
the last time the simulation is done.
Naturally, year 0 (column 0, in other words) of the table has harvest
value 0 for all trees.
In each ensuing year, using the grow() method of the class Mango,
each tree is grown and harvested, the harvest from that year is
added to the total harvest from the previous year and becomes the
value for that year's total harvest.
Then after the simulation, examine the final year's cumulative harvest
values to find the highest cumulative harvest and retain the index to
the tree with the highest cumulative harvest.
Use the field variable maxIndex
and the field variable maxValue
to record respectively the index to the mango that produces the
largest value and the corresponding value.
After the simulation has been completed, print the result on the screen.
To print the print a currency amount, you can rely on
the static void method by the name of printValue
that takes an integer width
and an integer value
and prints on screen the value using width many
spaces with a period inserted before the second to last digit,
a dollar sign inserted at the beginning, and the number above the
decimal point punctuated with a comma.
The template MangoSim.java has all the field vaariables declared
Since Mango.java uses random number generation the results will be
different each time a simulation is run, but here is one example
for executing simulate( 10 ) on a mango array
as given in mangoData.txt and 11 for the width
given to printValue to print currency quantities
appearing in the table.
This is the main class for running the simulation.
The code is already written for this class.
The class creates a MangoSim object and
intersts with the user to take an appropriate action for each
user's input.