THINK FIRST·CODE LATER

← All labs

Population projection with constants

Problem

In a region there is one birth every 40 seconds, one death every 95 seconds and one new arrival every 120 seconds. Population reads the current population and a number of years, and projects the population (a year has 365 days).

Declare the four rates and the number of seconds in a year as named constants (final). The yearly counts are whole numbers (integer division: a fraction of a birth does not count). For the input

9250000 5

the program prints

Seconds in a year: 31536000
Births per year: 788400
Deaths per year: 331957
Arrivals per year: 262800
Change per year: 719243
Population in 5 years: 12846215

The population is a long. Why is (long) changePerYear * years safer than changePerYear * years?

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l3-7-data-1.txt

9250000 5
What your program must print for l3-7-data-1.txt
Seconds in a year: 31536000
Births per year: 788400
Deaths per year: 331957
Arrivals per year: 262800
Change per year: 719243
Population in 5 years: 12846215

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l3-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content l3-7-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.