Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Canadian Forest Service wants to do a simple simulation of the growth and pr

ID: 3710006 • Letter: T

Question

The Canadian Forest Service wants to do a simple simulation of the growth and pruning of forests. Each forest has a name and exactly 10 trees. The trees are planted when they are 1' to 5' tall, and each tree has a individual growth rate of 50%-100% per year. For the simulation new trees are constructed randomly within these bounds. A forest is reaped (by lumberjacks) on demand - all trees above a specifed height are cut down and replaced with new trees.

The user interface to the simulation must allow the user to:

Display the current forest (with tree heights to 2 decimal places)

Discard the current forest and create a new forest

Simulate a year's growth in the current forest

Reap the current forest of trees over a user specified height, replacing the reaped trees with random new trees.

Save the information about the current forest to file (named after the forest)

Discard the current forest and load the information about a forest from a file.

The user interface must deal with invalid user input in a graceful way.

Here what a sample run should look like (with the keyboard input shown in italics) ...

You must:

Do an object oriented analysis and design.

Implement the program in Java. Restriction: Only the class containing the main method may have a Scanner object (the keyboard variable).

Explanation / Answer

package January;

import java.io.*;

import java.util.*;

// Class ForestGroth definition

public class ForestGroth

{

// Scanner class object created to read data from console

Scanner sc = new Scanner(System.in);

// Random class object created

Random ran = new Random();

// To store forest name

String forestName;

// To store height of the tree

double treeHeight[] = new double[10];

// To store growth percentage

int growthPercent[] = new int[10];

// Minimum height of the tree

double minH = 1;

// Maximum height of the tree

double maxH = 5;

// Minimum percentage growth

int minP = 50;

// Maximum percentage growth

int maxP = 100;

// Method to display menu and return user choice

char displayMenu()

{

// To store user choice

char choice;

// Displays menu

System.out.println(" (D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it : ");

// Accepts user choice and stores the first character

choice = sc.next().charAt(0);

// Returns the choice

return choice;

}// End of method

// Method to generate tree height and percentage growth

void generateTree()

{

// Accepts the name of the forest

System.out.print(" What is the forest name : ");

forestName = sc.next();

// Loops 10 times for 10 trees

for(int x = 0; x < 10; x++)

{

// Generates random height

treeHeight[x] = minH + (maxH - minH) * ran.nextDouble();

// Generates random percentage growth

growthPercent[x] = ran.nextInt((maxP - minP) + 1) + minP;

}// End of for loop

}// End of method

// Method to display forest information

void displayTree()

{

// Displays name

System.out.println("Forest Name: " + forestName);

// Loops 10 times for 10 trees

for(int x = 0; x < 10; x++)

// Displays tree height and growth percentage

System.out.printf(" %d: %.2f (%d %%pa)", (x + 1), treeHeight[x], growthPercent[x]);

}// End of method

// Method to grow percentage of height of each tree per year

void growthYear()

{

// Loops 10 times for 10 trees

for(int x = 0; x < 10; x++)

// Calculates the growth percentage

treeHeight[x] = treeHeight[x] + treeHeight[x] * (growthPercent[x] / 100.0);

}// End of method

// Method to reap the tree based on the value entered by the user

void reapTree()

{

// Accept the height to reap

System.out.println(" What height to reap at: ");

double reap = sc.nextDouble();

// Loops 10 times for 10 trees

for(int x = 0; x < 10; x++)

{

// Checks if the current height of the tree is greater than or equals to the reap value entered by the user

if(treeHeight[x] >= reap)

{

// Displays cut tree height and growth percentage

System.out.printf(" Cut %d: %.2f (%d %%pa)", (x + 1), treeHeight[x], growthPercent[x]);

// Generates random height

treeHeight[x] = minH + (maxH - minH) * ran.nextDouble();

// Generates random percentage growth

growthPercent[x] = ran.nextInt((maxP - minP) + 1) + minP;

// Displays tree height and growth percentage

System.out.printf(" New %d: %.2f (%d %%pa)", (x + 1), treeHeight[x], growthPercent[x]);

}// End of if condition

}// End of for loop

}// End of method

// Method to save the forest information into a file

void saveFile()

{

// try block begins

try

{

// Opens the file for writing

File file = new File(forestName + ".txt");

// Generates PrintWriter object for the file

PrintWriter pw = new PrintWriter(file);

// Writes the forest name

pw.println(forestName);

// Loops 10 times for 10 trees

for(int x = 0; x < 10; x++)

{

// Writes the tree height and growth percentage for each tree

pw.printf("%f %d", treeHeight[x], growthPercent[x]);

// Checks if not last record write new line character

if(x != 9)

// Write new line character

pw.println();

}// End of for loop

// Close the file

pw.close();

}// End of try block

// Catch block to handle FileNotFoundException

catch(FileNotFoundException fe)

{

System.out.println("Cannot create file.");

}// End of catch block

}// End of method

// Method to load a file

void loadFFile()

{

// try block begins

try

{

// To store file name

String fileName;

int x = 0;

// Accepts the file name for reading

System.out.print(" What is the forest name : ");

fileName = sc.next();

// Scanner object created to read file contents

Scanner sc = new Scanner(new File(fileName));

// Reads the forest name and stores in instance variable

forestName = sc.next();

// Loops till data found

while(sc.hasNext())

{

// Extracts height of the tree and stores and stores in instance variable

treeHeight[x] = sc.nextDouble();

// Extracts growth percentage of the tree and stores and stores in instance variable

growthPercent[x] = sc.nextInt();

}// End of while loop

}// End of try block

// Catch block to handle FileNotFoundException

catch(FileNotFoundException fe)

{

System.out.println("Cannot open file for reading.");

}// End of catch block

}// End of method

// main method definition

public static void main(String[] args)

{

// Declares ForestGroth class object

ForestGroth forestGrowth = new ForestGroth();

// Loops till user option is not 'e' or 'E'

do

{

// Calls the method to accept user choice

// switch will check the return user choice of the method displayMenu()

// And will call the other methods based on the user choice

switch(forestGrowth.displayMenu())

{

case 'd':

case 'D':

forestGrowth.displayTree();

break;

case 'n':

case 'N':

forestGrowth.generateTree();

break;

case 'y':

case 'Y':

forestGrowth.growthYear();

break;

case 'r':

case 'R':

forestGrowth.reapTree();

break;

case 's':

case 'S':

forestGrowth.saveFile();

break;

case 'l':

case 'L':

forestGrowth.loadFFile();

break;

case 'e':

case 'E':

System.out.println(" Goodbye");

System.exit(0);

default:

System.out.println(" ERROR: Invalid option.");

}// End of switch case

}while(true); // End of do - while loop

}// End of main method

}// End of class

Sample Output:


(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
n

What is the forest name : Sherwood

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
d
Forest Name: Sherwood

1: 2.11 (85 %pa)
2: 3.16 (75 %pa)
3: 3.02 (70 %pa)
4: 3.47 (78 %pa)
5: 4.27 (100 %pa)
6: 3.17 (82 %pa)
7: 2.95 (83 %pa)
8: 2.36 (84 %pa)
9: 3.29 (97 %pa)
10: 2.53 (72 %pa)
(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
y

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
d
Forest Name: Sherwood

1: 289.13 (85 %pa)
2: 277.55 (75 %pa)
3: 210.61 (70 %pa)
4: 349.23 (78 %pa)
5: 1092.66 (100 %pa)
6: 382.00 (82 %pa)
7: 370.64 (83 %pa)
8: 310.49 (84 %pa)
9: 745.51 (97 %pa)
10: 193.86 (72 %pa)
(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
R

What height to reap at:
350

Cut 5: 1092.66 (100 %pa)
New 5: 2.69 (60 %pa)
Cut 6: 382.00 (82 %pa)
New 6: 1.74 (65 %pa)
Cut 7: 370.64 (83 %pa)
New 7: 1.14 (63 %pa)
Cut 9: 745.51 (97 %pa)
New 9: 1.03 (68 %pa)
(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
s

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
n

What is the forest name : Chippewa

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
d
Forest Name: Chippewa

1: 2.15 (97 %pa)
2: 2.89 (56 %pa)
3: 3.92 (97 %pa)
4: 4.14 (99 %pa)
5: 1.15 (81 %pa)
6: 1.78 (56 %pa)
7: 2.74 (70 %pa)
8: 2.82 (86 %pa)
9: 4.75 (57 %pa)
10: 1.96 (60 %pa)
(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
l

What is the forest name : Sherwood
Cannot create file.

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
l

What is the forest name : Sherwood.txt

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
d
Forest Name: Sherwood

1: 193.86 (72 %pa)
2: 2.89 (56 %pa)
3: 3.92 (97 %pa)
4: 4.14 (99 %pa)
5: 1.15 (81 %pa)
6: 1.78 (56 %pa)
7: 2.74 (70 %pa)
8: 2.82 (86 %pa)
9: 4.75 (57 %pa)
10: 1.96 (60 %pa)
(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
t

ERROR: Invalid option.

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :
E

Goodbye

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote