Welcome to Dream.In.Code
Become a Java Expert!

Join 150,058 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,728 people online right now. Registration is fast and FREE... Join Now!




histogram II

2 Pages V  1 2 >  
Reply to this topicStart new topic

histogram II

mds913
6 Jun, 2008 - 11:44 AM
Post #1

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

i reworked my code a little here from my last post. i just can't seem to understand why the histogram won't print out. after it goes through generateUser it jumps back up to Main (Scores = generateUser(students)) and dies. i don't know how to make it go down to the outputHistogram method and print it out...

CODE


/import javax.swing.JOptionPane;

public class brandnew

{
    public static void main(String[]args)
    {
       int students;
       int [ ] scores = new int[10];
       String numOfStudents;
       String posNumber;
       String inputOrRandom=null;
       String answer;
      
        numOfStudents = JOptionPane.showInputDialog("How many students in the class?");
        students = Integer.parseInt(numOfStudents);
        
        while(students < 0)
        {
            posNumber = JOptionPane.showInputDialog("Please enter a positive number.");
            students = Integer.parseInt(posNumber);
        }
            if(students > 0)
            inputOrRandom = JOptionPane.showInputDialog("Would you like to input the numbers or have them random generate? (I/R) ");
            
            if(inputOrRandom.equals("I") || inputOrRandom.equals("i"))
             scores = generateUser(students); //System.out.print("xxxx");
                        
            else if
            (inputOrRandom.equals("R") || inputOrRandom.equals("r"))
            System.out.print("RRR"); //generateRandom(students);  //
            
            else
            return;
            
        }

    public static int [] generateUser(int numOfStudents)
    {
        int i=0;
        int [ ] scores = new int[10];
        int grade;
        
       for(int j = 0; j < 10;++j)
            scores[j] = 0;
          
        for(i = 0;i < numOfStudents;i++)
        {
            do
            {
                grade = Integer.parseInt(JOptionPane.showInputDialog(
                    "enter score from 1 to 100 inclusive"));
            }while(grade < 1 || grade >100);
            
            scores[(grade-1)/10]= scores[(grade-1)/10]+1;
            System.out.print(grade + " ");
        }
       System.out.println();
      return scores;
    }
    
    public static void outputHistogram(int [] scores)
    {
             for(int i = 0; i < 10;++i)
             {
                System.out.print((i*10+1)+"-"+(i*10+10)+":");
                for(int x =0; x < scores[i];++x)
                    System.out.print("*");
                
                System.out.println();
             }
            
             return;
    }
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Histogram II
6 Jun, 2008 - 11:57 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
CODE


     else
        return;

     // this one is missing
     outputHistogram(scores);   is missing after


Suggest to use a scanner a lot easier to use to test your code

User is online!Profile CardPM
+Quote Post

pbl
RE: Histogram II
6 Jun, 2008 - 12:07 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Version with scanner

java


import java.util.Scanner;

public class brandnew

{
public static void main(String[]args)
{
int [] scores = new int[10];
Scanner scan = new Scanner(System.in);

System.out.print("Enter number of students: ");
int students = scan.nextInt();
for(int i = 0; i < students; i++) {
System.out.print("\nEnter score for student #" + (i+1) + ":");
int grade = scan.nextInt();
scores[(grade-1)/10]++;
}
outputHistogram(scores);
}


public static void outputHistogram(int [] scores)
{
for(int i = 0; i < 10;++i)
{
System.out.print((i*10+1)+"-"+(i*10+10)+":");
for(int x =0; x < scores[i];++x)
System.out.print("*");

System.out.println();
}

return;
}
}


User is online!Profile CardPM
+Quote Post

mds913
RE: Histogram II
6 Jun, 2008 - 12:08 PM
Post #4

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

I'm sorry..where does outputHistogram(scores); go exactly?
User is offlineProfile CardPM
+Quote Post

mds913
RE: Histogram II
6 Jun, 2008 - 12:17 PM
Post #5

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

aahh! ok i see it in MAIN.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Histogram II
6 Jun, 2008 - 12:17 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mds913 @ 6 Jun, 2008 - 01:08 PM) *

I'm sorry..where does outputHistogram(scores); go exactly?


after you return if not generateUser or generate random

CODE



     inputOrRandom = JOptionPane.showInputDialog("Would you like to input the numbers or have them random generate? (I/R) ");
            
      if(inputOrRandom.equalsIgnorecase("I")) {
             scores = generateUser(students);
      }
      else if (inputOrRandom.equalsIgnoreCase("R") ) {
            scores = generateRandom(students);
      }      
      else
            return;
      
      outputHistogram(scores);      

User is online!Profile CardPM
+Quote Post

mds913
RE: Histogram II
6 Jun, 2008 - 12:23 PM
Post #7

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

i bow down to your Java wisdom smile.gif

thanks so much. I may have another question later. Now i have to build a method that adds the option of using next.Gaussian instead of manually entering the scores.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Histogram II
6 Jun, 2008 - 12:30 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mds913 @ 6 Jun, 2008 - 01:23 PM) *

i bow down to your Java wisdom smile.gif

thanks so much. I may have another question later. Now i have to build a method that adds the option of using next.Gaussian instead of manually entering the scores.

Excuse my ignorance but what is next.Gaussian ?
A kind of random generator that generates number according to a Gaussian distribution ?

QUOTE(pbl @ 6 Jun, 2008 - 01:27 PM) *

QUOTE(mds913 @ 6 Jun, 2008 - 01:23 PM) *

i bow down to your Java wisdom smile.gif

thanks so much. I may have another question later. Now i have to build a method that adds the option of using next.Gaussian instead of manually entering the scores.

Excuse my ignorance but what is next.Gaussian ?
A kind of random generator that generates number according to a Gaussian distribution ?


Found it !!
Give me 5 minutes

User is online!Profile CardPM
+Quote Post

mds913
RE: Histogram II
6 Jun, 2008 - 12:35 PM
Post #9

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

Yup. i need to generate the same thing as the generateUser method except that the next.Gaussian will generate the scores and curve the Gaussian 'bell curve' distribution. it uses Random generator = new Random(); as the constructor and Math.max and Math.min to keep the scores over 1 and less than 100.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Histogram II
6 Jun, 2008 - 12:38 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions


CODE

    public static int[] generateRandom(int nbStudends) {
        int[] scores = new int[10];
        Random ran = new Random();
        for(int i = 0; i < nbStudends; i++) {
            double x = ran.nextGaussian() * 100.0;
            int grade = (int) x;
                     scores[(grade-1)/10]++;            
        }
        return scores;
    }



User is online!Profile CardPM
+Quote Post

mds913
RE: Histogram II
6 Jun, 2008 - 12:55 PM
Post #11

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

man you are fast! I got an outofbounds exception but i'm going to work with it a bit and tweak it. thanks again!!!
User is offlineProfile CardPM
+Quote Post

pbl
RE: Histogram II
6 Jun, 2008 - 01:03 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mds913 @ 6 Jun, 2008 - 01:55 PM) *

man you are fast! I got an outofbounds exception but i'm going to work with it a bit and tweak it. thanks again!!!

Probably due to to a value of 0 returned
do it that way

CODE

    public static int[] generateRandom(int nbStudends) {
        int[] scores = new int[10];
        Random ran = new Random();
        for(int i = 0; i < nbStudends; i++) {
            int grade = 0;
            while(grade < 1 || grade > 100) {
               double x = ran.nextGaussian() * 100.0;
               grade = (int) x;
            }
                      scores[(grade-1)/10]++;            
        }
        return scores;
    }


This post has been edited by pbl: 6 Jun, 2008 - 01:09 PM
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:36PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month