Project No.2: Growing a Mango
This project consists of 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.
For the first part, we envision that each Mango tree has information about
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).
Each year the tree receives some updates. First, the price appreciates by 1 percent.
Second, the tree grows. The growth in percentage ranges uniformly between 5 percent and 15 percent. However, if the tree grows too tall, it will go beyond the reach for pickers, and so it is trimmed down. We assume that whenever the tree grows taller than 260 inches it is trimmed down to 150 inches.
After the two updates, mangoes are picked and sold. We assume that two fruits are produced per inch of the height of the tree and each fruit is sold at the current price.
The task of this lab is to write a code for the class Mango with the following functionality:
- The class has three instance variables, one for the kind, one for the height, and one for the price.
- A constructor that takes values for the kind, the height, and the price as parameters and sets these to instance variables.
- Three methods for accessing the kind, the height, and the price individually, named getKind(), getHeight(), and getPrice().
- A method named 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.
- A method named getHarvest() for returning the harvest obtained from the tree, which is the number of fruits times the price.
The above idea has been defined in an interface MangoInterface.java . Your code should implement this interface.
Use the following program MangoTest.java for testing your program.
A sample execution produces the following output:
% java MangoTest
Trimmed. kind=Alfonso, height=150, price=404, harvest=121200
Not trimmed. kind=Alfonso, height=171, price=408, harvest=139536
Not trimmed. kind=Alfonso, height=196, price=412, harvest=161504
Not trimmed. kind=Alfonso, height=217, price=416, harvest=180544
Not trimmed. kind=Alfonso, height=241, price=420, harvest=202440
Trimmed. kind=Alfonso, height=150, price=424, harvest=127200
Not trimmed. kind=Alfonso, height=158, price=428, harvest=135248
Not trimmed. kind=Alfonso, height=180, price=432, harvest=155520
Not trimmed. kind=Alfonso, height=195, price=436, harvest=170040
Not trimmed. kind=Alfonso, height=209, price=440, harvest=183920
Not trimmed. kind=Alfonso, height=235, price=444, harvest=208680
Not trimmed. kind=Alfonso, height=248, price=448, harvest=222208
Trimmed. kind=Alfonso, height=150, price=452, harvest=135600
Not trimmed. kind=Alfonso, height=170, price=456, harvest=155040
Not trimmed. kind=Alfonso, height=178, price=460, harvest=163760
Not trimmed. kind=Alfonso, height=196, price=464, harvest=181888
Not trimmed. kind=Alfonso, height=219, price=468, harvest=204984
Not trimmed. kind=Alfonso, height=232, price=472, harvest=219008
Not trimmed. kind=Alfonso, height=248, price=476, harvest=236096
Trimmed. kind=Alfonso, height=150, price=480, harvest=144000
For the second part, MangoSim.java shall take two parameters at the time of running, args being the name of a file that contains the data of mango trees to be simulated and args being the number of years to be simulated. In the 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 conduct the simulation, first an array of mango trees are defined based on the information presented in the file and then data are read. Next, the simulations are conducted and the cumulative harvests of the individual trees are stored in a two dimensional int array by the name of theTotalHarvests. The first dimension (the rows) of this array represents the mangoes, 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. Naturally, year 0 (column 0, in other words) has harvest 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, the final year's cumulative harvests are examined to find the highest cumulative harvest and the index to the tree with the highest cumulative harvest.
After the simulation has been completed, the results will be printed on screen. The template MangoSim.java has some variables declared and the part for presenting the results already written. What you have to write is the sections numbered 1, 2, 3 indicated in the template. Since Mango.java uses random number generation the results will be different each time a simulation is run, but here is one example mangoSimResult.txt, which is the result of running java MangoSim mangoData.txt
In addition to developing the code, you must submit a paper describing the idea you used in writing the code. You may use a diagram if necessary. The grading will be 1/3 for the paper, 1/3 for the first part of the coding, and 1/3 for the second part of the coding. The code is due at the conclusion of your lab in the second week.
THE CODE FOR THIS PROJECT HAS ALREADY BEEN WRITTEN!
Attached is the answers to this project. They will need to be changed slightly because this was the coding of my friend(make sure it can compile once changed). After this is done, I will need a summary explaining the algorithms used. An example attached file called "brainstorming algorithm" is located here. Same length as the example.
Please find the answer. Kindly accept the... View the full answer