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

Write a program in JAVA that prompts the user to enter the population values of

ID: 663846 • Letter: W

Question

Write a program in JAVA 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: **********

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:

Explanation / Answer

package com;

import java.util.Scanner;

public class Population {

   public int[] getPopulation()
   {
       int[] p=new int[5];
       Scanner sc=new Scanner("System.in");
       for(int i=1;i<=5;i++)
       {
       System.out.println("Enter the population of town"+i +":");
       p[i] = sc.nextInt();
       }
       return p;
   }
   public void drawPopulationBar(int arr[], int arr2[])
   {
       int i,j=1;
       for(i=1;i<=5;i++)
       {
           System.out.println("Town"+i+"["+arr[1]+"]:");
           for(j=1;j<=arr2[i];j++)
           {
               System.out.print("*");
           }
       }
   }
   public static void main(String[] args) {
       int arr2[]=new int[5];
       Population pop=new Population();
       int arr[]=pop.getPopulation();
       for(int i=1;i<=5;i++)
       { int temp;
       temp=arr[i];
      
           arr2[i]=(int) (temp%1000);
       }
       pop.drawPopulationBar(arr,arr2);
  
   }

}

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