Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Histogram Java Program

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Histogram Java Program

    Hi here is my task.

    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:

    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:

    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Histogram Java Program

    The class called Histogram is the Main class so i dont understand.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Histogram Java Program

    Quote Originally Posted by djl1990 View Post
    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

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Histogram Java Program

    Working now

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Histogram Java Program

    Is there any way to accomplish this without IF statements?

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Histogram Java Program

    Quote Originally Posted by cr125jairme View Post
    Is there any way to accomplish this without IF statements?
    Try to do with switch statement.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Histogram Java Program

    Quote Originally Posted by Mr.777 View Post
    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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  10. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Histogram Java Program

    For/While?

  11. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Histogram Java Program

    Quote Originally Posted by Mr.777 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Histogram Java Program

    Quote Originally Posted by cr125jairme View Post
    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 View Post
    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 View Post
    I am stuck on this and dont know what the professor means. Any ideas?
    What have you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Histogram Java Program

    Quote Originally Posted by KevinWorkman View Post
    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.

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Histogram Java Program

    Quote Originally Posted by Mr.777 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Histogram Java Program

    Quote Originally Posted by KevinWorkman View Post
    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.

  17. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Histogram Java Program

    Quote Originally Posted by Mr.777 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #18
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Histogram Java Program

    Quote Originally Posted by KevinWorkman View Post
    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.

Similar Threads

  1. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  2. Help with this java program
    By Nemphiz in forum Object Oriented Programming
    Replies: 3
    Last Post: April 13th, 2010, 03:09 AM
  3. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  4. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM