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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 48

Thread: Guys, really need your help

  1. #1
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Unhappy Guys, really need your help

    I just don't know where to start, and what to do now, if you have some suggestions please help me out

    Here is a problem:

    Write a program that prints a chart to the screen showing the randomness of the random number generator. The program should prompt the user for a number and then call the random number generator this user inputted number of times. The program should then put the random numbers generated into ten "boxes" corresponding to each tenth of the range of the random numbers. ( If the random number generator behaves in a truly random fashion, each time you run the program the chart should look a little different reflecting the randomness of the random number generator.) After your program can do a chart with numbers, add the code to draw a graph that shows the numbers displayed as asterisks. Keep in mind that you have two System methods: System.out.println () and System.out.prin (). The first prints a new line and the second doesn't

    A sample graph may look like this
    Please enter the number of times that you would like to call the random numbers generator: 100
    The chart showing 100 calls to the random number generator is:
    0 - .1 **********
    .1 -.2 ********
    .2 -.3**********
    .3 -.4*******
    .4 -.5*****
    .5 -.6**********
    .6 -.7****
    .7 -.8************
    .8 -.9*******
    .9-10 *******

    double num = Math,random(); // gives a random number 0 <= num <1


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    Where are you having problems? List the steps the program need to do and work on them one at a time.
    When you have a problem with a step, post the code and your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Alright. I know how to do random numbers, but I don't get how to turn them into stars like in the output above... and if you write 100, it will show you 100 stars.. I really don't have an idea what to do. Here is my code:


    import java.util.Scanner;
    public class shapes
    {
    public static void main (String [] args)
    {
    Scanner scan = new Scanner(System.in);
    int i = 0;
    double num;
    int input;
    System.out.println("enter the number");
    input = scan.nextInt();
    System.out.println("Please, enter the number of times that you would like to call the random number generator: " +input);
    System.out.println("The chart showing " +input+ " calls to the random number generator is:");

    while(i<10)
    {

    System.out.println(Math.random() +i);
    i++;
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    but I don't get how to turn them into stars
    To print many characters on one line use the
    System.out.print("<THE CHAR(S) HERE>") method in a loop.
    When you want to go to the next line use the println() method.

    If you have a count of the number of *s you want on a line, loop that many times with the print() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Daler (June 22nd, 2012)

  6. #5
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    I got it, but for example if you wrote 100 as an input, how do the program knows how many *s are needed? How to make *s instead of just random numbers??

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    how do the program knows how many *s are needed?
    That is what the assignment is all about. Go back and read post#1.

    Count the number of numbers in each range.
    Print out *s for each count.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    So I have to use if-else ?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    That would be one way.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Am I right using "while" instead of "for"? Sorry, I am just new to programming...

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    The for loop is a better fit when you know how many times you want to loop.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    Daler (June 23rd, 2012)

  13. #11
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    but what about double num = Math,random() ? Where do I have to use it?

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    Explain what the code needs to do and why you think the Math class's random() method would be useful.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    what about double num = Math,random(); do I have to use it before loop?

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    You need to work out the logic of the program before you start writing the code. After you get the logic for the program worked out, you will see where to put the call to the random() method.
    Can you list the steps the program must do to solve the problem?

    Your program can have more than one loop in it. You will have to post the logic for the program before you can decide where to put the call to random().
    If you don't understand my answer, don't ignore it, ask a question.

  17. #15
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Ok! I will try, but if I am wrong, sorry...

    1. Understand what the program is supposed to do. ( I understand, but not so clearly as I should)
    2. Initialize and declare the variables(I should declare about 10 variables. It means for each box 10 variables)
    3. Definitely, I need a loop.(as you said, even 2 loops, but unfortunately, I don't know where I should to put the second one)
    4. Create the random numbers inside the loop. (still it's not really clear where)
    5. Use System.out.print() and System.out.println() methods.

    If I skipped something, please tell me

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    I should declare about 10 variables.
    Do you know about arrays? This program should use an array NOT 10 separate variables.
    What will be done inside of the first loop?
    And then in the second loop?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #17
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    I am not really sure about arrays. We didn't cover the topics about arrays though. Can you tell me more about it?
    In the first loop we just put like

    for(double num= Math.random(); num <= 10; num++)

    right?

    I don't know about second one yet...

  20. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Guys, really need your help

    Arrays are a collection of similar, to an extent, an Object array could hold anything for instance, types.

    It has int indexes and would, assuming it was already intiialized, and assuming you wanted to just print out each part of the array,

    for (int i =0; i < array.length; i++)
    {
    System.out.println(array[i]);
    }

    An int array of size 10 would look like this

    int[] array = new int[10];

    it would start be initalized at index 0 and would go up to index 9, (including index 9).

    For loops are usually used on ints. Your one with a double seems weird.

    Math.random could return 0, .5, .5553, .831234532, .23352, etc.
    Last edited by javapenguin; June 23rd, 2012 at 06:59 PM.

  21. The Following User Says Thank You to javapenguin For This Useful Post:

    Daler (June 23rd, 2012)

  22. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    for(double num= Math.random(); num <= 10; num++)
    Go back and reread the assignment. It specifies where the count of the number of loops comes from.
    It is NOT random!!

    If you don't use arrays, that will make the program many times longer than it should be. This assignment should use arrays. You should ask your instructor to make sure.

    Stop trying to write code. Wait Until you have the list of steps the program is to do.
    The first step is to ask the user a question.
    The second step is to get his response.
    Now fill in what the program is to do for the rest.
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    Daler (June 23rd, 2012)

  24. #20
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    so if I will use arrays, it should look like this?

    import java.util.Scanner;
    public class shapes
    {

    public static void main (String [] args)
    {

    Scanner scan = new Scanner(System.in);
    int numberOfTimes;

    int[] count = new int[10];
    double n = Math.random();

    System.out.println("enter the number");
    numberOfTimes = scan.nextInt();

    System.out.println("Please, enter the number of times that you would like to call the random number generator: " +numberOfTimes);
    System.out.println("The chart showing " +numberOfTimes+ " calls to the random number generator is:");

    while(numberOfTimes <= 10)
    {

    if( n < 0.1 ) count[0]++;
    else if( n < 0.2 ) count[1]++;
    else if( n < 0.3 ) connt[2]++;
    else if( n < 0.4 ) connt[3]++;
    else if( n < 0.5 ) connt[4]++;
    else if( n < 0.6 ) connt[5]++;
    else if( n < 0.7 ) connt[6]++;
    else if( n < 0.8 ) connt[7]++;
    else if( n < 0.9 ) connt[8]++;
    else if( n < 1.0 ) connt[9]++;
    }
    }
    }

  25. #21
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Guys, really need your help

    Actually, you might possibly be able to use a for loop for that one, though the while should be kept as well.

    for (int k = 1; k <=10; k++)
    {


    if ( n < 0.1k) count(k-1)++;


    }

    Also, you might want to keep changing n as it will keep the same value unless you put it in the while loop for the other 9 times.

    Have another variable called numberOfTimes2 and also have it start out at 0.

    Change the while loop to

    while (numberOfTimes2 <= numberOfTimes)

    if you're going to be scanning in the number of times and iterating through that many, which is what it appears you're doing.

  26. The Following User Says Thank You to javapenguin For This Useful Post:

    Daler (June 23rd, 2012)

  27. #22
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    What does this mistake mean:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException… -3
    at pratice.main(pratice.java:16)

    it should work now.. I don't know why it does appear that way...

  28. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    On line 16 the program tried to use an index that was not in the range of valid indexes for the array. Array indexes start at 0 and go to the array length-1. -3 is not a valid index.

    Please post the code that created the error. The posted code does not compile.



    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    Last edited by Norm; June 24th, 2012 at 07:28 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  29. #24
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Alright! Here is my code:

    import java.util.*;
    public class Shapes
        {
        public static void main(String [] args)
             {
                   Scanner S=new Scanner(System.in);
                   System.out.println("Enter how many random numbers you want to generate");
                   int N=S.nextInt();
                   Random r = new Random();
                   int []freq=new int[10];
     
                   for(int i=0;i<10;i++)freq[i]=0;
     
                   for(int i=0;i<N;i++)
                        {
                          int k=r.nextInt()%10;
                          freq[k]++;
                         }
                   for(int i=0;i<10;i++)
                        { 
                          for(int j=0;j<freq[i];j++)System.out.print("*");
                          System.out.println();
                        }
     
             }
    }

  30. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Guys, really need your help

    Does it work as you want it to?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. HI guys
    By nchmurali in forum Member Introductions
    Replies: 2
    Last Post: December 15th, 2011, 02:43 AM
  2. hi guys
    By johnyabu@gmail.com in forum Member Introductions
    Replies: 1
    Last Post: February 2nd, 2011, 12:13 PM
  3. Hey guys
    By StarKannon in forum Member Introductions
    Replies: 2
    Last Post: February 2nd, 2011, 04:58 AM
  4. Hi Guys
    By dazzl3r in forum Member Introductions
    Replies: 1
    Last Post: December 7th, 2010, 04:04 PM
  5. Hi guys
    By nextngema in forum Java SE APIs
    Replies: 2
    Last Post: July 17th, 2010, 04:34 PM