Write an error-free Java program to do the following things. Type your program a
ID: 3808668 • Letter: W
Question
Write an error-free Java program to do the following things. Type your program and turn in at the beginning of class on 3 April 2017. The program should prompt the user to input a sequence of numbers and then displays the numbers as a bar chart using asterisks. The numbers must be stored in an array. There will be 6 numbers in total. Write a method to determine the largest value in the array and then from main0 display the largest value. Sample output Input population of state 1 (in millions):2 Input population of state 2 (in millions):4 Input population of state 3 (in millions):1 Input population of state 4 (in millions): 6 Input population of state 5 (in millions):4 Input population of state 6 (in millions): 3 .Max value entered = 6Explanation / Answer
//code
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class Population {
public static void main(String argv[])
{
int n=6,i,j;//variable declaration
int a[]=new int[n];//array variable to store input values...
Scanner sc = new Scanner(System.in);//variable to read number
//reading population in millions of 6 states
i=0;
while(i<n)
{
System.out.print("Input population of state "+(1+i)+" (in millions ) :");//prompting input
a[i]=sc.nextInt();//reading number
i++;
}
//displaying in bar chart and calculating max
i=0;
int max=a[i];
while(i<n)
{
j=0;
System.out.print(".");//printing bar chart
while(j<a[i])
{
System.out.print("*");
j++;
}
System.out.println();
if(a[i]>max)max=a[i];//calculating max
i++;
}
//printing max
System.out.println("Max value entered = "+max);
}
}
ouput:-
run:
Input population of state 1 (in millions ) :2
Input population of state 2 (in millions ) :4
Input population of state 3 (in millions ) :1
Input population of state 4 (in millions ) :6
Input population of state 5 (in millions ) :4
Input population of state 6 (in millions ) :3
.**
.****
.*
.******
.****
.***
Max value entered = 6
BUILD SUCCESSFUL (total time: 14 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.