Results 1 to 8 of 8
  1. #1
    shawnalex is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Struggling to do this in java

    I'm currently trying to make this histogram display vertically but not sure how to. I'm not sure if I just need to edit the current coding or going to need to add more coding. This is how it looks at the moment. Thank you.

    Java Code:
    import java.util.Scanner;
    
    public class mark {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            Scanner scan = new Scanner(System.in);
            
            int[] array = new int[100];
            int num = 0;
            int count = 0;
            int total = 1;
            int max = 0;
            int min = 100;
            int sum = 0;
            
            System.out.println ("Enter students marks in the range 0 to 100\n");
            
            loop: for (count = 0; count <= total; count++)
            {
                System.out.println ("Enter a number:");
                num = scan.nextInt();
                if (num < 0 || num > 100)
                {
                    break loop;
                }
                array[count] = num;
               total = count+1;
            }
            System.out.println ("How many times a number between 0-100 occur.");
            
            String[] asterisk = {"0- 29   | ", "30- 39  | ","40- 69  | ", "70- 100 | "}; //4 strings
            
            for (count = 1; count <= total; count++)
            {
                num=array[count];
                if (num >=0 && num <=29) asterisk [0] +="*";
                else if (num>29 && num <=39) asterisk[1] +="*";
                else if (num>39 && num <=69) asterisk[2] +="*";
                else if (num>69 && num <=100) asterisk[3] +="*";
            }
            for (count =0;count < 4;count++)
                System.out.println(asterisk[count]);
            System.out.println("The total amount of students is " + total);
            
            for (int i = 0; i < total; i++) {
                if (array[i] < min) {
                    min = array[i];
                }
        }
            for (int i = 0; i < total; i++) {
                if (array[i] > max) {
                    max = array[i];
                }
        }
            for (int i = 0; i < total; i++) {
                sum += array[i];
            }
            double avg = (double) (sum) / total;
            
            System.out.printf(" min: " + min);
            System.out.printf("%n max: " + max);
            System.out.printf("%naverage: %.1f", avg);
        }
    }
    Last edited by shawnalex; 12-01-2013 at 11:05 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Struggling to make it go vertical in java

    Please post the program's current output and post an example showing what you want the output to look like.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    shawnalex is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Struggling to make it go vertical in java

    Struggling to make it go vertical in java-untitled.png That is the output but I want it in vertical instead. Struggling to make it go vertical in java-untitled.png The range I do not wish for it to be looking vertically rather that they all look horizontal. But I want the * to be vertical rather than it be horizontal so that it also looks as though they fall into their respective ranges.
    Last edited by shawnalex; 12-01-2013 at 01:48 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Struggling to make it go vertical in java

    I don't know of a way to print numbers on their sides like that.
    For the columns of *s, for each column you will need to remember the number of rows to print on. For the example, the first column has *s on 4 rows, the second column has *s on 5 rows, the last two columns have *s on 3 rows.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    shawnalex is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Struggling to make it go vertical in java

    I don't quite understand what you mean. Also the *'s that are shown within the range can show as any number asterisks. The user inputs any number until they enter a number over 100 to display the histogram. I didn't show the whole output, just cropped out and rotated the image to show how I want the histogram to look.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Struggling to make it go vertical in java

    To me the histogram looks like 4 columns of *s that start at the top with each column containing different number of *s.
    If that is not what you are looking for, please explain.

    In your diagram, going row by row, each column has a different number of *s in it. The number of *s in a column comes from another part of the program that does some counting.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    shawnalex is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Struggling to make it go vertical in java

    This is my output for example:

    run:
    Enter students marks in the range 0 to 100

    Enter a number:
    10
    Enter a number:
    54
    Enter a number:
    56
    Enter a number:
    678
    How many times a number between 0-100 occur.
    0- 29 | *
    30- 39 |
    40- 69 | **
    70- 100 |
    The total amount of students is 3
    min: 10
    max: 56
    average: 40.0BUILD SUCCESSFUL (total time: 14 seconds) I want it so that the histogram is vertical not horizontal but not sure how to do this.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Struggling to make it go vertical in java

    You need to see what each row the program is to print looks like. Take a piece of paper and draw on it what is to go on each row that the computer prints. For example the first few rows might look like this:
    Java Code:
      *  *  *  *
      *  *  *  *
         *
    What is on each row? Some spaces and some *s. The *s are in columns with their length coming from the array where the counts are. There is a relationship between what goes on a row (* or space ) and the count.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2012, 06:10 PM
  2. How to make buttons vertical
    By ninjalord918 in forum AWT / Swing
    Replies: 2
    Last Post: 07-29-2010, 09:10 AM
  3. Struggling with OnKeyPressed Event in java
    By Camzie in forum NetBeans
    Replies: 2
    Last Post: 12-01-2009, 07:58 AM
  4. Struggling with java .ini files
    By Camzie in forum NetBeans
    Replies: 6
    Last Post: 11-24-2009, 03:13 PM
  5. VERTICAL BAR GRAPH in JAVA
    By Robert_85 in forum Java 2D
    Replies: 4
    Last Post: 03-08-2009, 12:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •