Write a program that reads a sequence of input values and displays a bar chart o
ID: 3536092 • Letter: W
Question
Write a program that reads a sequence of input values and displays a bar chart of the values in data, using asterisks, like this:
*********************
*************************
*********
**************************
You may assume that all values are positive. First figure out the maximum value in data. That value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks. Improve your program by adding caption to your bar. Prompt the user for the captions and data values. The output should look like this:
Egypt ********************* France ************************* Norway ********* Germany **************************Explanation / Answer
import java.util.*;
/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int[] a=new int[4];
String[] citi_names = new String[4];
for(int i=0; i<4; i++)
{
System.out.println("Enter name of caption : ");
citi_names[i] = in.next();
System.out.println("Enter value for " + citi_names[i] + ": ");
a[i] = in.nextInt();
}
int max = a[0];
for(int i=1; i<4; i++)
{
if(a[i] > max)
max = a[i];
}
System.out.println();
for(int i=0; i<4; i++)
{
System.out.print(citi_names[i]+ " ");
for(int k=0; k<(40*a[i]/max); k++)
System.out.print("*");
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.