Question
Need help coding with Java. Explanation in code would be greatly appreciated. Thanks
Bar graph 2.0 Write a program that accepts a list of up to 100 integers and displays a bar graph showing the frequencies of the values and the corresponding percentage frequency distributions (printed to 1 significant digit). Example runs are shown below. Number of values to graph: Enter the integer values separated by a space: Bar graph for 3 #1111# 01110 100.01 umber of integers to readi Enter the integer values separated by a spaces 1 1 113 2 Bar graph for 1 3 2: i 0 66.7 16.74 16.7 Note that spaces should be included to the end of each row when the final value is not large enough to warrant an # at that height.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Main
{
    public static void main(String args[]) throws Exception
    {
        System.out.println("Number of value to the graph: ");
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int a[]=new int[n];
        int b[]=new int[100];//stores number of occurances of each number
        int sum=0;//to calculate frequency
        System.out.print("Enter the integer values seperated by space: ");
        for(int i=0;i<n;i++)
        {
            a[i]=in.nextInt();//taking input
            b[a[i]]++;
        }

        int max=0;
        System.out.print("Bar graph for ");//printing first line
        for(int i=0;i<100;i++)
        {
            sum=sum+b[i];
            if(b[i]>0)
                System.out.print(i+" ");
            if(max<b[i])
                max=b[i];
        }
        System.out.println();
        int x=max;
        
        /*logic for printing # is that max number should have # from first line
        where as remaining should have from the line of (max-their value)*/
        
        for(int i=0;i<x;i++)
        {
            System.out.print("  ");
            for(int j=1;j<100;j++)
            {
                if(b[j]>=max)
                    System.out.print("# # # # # #  ");
                else
                    System.out.print(" ");
            }
            max--;
            System.out.println();
        }

        for(int i=0;i<100;i++)
            if(b[i]>0)
                System.out.print("-------------");
        System.out.println();

        for(int i=0;i<100;i++)
            if(b[i]>0)
                System.out.print("            "+i);
        System.out.println();

        for(int i=0;i<100;i++)
            if(b[i]>0)
            {
                double k=(double)b[i]*100/sum;
                String s = String.format("%.2f",k);
                System.out.print("        "+s);
            }

    }
}

-sample ICSUsers darsh Desktop-HomeworkLib sample-..src Main java Isamplel-IntelliJ DEA Eile Edit ie Navigate Code Analyze Befacter Build Run Iools vCS Window Help sample STE) Main) Run: Main Main C:\Program Files\Java\jdkー10.0.2(bin\java .exe Number of value to the graph: -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Contuu nity Edition 2018.2(1ib\idea-rt.jar#626 Enter the integer values seperated by space: 11112 3 :↓ | Bar graph for 1 2 3 66.6了 16.6r 16.6 Process finished with exit code θ 4 Run TODO Terminal All files are up-to-date (a minute ago) 17M chars66 line breaks 65:6 CRLF 수 UTF-8 수 . , 0416 Type here to search ofa ^ 4: FNG 31-101-2D19

Add a comment
Know the answer?
Add Answer to:
Need help coding with Java. Explanation in code would be greatly appreciated. Thanks Bar graph 2.0...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT