Write a program that prompts the user to enter the population values of 5 towns.
ID: 671957 • Letter: W
Question
Write a program that prompts the user to enter the population values of 5 towns. After the 5 populations have been entered, the program should display a horizontal bar graph comparing each population. The bars in the graph should be made of asterisks “*” where each asterisk equals 1,000 people. An example interaction is as follows (bold print is what the user enters):
Enter the population of town 1: 12549 [Enter]
Enter the population of town 2: 3578 [Enter]
Enter the population of town 3: 5023 [Enter]
Enter the population of town 4: 1732 [Enter]
Enter the population of town 5: 10522 [Enter]
POPULATION GRAPH:
Town 1: ************
Town 2: ***
Town 3: *****
Town 4: *
Town 5: **********
DETAILS
Name your class Programming1 (Save as Programming1.java).
You are required to follow the rules for correct documentation usage and identifier names as described in the “Documentation Requirements.pdf” file posted on the Canvas website for 143.
You are required to create and use at least 2 methods: getPopulation & drawPopulationBar
getPopulation should receive an integer used to represent or identify the town on which a population is to be entered and a scanner to System.in. It should prompt the user for this town and return the integer value the user enters. As an example, one of the calls to this method might be:
town2Population = getPopulation(2, console); // Enter the population of town 2: 3578 [Enter]
drawPopulationBar should be passed both the integer representing the town and its population value. It should display the town number and the bar of asterisks based on the population value as seen above. As an example, one of the calls to this method might be:
drawPopulationBar(3, town3Population); // Output would be: Town 3: *****
Without the use of arrays, you will have at very least 5 variables for the 5 populations, 5 separate calls to getPopulation and 5 separate calls to drawPopulationBar (You are NOT allowed to use arrays in this assignment).
Explanation / Answer
import java.util.*;
public class Programming1 {
/* Definition of getPopulation() method where program will read input entered by user and store it in to varriable and return that variable to main() method*/
int getPopulation(int n, Scanner s)
{
System.out.print("Enter the population of town"+n+":");
/* 's' is the scanner object through which we will read value enter by use. As user enter Integer so we use 'nextInt(); */
int population=s.nextInt();
System.out.println(population);
return population; // return entered value by user to main() method
}
/* Definition of drawPopulationBar() method where program will display graph for Town. Method take Town no and Population as arguments*/
void drawPopulationBar(int n,int townpopulation)
{
System.out.print("Town"+n+":");
/* logic is for every '1000' value we have to print one '*'. So code will subtract 1000 from value until than '1000' and for every subtraction it will print one '*'.
Like if value is 2000. Then it can be written as 2000= 1000+1000. For every 1000 there will be one '*'
So 2000= * + * means two '*'
Similarly for 3568= 1000+1000+1000+568
So 3568= * + * + * means three '*'
*/
while(townpopulation>=1000)
{
System.out.print("*");
townpopulation=townpopulation-1000;
}
System.out.print(" ");
// As return type of this method is Void. So it will not return anything.
}
/*step 1 : Every Program starts with Main Method(). So we will start looking from Main().*/
public static void main(String[] args)
{
/*step 2 : I have declared 5 variables which will store population of 5 towns enter by user*/
int town1population,town2population,town3population,town4population,town5population;
/*step 3 : Declared new objects to call methods*/
Programming1 p1=new Programming1();
/*step 4 : Create Scanner object to read input from user. "System.in" is used to read input from keyboard because user will enter values using keyboard*/
Scanner s= new Scanner(System.in);
/*step 5 : Now call getPopulation() method five times to get input for 5 towns.In getPopulation() I have passed 2 parameters, First one is town no for which user will input value and second one is Scanner object through which method will read input enter by user.*/
town1population=p1.getPopulation(1, s);
town2population=p1.getPopulation(2, s);
town3population=p1.getPopulation(3, s);
town4population=p1.getPopulation(4, s);
town5population=p1.getPopulation(5, s);
/*step 6 : Similarly call drawPopulationBar() method five times to Display graph for 5 towns.In drawPopulationBar() I have passed 2 parameters, First one is town no for which we will display graph and Second one is value of population enter by user for the town mentioned in first parameter.*/
p1.drawPopulationBar(1,town1population); /* like (1,town1population) 1 is town no and town1population is population of first town. */
p1.drawPopulationBar(2,town2population);
p1.drawPopulationBar(3,town3population);
p1.drawPopulationBar(4,town4population);
p1.drawPopulationBar(5,town5population);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.