Results 1 to 13 of 13
  1. #1
    feastfulsaint is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Question I'm having a problem trying wrap up this code

    I have been on this all day and a bit burnt out right now. This is my third draft for a barchart. It compiles, the the JOptionPane appears once. It supposed to appear ten times for multiple integers. Here is the code:

    Java Code:
    import javax.swing.JOptionPane;
    
    public class JUnit3Draft3
      /* Work log:
       * The third draft is a mixture of draft two and some new coding
       * However the JOptionPane won't allow me to enter 10 integers, just one.
       * Started paper coding @ 10:15 Am. Ended paper coding @ 4:22 pm
       * Began physical coding @ 7:07 PM. Ended physical coding @ 7:44 pm
       * Outcome: JOptionPane appears once, but outcome is successful.
       * Error message working. Reworked code a large number of ways, but 
       * JOptionPane stills appears only once. Submitted project for peer review.
       * Items to be added: -1 to quit program.
       */
    {
      public static void main ( String[] args )
      {
        int[] array = new int[11]; //it takes numbers 0 - 99
          int num = 0;
          int count = 0;
          int total = 0;
          String input;
          {
            input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
            num = Integer.parseInt(input);
            while (num < 0 || num > 99)
            {
              JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                            "ERROR!", JOptionPane.ERROR_MESSAGE );
              num = Integer.parseInt(input);
             input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");//Suggested by professor Imroz.
            }
            array [ count ] = num;
    
        System.out.println( "number average:" );
        int[] frequency = new int[11];
       
          ++frequency[ num / 10 ];
        for ( count = 0; count < frequency.length; count++)
        {
          if (count == 10)
            System.out.printf( "%5d: ", 100);
          else
            System.out.printf( "%02d-%02d: ",
                              count * 10, count * 10 + 9 );
          for ( int stars = 0; stars < frequency[ count ]; stars++ )
            System.out.print("*");
          System.out.println();
        }
      }
      }
    }
    I think the answer is simple, but again I am burnt out.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    feastfulsaint is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    I have the JOptionPane to appear ten times, but the last integer entered is only being placed in the barchart.
    Java Code:
    import javax.swing.JOptionPane;
    
    public class JUnit3Draft3
    {
      public static void main ( String[] args )
      {
        int[] array = new int[10];
          int num = 0;
          int count = 0;
          String input;
         for(count = 0; count < 10; count++)
              {
            input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
            num = Integer.parseInt(input);
            while (num < 0 || num > 99)
            {
              JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                            "ERROR!", JOptionPane.ERROR_MESSAGE );
              num = Integer.parseInt(input);
             input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");//Suggested by professor Imroz.
            }
            array [ count ] = num;
         }
        System.out.println( "number average:" );
        int[] frequency = new int[10];
       
          ++frequency[ num / 10 ];
        for ( count = 0; count < frequency.length; count++)
        {
          
          if (count == 10)
            System.out.printf( "%5d: ", 100);
          else
            System.out.printf( "%02d-%02d: ",
                              count * 10, count * 10 + 9 );
          for ( int stars = 0; stars < frequency[ count ]; stars++ )
            System.out.print("*");
          System.out.println();
        }
      }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Can you describe the logic of your program?
    What is each variable used for?
    For example the two arrays: "array" and "frequency"

    You don't seem to have a design with the way you are using variables.

  5. #5
    feastfulsaint is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Write a program that reads in a list of numbers from the user and generates a histogram of those numbers. Your program must include the following functionality:



    • Uses a JOptionPane to get the numbers from the user

    • Accepts up to 10 integers between 0 and 99

    • Displays a bar chart histogram with a bucket size of 10 (that is, it shows how many numbers between 0 and 9 were entered, how many between 10 and 19 were entered, etc)

    • You can assume that the user will enter an integer

    • You cannot assume that the user will enter a value between 0 and 99. If they enter a number outside that range, simply ignore it (that is, do not include this data in your histogram output) it is easier if you place a error message

    • The user will specify an end of the input by entering -1 (that is, when the user enters -1, you should stop requesting numbers)

    • Your program must use a single, 10 element array of integers that keeps track of the histogram information

    • Your histogram image should use filled rectangles or stars to graphically display the results. Be sure that you label your axes and that there is enough room to display the results.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    That looks the the problem definition.
    Now you need to come up with a design for your program that solves that problem.
    Do you have that? You're wasting your time if you code before you design.

    Can you answer these questions I asked before?
    What is each variable used for?
    For example the two arrays: "array" and "frequency"

  7. #7
    feastfulsaint is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    The array is for the numbers entered and the frequency is for the bar chart to determine how many times the numbers in the same range ex: 0-9, 10-20 is entered and displays the out come via * or []

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Ok for array.
    Now what about the frequency array? How do you use it to compute the number of times?
    Explain your logic on its use.

  9. #9
    feastfulsaint is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    I placed the frequency because when it was
    [CODE]System.out.println( "number of times numbers has been entered:" );


    ++array[ num / 10 ];
    for ( count = 0; count < array.length; count++)
    {

    if (count == 10)
    System.out.printf( "%5d: ", 100);
    else
    System.out.printf( "%02d-%02d: ",
    count * 10, count * 10 + 9 );
    for ( int stars = 0; stars < array[ count ]; stars++ )
    System.out.print("*");
    System.out.println();[CODE]
    the final out come would be ****** in all areas. Despite what numbers are entered. I figured I placed a frequency in order to keep track.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Sorry, I don't follow what you are trying to explain.
    Your posting code vs showing me the design for how the program should execute.
    Can you write pseudo code to show the steps you are taking to solve the problem?

    For example the first part of your program does this:
    loop ten times
    ask user for a number
    validate number is in range 1-99
    save number in next slot in array
    end loop

    Now what should the program do to solve the problem?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    If you don't want to bother designing your code, here's a suggestion to help you debug it.
    Show the contents of your arrays after they have been filed and before you use them by calling the Arrays.toString(arrayhere) method

  12. #12
    feastfulsaint is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Sorry for not responding last night. Had to go to work, but here is a revision of the code and little by little I am slowly getting it to show what I want. I am currently creating a fourth draft with arrays.toString method. I will post that code also when I am done.

    [Code]import javax.swing.JOptionPane;

    public class JavaAssignmentUnit3Draft3
    /*
    * Items to be added: -1 to quit program.
    */
    {
    public static void main ( String[] args )
    {
    int[] array = new int[10];
    int num = 0;
    int count = 0;
    String input;
    int[] frequency = new int[10];
    for(count = 0; count < 10; count++)
    {
    input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
    num = Integer.parseInt(input);

    while (num < 0 || num > 99)
    {
    JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
    "ERROR!", JOptionPane.ERROR_MESSAGE );
    input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");//Suggested by professor Imroz.
    num = Integer.parseInt(input);
    ++frequency[ num / 10 ];

    }
    array [ count ] = num;
    }
    System.out.println( "number of times numbers has been entered:" );

    for ( count = 0; count < frequency.length; count++)
    {

    if (count == 10)
    System.out.printf( "%5d: ", 100);
    else
    System.out.printf( "%02d-%02d: ",
    count * 10, count * 10 + 9 );
    for ( int rectangel = 0; rectangel < array[ count ]; rectangel++ )
    System.out.print("[]");
    System.out.println();
    }
    }
    }[Code]

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    You seem to be randomly moving code around. This is called the "shotgun" approach.
    You keep changing things hoping it will fix your problems.

    have you noticed that your code tags are not working? The ending tag, like HTML or XML needs a leading \ before the word "code"

Similar Threads

  1. JTextArea Wrap Around or something?
    By Krooger in forum New To Java
    Replies: 6
    Last Post: 11-28-2009, 04:29 AM
  2. Eclipse import line wrap
    By bogomips in forum Eclipse
    Replies: 0
    Last Post: 11-26-2008, 04:16 PM
  3. How to create tool bar (wrap on resize)
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:10 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
  •