Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The population of a town A is less than the population of town B. However, the p

ID: 3905249 • Letter: T

Question

The population of a town A is less than the population of town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town B and the populations of both the towns at that time. (A sample input is: Population of town A 5000 growth rate of town A-4%, population of town B-8000, and growth rate of town B-296) 1. No documentation (-5) 2. Weak documentation (-3) 3. input should be easy to read and follow, including clear prompts for how to enter the town's populations and growth rates (2.1 vs. .021 for 2.1%, etc.) (-3 if not) 4. If Town B's population is entered higher than Town A's (or the growth rate of B entered greater than A's), the program should state this and continue asking for populations until town A is less than B (do while loop) 5. Needs a while loop or do while to calculate the new populations for each town based on the growth rate (-6 if not, or if for loop used) 6. Should output town A's, Town B's populations and how many years it took for town A to take over town B 7. Inefficiencies, up to -5 and noted why TEST: Run with A: 13000, B: 19750. Growth Rates, A: 3%, B: 2%. Final Results (neatly formatted,-4 if not) Town A, 46297. Town B, 46243· 43 years. If populations are incorrect but years are correct, (4). If all output is wrong (-10). If you output population with decimals (-3-cannot have a % of a person)

Explanation / Answer

Solution:

import java.util.InputMismatchException;

import java.util.Scanner;

/*

* File Name: Population.java

* Author: Your's name here

* Creation Date: Today's date

* Description: Calculation the population at the year when population of town A will be greater or equal to

* the population of town B.

* */

public class Population {

static int populationA;

static float rateA;

static int populationB;

static float rateB;

public static void main(String[] args) {

askTownA();//Taking Population of Town A

askTownB();//Taking population of Town B

validatePopulation();//Validating the population and growth rate

computePopulation();//Computing year as well as population of Town A and Town B

}

//Computing the year at which the Town' A population will be equal or greater than the Town's B population

//As well as we are also calculating the Town A and B population.

private static void computePopulation() {

//Initializing the year to the 0

int year=0;

do {

populationA = (int) (((rateA / 100) * populationA) + populationA); // calculates population growth in one year

populationB = (int) (((rateB / 100) * populationB) + populationB);

year++;//Computing the population over a year until population of town A get equals or greater than population B.

} while (populationA < populationB);

//After computing displaying the year and respective town's population.

System.out.println("Year: "+year);

System.out.println("Town A population is "+ populationA);

System.out.println("Town B population is "+ populationB);

}

//Validating the population and growth rate and asking for the input again if user types wrof input

private static void validatePopulation() {

if(populationA > populationB || rateA < rateB) {

askTownA();

askTownB();

}

}

//Taking a valid input for population and growth rate of Town B

private static void askTownB() {

//Continuing asking for the input until and unless user gives the correct information

while(true) {

try {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the Population of Town B. For eg. 2000 ");

populationB = scan.nextInt();

if(populationB <=0) {

continue;

}else {

while(true) {

try {

scan = new Scanner(System.in);

System.out.println("Enter the growth rate of Town B in percentage. For eg. 2.5 ");

rateB = scan.nextFloat();

if(rateB>0 && rateB<=100) {

break;

}else {

continue;

}

}catch(InputMismatchException ime) {

System.out.println("Invalid Rate");

}

}

break;

}

}catch(InputMismatchException ime) {

System.out.println("Invalid Population");

continue;

}

}

}

//Taking a valid input for population and growth rate of Town A

private static void askTownA() {

//Continuing asking for the input until and unless user gives the correct information

while(true) {

try {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the Population of Town A. For eg. 2000 ");

populationA = scan.nextInt();

if(populationA <=0) {

continue;

}else {

while(true) {

try {

scan = new Scanner(System.in);

System.out.println("Enter the growth rate of Town A. For eg. 2.5 ");

rateA = scan.nextFloat();

if(rateA>0 && rateA<=100) {

break;

}else {

continue;

}

}catch(InputMismatchException ime) {

System.out.println("Invalid Rate");

}

}

break;

}

}catch(InputMismatchException ime) {

System.out.println("Invalid Population");

continue;

}

}

}

}

Sample Run:

Enter the Population of Town A. For eg. 2000

dsdwg
Invalid Population
Enter the Population of Town A. For eg. 2000

-34324
Enter the Population of Town A. For eg. 2000

13000
Enter the growth rate of Town A. For eg. 2.5

fwdv
Invalid Rate
Enter the growth rate of Town A. For eg. 2.5

-032
Enter the growth rate of Town A. For eg. 2.5

3
Enter the Population of Town B. For eg. 2000

dvdds
Invalid Population
Enter the Population of Town B. For eg. 2000

-4324
Enter the Population of Town B. For eg. 2000

19750
Enter the growth rate of Town B in percentage. For eg. 2.5

fvfvd
Invalid Rate
Enter the growth rate of Town B in percentage. For eg. 2.5

-434
Enter the growth rate of Town B in percentage. For eg. 2.5

2
Year: 43
Town A population is 46297
Town B population is 46243

Note: Before giving negative feedback do the discuss in the comment section what went wrong. If you really liked it then dont forget to give a thumbs up.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote