# Histogram Java Program

• October 13th, 2011, 01:18 PM
djl1990
Histogram Java Program

1. Design and implement an applicaton that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 1000 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.

1- 10 | ***
11- 20 | **
21- 30 | *
31- 40 | **
41- 50 | *
51- 60 | *
61- 70 | *
71- 80 |
81- 90 | ***
91-100 | *

I have coded this:

Code :

```package componentsandservices;   import java.util.Scanner;   public class Histogram {   public static void main(String[] args) {   Scanner Keyboard = new Scanner(System.in);   int n, nextt, ix;   // how many numbers to read?   do { System.out.print("Please type the number of numbers:"); n = Keyboard.nextInt(); } while (n < 0); int[] numbers = new int[n]; //read in the numbers for (ix = 0; ix < n; ix++) { // the number must be between 1 and 100 do { System.out.println((ix + 1) + ") Please type a new number in the range 1 and 100:"); nextt = Keyboard.nextInt(); } while (nextt < 1 || nextt > 100); numbers[ix] = nextt; } // create the histogram values String[] stars = {" 1-10 |", "11- 20 | ", "21- 30 | ", "31- 40 | ", "41- 50 | ", "51- 60 | ", "61- 70 | ", "71- 80 | ", "81- 90 | ", "91-100 | "}; //10 strings for (ix = 0; ix < n; ix++) { nextt = numbers[ix]; if (nextt < 11) { stars[0] += "*"; } else if (nextt < 21) { stars[1] += "*"; } else if (nextt < 31) { stars[2] += "*"; } else if (nextt < 41) { stars[3] += "*"; } else if (nextt < 51) { stars[4] += "*"; } else if (nextt < 61) { stars[5] += "*"; } else if (nextt < 71) { stars[6] += "*"; } else if (nextt < 81) { stars[7] += "*"; } else if (nextt < 91) { stars[8] += "*"; } else { stars[9] += "*"; } } for (ix = 0; ix < 10; ix++) { System.out.println(stars[ix]); }     } }```

And i am getting the following output:

Code :

```run: java.lang.NoClassDefFoundError: componentsandservices/Main Caused by: java.lang.ClassNotFoundException: componentsandservices.Main at java.net.URLClassLoader\$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher\$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: componentsandservices.Main. Program will exit. Exception in thread "main" Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)```

Please does anyone know whats wrong?
• October 13th, 2011, 01:20 PM
KevinWorkman
Re: Histogram Java Program
Your compiler is looking for a class named Main. I don't see one here. How are you trying to compile this?
• October 13th, 2011, 01:59 PM
djl1990
Re: Histogram Java Program
The class called Histogram is the Main class so i dont understand.
• October 13th, 2011, 02:19 PM
copeg
Re: Histogram Java Program
Quote:

Originally Posted by djl1990
The class called Histogram is the Main class so i dont understand.

You are trying to run a class called Main and there is only a class called Histogram. Having a main method and a class named Main are two separate things...you need to pass to the JRE the name of the class that contains the main method - in this case Histogram
• October 13th, 2011, 02:57 PM
djl1990
Re: Histogram Java Program
Working now
• October 18th, 2011, 08:58 PM
cr125jairme
Re: Histogram Java Program
Is there any way to accomplish this without IF statements?
• October 19th, 2011, 04:08 AM
Mr.777
Re: Histogram Java Program
Quote:

Originally Posted by cr125jairme
Is there any way to accomplish this without IF statements?

Try to do with switch statement.
• October 19th, 2011, 07:59 AM
KevinWorkman
Re: Histogram Java Program
Quote:

Originally Posted by Mr.777
Try to do with switch statement.

How would that help with the range of values that the OP is using?

djl- the short answer to your question is "yes". What have you tried?
• October 19th, 2011, 02:12 PM
cr125jairme
Re: Histogram Java Program
I am basically doing the exact same project. Instead of starting a new thread, I was just hoping it would be cool to ask the question on this one. I understand how to solve the problem with a nested IF statement, but we are told that we can NOT use one. So these are the words of the instructor,
"A mathematical calculation should be used to determine which of the ten "bin" counts will be incremented for each valid integer input value. A multi-level nested IF structure should NOT be used for this determination. "

I asked the professor a question about it and he responded "All you should need is a mathematical expression to determine the bin value. "

I am stuck on this and dont know what the professor means. Any ideas?
• October 19th, 2011, 02:31 PM
cr125jairme
Re: Histogram Java Program
For/While?
• October 19th, 2011, 11:50 PM
Mr.777
Re: Histogram Java Program
[QUOTE=KevinWorkman;46263]How would that help with the range of values that the OP is using?

QUOTE]
I guess, you can put conditions in switch cases though. That's how i was talking about.
• October 20th, 2011, 07:44 AM
KevinWorkman
Re: Histogram Java Program
Quote:

Originally Posted by Mr.777
I guess, you can put conditions in switch cases though. That's how i was talking about.

That doesn't really eliminate if statements then. I appreciate you trying to help, but please don't offer suggestions if they're only guesses- or at least include a disclaimer that you're just guessing.
• October 20th, 2011, 07:46 AM
KevinWorkman
Re: Histogram Java Program
Quote:

Originally Posted by cr125jairme
I am basically doing the exact same project. Instead of starting a new thread, I was just hoping it would be cool to ask the question on this one.

You should have started your own thread, with a link to this one. Oh well, here we are now.

Quote:

Originally Posted by cr125jairme
I asked the professor a question about it and he responded "All you should need is a mathematical expression to determine the bin value. "

I suggest you write down all of the numbers, then write which index it should map to. Do you see a pattern?

Quote:

Originally Posted by cr125jairme
I am stuck on this and dont know what the professor means. Any ideas?

What have you tried?
• October 20th, 2011, 11:53 PM
Mr.777
Re: Histogram Java Program
Quote:

Originally Posted by KevinWorkman
That doesn't really eliminate if statements then. I appreciate you trying to help, but please don't offer suggestions if they're only guesses- or at least include a disclaimer that you're just guessing.

By the way, that guess was for you to know that switch statements work in that case too.
• October 21st, 2011, 10:41 AM
KevinWorkman
Re: Histogram Java Program
Quote:

Originally Posted by Mr.777
By the way, that guess was for you to know that switch statements work in that case too.

Right, switch statements can sometimes be used instead of if statements. But they don't really work in the case of ranges, which is what the OP has, so it's not really a great suggestion. It will just serve to confuse the OP and send him down the wrong path, which is a waste of his (and our) time. I appreciate you trying to help, but it's a pain in the butt to have to correct incorrect advice.
• October 24th, 2011, 12:46 AM
Mr.777
Re: Histogram Java Program
Quote:

Originally Posted by KevinWorkman
Right, switch statements can sometimes be used instead of if statements. But they don't really work in the case of ranges, which is what the OP has, so it's not really a great suggestion. It will just serve to confuse the OP and send him down the wrong path, which is a waste of his (and our) time. I appreciate you trying to help, but it's a pain in the butt to have to correct incorrect advice.

Anyways, thanks for your guide man. I will try to guide more well, for the next time. Sorry.
• October 24th, 2011, 06:42 AM
KevinWorkman
Re: Histogram Java Program
Quote:

Originally Posted by Mr.777
Anyways, thanks for your guide man. I will try to guide more well, for the next time. Sorry.

It's okay, happens to the best of us. Part of the learning experience.
• October 24th, 2011, 06:47 AM
Mr.777
Re: Histogram Java Program
Quote:

Originally Posted by KevinWorkman
It's okay, happens to the best of us. Part of the learning experience.

Ops forgot to say thanks for guiding me. I hope you all (Experts) will guide me in the same way, you did.
I am learning alot here. The best learning community is this.
Thanks to all of you.