Note: This and all assignments given in this course can be and must be solved using only the materials that have been discussed in class. Do not look for alternative methods that have not been covered as part of this course.
Program 1 (60 points):
Rangers at the Serengeti National Park are in need of a program to simulate the ecological balance between lion and gazelle populations. Gazelles eat from an unlimited supply of grass; lions eat gazelles. Lions are the only obstacle to increasing population growth for the gazelles. The population of lions increases as the population of gazelles increases.
You have been asked to write a program to calculate the populations of gazelles and lions over a 1000-day period with population counts displayed every 100 days only. The program must request from the user the initial numbers of gazelles and lions. The following formulas are to be used for calculating the day-to-day changes in the gazelle (G) and lion (L) populations:
GTomorrow = (1 + a) ∙ GToday – c ∙ GToday ∙ LToday LTomorrow = (1 - b) ∙ LToday + c ∙ d ∙ GToday ∙ LToday
Where:
= 0.01 Fractional increase in gazelle population without competition from lions = 0.008 Fractional decrease in lion population without gazelle to eat c = 0.00005 Likelihood that a lion will encounter and eat a gazelle d = 0.015 Fractional increase in lion population attributed to a devoured
Finally, as summary information, print the highest individual daily population number of lions and of gazelles and the corresponding average values over the 1000-day time period.
Program details:
The program should welcome the user and prompt the user for input data with clear understandable messaging. Add proper error handling for erroneous input (e.g. values less than one). Make sure the user gets an explanatory error message if the supplied input is invalid. In case of invalid input, the user should be asked to try again until the input provided is valid.
The number of iterations (1000 days) must be defined as a declared constant integer. Values for a, b, c, and d have to be defined as constant as well. Choose the proper data types.
The program must use a repetition structure that loops 1000 times. Within the loop, using today’s values for G and L, compute the corresponding tomorrow values for G and L. While it is impossible to have a portion of a gazelle or of a lion, use double data types for all G and L values anyway.
Use a selection structure to identify a 100th iteration, allowing displaying on the monitor the population values for that iteration.
Use selection structures to determine the highest number individual daily population of both lions and gazelles.
You also need to determine the overall sum of gazelles and lions (for calculating the average) over the 1000-day period to be displayed as summary information.
Summary information has to be printed to the output. Here is a sample run of a typical solution to this problem:
Note on formatting output to print the table:
In order to be able to display the table in a pleasant and easy to read format (e.g. population figures with 2 decimal places, the three columns under the headings and aligned to the left, etc.) you may want to use either
System.out.printf() or System.out.format()methods of System.out. These output methods allow you to specify formatting information for the objects to be displayed, such as the size of the printing field, the alignment
(left, right), and the number of decimal places after the decimal point
(among other things)
The first parameter in either one of these methods is a string that specifies the format. The format is specified using converters and flags. For example:
double pi = Math.PI;
System.out.format("%-4s%10.3f%n", “pi: ”, pi);
will display pi as a number with 3 decimal places after the decimal point, using a printing field of size 10, aligned to the left:
pi: 3.142
In this example:
%-4s indicates that the object to be displayed is a string (s), that will be printed in a field of size 4, aligned to the left (-) of the printing space.
%10.3f indicates that the object to be displayed is a floating-point number (f), that it should be displayed in field of size 10, and with 3 decimal places after the decimal point.
%n is to advance to the next line after displaying pi (similar to \n)
Converts commonly used include f for floating-point numbers, s for strings, n for new line. Flags commonly used include – to align to the right.
Receives a integer number n as its parameter Prints the out all the even numbers between n and 0 (inclusive) Returns the number of even number between 0 and n, for any positive integer n Make sure that you include a base case. What is the recursive case? Let the recursive case solve a simpler problem.
Test your recursive method in a main method that:
Asks the user to enter a positive integer value, or 0 to end the program Validates the user’s input: if the user enters a negative value, the program should issue an error message and ask the user to try again until a valid value is entered Calls the recursive method and prints the returned value Goes back to ask the user to enter a positive integer value, or 0 to end the program (hint: you need a loop)
Note: Please make sure to submit well-written programs for these programming tasks. Good identifier names, useful comments, indentation, and spacing will be some of the criteria that will be used when grading this assignment.