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) ...

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

(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
Sherwood
1 : 4.40 (59% pa)
2 : 4.62 (69% pa)
3 : 4.99 (76% pa)
4 : 1.18 (67% pa)
5 : 2.59 (72% pa)
6 : 2.61 (91% pa)
7 : 1.14 (89% pa)
8 : 1.42 (77% pa)
9 : 1.91 (90% pa)
10 : 3.58 (53% 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 : d
Sherwood
1 : 44.68 (59% pa)
2 : 63.66 (69% pa)
3 : 84.35 (76% pa)
4 : 15.37 (67% pa)
5 : 38.93 (72% pa)
6 : 66.24 (91% pa)
7 : 27.40 (89% pa)
8 : 24.68 (77% pa)
9 : 47.20 (90% pa)
10 : 29.99 (53% pa)

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it : r
What height to reap at : twenty seven
ERROR: Invalid height

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it : r
What height to reap at : 60
Cut 2 : 63.66 (69% pa)
New 2 : 3.51 (76% pa)
Cut 3 : 84.35 (76% pa)
New 3 : 3.49 (54% pa)
Cut 6 : 66.24 (91% pa)
New 6 : 3.11 (65% pa)

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it : d
Sherwood
1 : 44.68 (59% pa)
2 : 3.51 (76% pa)
3 : 3.49 (54% pa)
4 : 15.37 (67% pa)
5 : 38.93 (72% pa)
6 : 3.11 (65% pa)
7 : 27.40 (89% pa)
8 : 24.68 (77% pa)
9 : 47.20 (90% pa)
10 : 29.99 (53% 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
Chippewa
1 : 2.90 (84% pa)
2 : 3.13 (93% pa)
3 : 4.39 (60% pa)
4 : 2.92 (53% pa)
5 : 3.90 (68% pa)
6 : 3.19 (91% pa)
7 : 3.19 (71% pa)
8 : 1.47 (90% pa)
9 : 3.92 (56% pa)
10 : 4.54 (73% pa)

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it : l
What is the forest name : Sherwood

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it : d
Sherwood
1 : 44.68 (59% pa)
2 : 3.51 (76% pa)
3 : 3.49 (54% pa)
4 : 15.37 (67% pa)
5 : 38.93 (72% pa)
6 : 3.11 (65% pa)
7 : 27.40 (89% pa)
8 : 24.68 (77% pa)
9 : 47.20 (90% pa)
10 : 29.99 (53% pa)

(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it : q
ERROR: Invalid option

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

I know the code needs to start out with this:

import java.util.Scanner;
//=============================================================================
public class ForestryManagement {
//-----------------------------------------------------------------------------
    private static Scanner keyboard = new Scanner(System.in);
//-----------------------------------------------------------------------------
    public static void main(String[] args) {

but other than that I am unsure of what to do as I just started learning java. Any and all help is appreciated.

Recommended Answers

All 12 Replies

Has your course covered the idera of defining a class? The obvious approach is to create a Tree class with the tree's height and growth rate and the related methods. A Forest is then just a collection (could be an array) of Tree objects. Reaping is just a pass through the collection of trees deleting Tree objects over the given height and replacing them with new Tree objects.

commented: I just commented what I have so far, if this is wrong please help guide me in the right direction. +0

We've touched on it, but I don't really understand it... I'm not really sure what would start the code or be in the main method. This is what I have so far:

import java.util.Scanner;
//=============================================================================
public class ForestryManagement {
//-----------------------------------------------------------------------------
    final static double MIN_INITIAL_HEIGHT;
    final static double MAX_INITIAL_HEIGHT;
    final static double MIN_GROWTH_RATE;
    final static double MAX_GROWTH_RATE;
    private static Scanner keyboard = new Scanner(System.in);
//-----------------------------------------------------------------------------
    public static void main(String[] args) {

        char command;

        do {
            System.out.println("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
            command = keyboard.next().charAt(0);

            switch (command) {
            case 'D':
            case 'd':
                ******HELP*****
                break;

            case 'N':
            case 'n':
                ******HELP*****
                break;

            case 'Y':
            case 'y':
                *****HELP*****
                break;

            case 'R':
            case 'r':
                *****HELP*****
                break;

            case 'S':
            case 's':
                *****HELP*****
                break;

            case 'L':
            case 'l':
                *****HELP*****
                break;

            case 'X':
            case 'x':
                System.out.println("Goodbye");
                break;

            default:
                System.out.print("ERROR: Invalid option");
            }
        }
        while (command != 'x');
    }

OK, that's a good start.

Next step is decide how you will represent your forest of trees inside your program. There are 10 trees, and each tree has a height and a growth rate. A Tree class is the obvious Java solution, but if you have not got that far in your lessons then you can use two arrays - one for the 10 heights and one for the 10 growth rates.

Whichever you decide you can declare the necessary class or arrays, and move straight on to the code that displays the data, then the code that creates a new forest by populating it with random values. Don't worry about the other options until you have these two working!

commented: how do you code/create a new class? +0

If I do the array options, it would be an array with 10 rows and 3 columns correct? and how would I generate the random numbers between the min and max height and growth rate? Additionally how do I attach the name of the forest to these trees when the forest is "saved"? If it would be easier to creat a new class, can you walk me through that process please?

It sounds like you have not covered creating classes in your course yet, so if that's the case it would be a bad idea for me to try to overlap with your real teacher. I would stick to arrays if that's what you know. If there are just two variables for each tree then a 10x2 array is big enough.
The java.lang.Math class has a random method that gives you random numbers between 0 and 1 (see the API doc for details). You just need a bit of simple arithmetic to convert that into a specified range.
I wouldn't worry about saving yet. Solve one problem at a time. Beginners try to write the whole program, then see if it works. When it doesn't work they don't know where to look first. Real programmers break their problem into small steps then code AND TEST each step before moving on. Just google "test early, test often".

We went over creating classes, but I didn't understand it. But in displaying the array, how do I get it to print the location in the array as well as the numbers stored there? I know I will need to add 1 to the location as it starts at 0.

Well, yes. You just add 1 to the location when you print it, as in
System.out.println ( (i+1) + ": " etc

ps: creating classes is really important in Java. You should go back over your notes and materials until you "get" it. It's essential to your further progress.
You may find Oracle's tutorials useful - not the easiest but definitely the best, eg
https://docs.oracle.com/javase/tutorial/java/concepts/index.html

This is what I have so far, but I don't know where to put the forest array and generate the random numbers

import java.util.Scanner;

//=============================================================================

public class ForestryManagement {

//-----------------------------------------------------------------------------

    final static double MIN_INITIAL_HEIGHT;

    final static double MAX_INITIAL_HEIGHT;

    final static double MIN_GROWTH_RATE;

    final static double MAX_GROWTH_RATE;

    private static Scanner keyboard = new Scanner(System.in);

//-----------------------------------------------------------------------------

    public static void main(String[] args) {

        char command;

        int [][] forest = new int [10][2];
        string nameOfForest;

        do {

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

            command = keyboard.next().charAt(0);

            switch (command) {

            case 'D':

            case 'd':

                displayForest(nameOfForest);

                break;

            case 'N':

            case 'n':

                addNewForest();

                break;

            case 'Y':

            case 'y':

                simulateYear();

                break;

            case 'R':

            case 'r':

                reapForest();

                break;

            case 'S':

            case 's':

                saveForest();

                break;

            case 'L':

            case 'l':

                loadSavedForest();

                break;

            case 'X':

            case 'x':

                System.out.println("Goodbye");

                break;

            default:

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

            }

        }

        while (command != 'x');

    }

//-----------------------------------------------------------------------------

    private static void addNewForest() {

        string name;

        System.out.println("What is the forest name : ");
        name = keyboard.nextString();

        return (name);
    }
//-----------------------------------------------------------------------------
    private static void displayForest(string nameOfForest)

        string forestName;

        System.out.println(forestName);

You were right to put add/display etc into separate methods. Now your forestName and forest variables need to be declared where they are visible to all those methods - ie inside the class but outside any method.
The obvious time to populate the array with the random numbers is when you create a new forest, yes?

Be sure to make others aware if you are crossposting. Is this you at http://www.dreamincode.net/forums/topic/403609-need-help-putting-together-forest-program/

Also, is it this assignment? This is the cache of the assignment I found.

Did you forget to mention the following requirement?
"You must ...

Do an object oriented analysis and design "

If you only started coding a few days ago in Java, this may be a little deep. Usually you start with smaller apps and something like this is done after you get the basics out of the way. Is this a code boot camp? How did you get here?

Do an object oriented analysis and design

Is that right Heather? If so you just wasted a whole load of my time (and yours) by hiding that fundamental requirement.

commented: Objects of desire create ire when hidden? +12

Ha ha ..Why I have these kind of questions in my mail box?? I think that main reason to make a course o programming is....to learn programming. Anyway, If you still want to know how to do it, you have all the clues in your other post:

  1. "I have some code figured out such as tree information:", you just need to encapsulate the code in a class -> public class Tree[.....]
  2. If you don't have an implementation to Canadian_Foreset_service class, just create it with an static method to get the random numbers- > lok at this http://stackoverflow.com/questions/7961788/math-random-explained
  3. Create a Forest class with properties to define the name of forest, and an array to contain objects created from your Tree class. Keep watch these links: http://stackoverflow.com/questions/5889034/how-to-initialize-an-array-of-objects-in-java
    http://tutorials.jenkov.com/java-generics/generic-list.html
    4.Finally, If you want to keep these objects in a file, you have to implement Serializable interfaz in order to keep them in I/O file operations. Look at this: http://www.javapractices.com/topic/TopicAction.do?Id=45

By the way, I've heard that real forestry management programs use Voronoi's models to perform simulations. If you are really interested in develop biological models, it would be better to learn ....R.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.