Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2017


In this assignment, you will gain experience reading from a file, using arrays, and using Math methods. Most important, though, you will use the testing techniques we talked about in class to make sure your program is bullet-proof.

In addition to actually writing code to solve the problem, you must write test cases that match the test case categories we discussed in class on Monday. You should document these test cases in your code and actually run these tests to make sure the code passes them.

It is highly recommended that you break your code into methods and perform only one task per method, both to make the code easier to write, and to make it easier to test.
Logging Lease Optimizer
You work for a logging company which needs to decide what portion of a forest to lease in order to maximize its take of the juicy, juicy ponderosa pine trees. A detailed survey of the forest has been conducted giving details of each pine tree in the forest. The location of each tree is given by its distance (in feet) to the east and to the north of the southwest corner of the forest. Each tree also had its height recorded and its DBH (diameter at breast height). The survey also recorded the location of any Grizzly bear dens in the forest.

The forest service requires your lease be a square with sides that are an integer number of feet. The sides of the leased square must be as big as possible without exceeding the specified lease square footage. So for example, if a lease of 1000 ft^2 was requested, the lease area would be 31 ft x 31 ft. Your leased area must not extend outside the specified forest.

You are allowed to log any trees in your leased square including trees on the edge of the square. However, since Grizzlies are a threatened species (and your loggers are scared of them), you are not allowed to lease any area containing a bear den (even one on the lease's edge). The value of a lease area is determined by the total volume of trees contained in the area. A ponderosa pine in western Montana has a volume in cubic feet of approximately 0.00216 · d2 · h - 0.4467 where d is the DBH in inches and h is the height in feet.

The survey data comes in a text file that starts with the following three integer values: After this comes a series of data points consisting of four integer values: Here is an example file for a toy 10x7 forest having 12 trees and 2 bear dens:
1 6 40 20
2 5 32 18
3 6 23 15
4 2 50 24
5 2 18 6
5 3 -1 -1
5 4 80 30
6 1 33 17
6 3 29 17
8 3 75 30
8 6 9 4
9 1 -1 -1
10 6 42 21
2 0 49 20

Visual representation of the toy 10 x 7 forest.
The pink shaded area is the optimal 4 ft^2 lease.

Create a program that takes two command line parameters. The first one specifies the area (in square feet) of the lease. The second one is the name of the file that contains the survey data. Your program should read in the survey data from that file. Write an algorithm that evaluates all possible locations for the lease. Choose the location that maximizes the total volume of wood harvested. In the event of a tie, choose the location with the smallest sum of x and y coordinates. Your program should print out the following: If no valid lease can be placed in the forest (due to size or bear issues), print out "No lease possible!". Here are some example runs:
% java LoggingLease 4 10x7.txt
Forest trees     : 12
Forest bear dens : 2
Best southwest   : (6, 1)
Best northeast   : (8, 3)
Best tree volume : 183.16278 ft^3

% java LoggingLease 9 10x7.txt
Forest trees     : 12
Forest bear dens : 2
Best southwest   : (2, 4)
Best northeast   : (5, 7)
Best tree volume : 187.75277999999997 ft^3

% java LoggingLease 18 10x7.txt
Forest trees     : 12
Forest bear dens : 2
Best southwest   : (6, 2)
Best northeast   : (10, 6)
Best tree volume : 202.43472 ft^3

% java LoggingLease 25 10x7.txt
Forest trees     : 12
Forest bear dens : 2
No lease possible!

% java LoggingLease 2 10x7.txt
Forest trees     : 12
Forest bear dens : 2
Best southwest   : (4, 4)
Best northeast   : (5, 5)
Best tree volume : 155.0733 ft^3
Here is a much bigger square mile forest, some example runs, and a graph of the square mile forest:

% java LoggingLease 10000 square_mile.txt
Forest trees     : 100
Forest bear dens : 10
Best southwest   : (2591, 2185)
Best northeast   : (2691, 2285)
Best tree volume : 573.8091599999999 ft^3

% java LoggingLease 100000 square_mile.txt
Forest trees     : 100
Forest bear dens : 10
Best southwest   : (2375, 1969)
Best northeast   : (2691, 2285)
Best tree volume : 819.70614 ft^3

% java LoggingLease 7767368 square_mile.txt
Forest trees     : 100
Forest bear dens : 10
Best southwest   : (2494, 0)
Best northeast   : (5280, 2786)
Best tree volume : 3610.5308399999994 ft^3

Hints and FAQs:

What file should I use to test this? I strongly, strongly, strongly recommend that you do your development and testing on the toy forest file (10x7.txt). Some of the test runs on the larger file can take well over an hour to run to completion.

Do I have to use separate methods? Well, technically no, you don't have to. But, you will lose points if you don't, and you'll also find that writing test cases and testing your program will go much smoother if you write separate methods to handle different parts of the code.

What if I've already written the code and I didn't use methods? You can always take sections of your code that perform a single function and pull them out into a separate method.

Do I have to write test cases? Well, again, technically, no. But 12 points of the grading are about writing test cases and having your code pass those tests, so I would strongly recommend that you go through that process. Don't forget to document the tests you use for each method in a comment before the method, and any tests you use for the entire program can be documented in either the header comment or in a comment before the main method. You should break your tests into the four categories we talked about in class: valid cases, invalid cases, errors/exceptions, and boundary conditions. Not all test categories will be used on all methods, but you should document it when a particular category is not appropriate.

Some of my testing involves printing out array values to the screen. Should I include that code in the final program I turn in? No. The output from the program that you turn in should look like the example output shown in the assignment. So you can take out any additional print statements before turning in the code. These are useful during development, however, to assure yourself that your code actually passes the tests you have come up with.


Grade Item Logging Lease Points Earned
Program Compiles
Program Runs
Header Comment
Uses Methods
Test Cases Written for Each Method
Test Cases Written for Overall Code
Program Finds Highest Value Lease Section
Program Avoids Bear Dens
Passes All Instructor Tests

Extra credit:
The forest service has loosened up their lease rules. You can now lease rectangular sections of land. You may also request a lease with any area so long as it is less than or equal to the specified area (the area specified on the command line). Occasionally (due to bear dens or forest boundaries), a smaller lease may actually be more profitable. Copy your previous solution into a new class Modify your program to find the optimal lease under the new loosened rules. All extra credit programs should be turned in to the Extra Credit dropbox at the top of the Moodle page.
Submission. Submit your program and optionally via Moodle. Be sure each submitted source file has the required header with your name, username, and a description of the program.

Page last updated: August 15, 2018