SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Flow Of Control
Exercise:
Programming Projects
Question:6 | ISBN:9780132830317 | Edition: 5

Question

The Fibonacci numbers F n are defined as follows: is 1, is 1, and

i = 0, 1, 2, . . . . In other words, each number is the sum of the previous two num- bers. The first few Fibonacci numbers are 1, 1, 2, 3, 5, and 8. One place where these numbers occur is as certain population growth rates. If a population has no deaths, then the series shows the size of the population after each time period. It takes an organism two time periods to mature to reproducing age, and then the organism reproduces once every time period. The formula applies most straightforwardly to asexual reproduction at a rate of one offspring per time period. In any event, the green crud population grows at this rate and has a time period of five days. Hence, if a green crud population starts out as 10 pounds of crud, then in 5 days, there is still 10 pounds of crud; in 10 days, there is 20 pounds of crud; in 15 days, 30 pounds; in 20 days, 50 pounds; and so forth. Write a program that takes both the initial size of a green crud population (in pounds) and a number of days as input and outputs the number of pounds of green crud after that many days. Assume that the population size is the same for four days and then increases every fifth day. Your program should allow the user to repeat this calculation as often as desired.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// GreenCrudPopulation.java
import java.util.Scanner;
public class GreenCrudPopulation
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		char repeat;
		
		do
		{
			System.out.print("Enter the initial size of a green crud population (in pounds): ");
			int initialPopulation = keyboard.nextInt();
			
			System.out.print("Enter the number of days: ");
			int numberOfDays = keyboard.nextInt();
			
			int f0 = 0;
			int f1 = initialPopulation;
			int f2 = f1 + f0;
			
			for (int day = 1; day <= numberOfDays; day++)
			{
				if (day % 5 == 0)
				{
					f2 = f1 + f0;
					f0 = f1;
					f1 = f2;
				}
			}
			
			System.out.println("The number of pounds of green crud after " + numberOfDays 
					                                                       + " days: " + f2);
			
			System.out.print("\nEnter 'y' or 'Y' to repeat: ");
			repeat = keyboard.next().charAt(0);
			System.out.println();
		} while (repeat == 'Y' || repeat == 'y');

		keyboard.close();
	}
}

Output:

Enter the initial size of a green crud population (in pounds): 10
Enter the number of days: 5
The number of pounds of green crud after 5 days: 10

Enter 'y' or 'Y' to repeat: y

Enter the initial size of a green crud population (in pounds): 10
Enter the number of days: 10
The number of pounds of green crud after 10 days: 20

Enter 'y' or 'Y' to repeat: y

Enter the initial size of a green crud population (in pounds): 10
Enter the number of days: 15
The number of pounds of green crud after 15 days: 30

Enter 'y' or 'Y' to repeat: y

Enter the initial size of a green crud population (in pounds): 10
Enter the number of days: 20
The number of pounds of green crud after 20 days: 50

Enter 'y' or 'Y' to repeat: N

 

0 0

Discussions

Post the discussion to improve the above solution.