-
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
-
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.
-
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++;
}
}
}
-
Re: Guys, really need your help
Quote:
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.
-
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??
-
Re: Guys, really need your help
Quote:
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.
-
Re: Guys, really need your help
So I have to use if-else ?
-
Re: Guys, really need your help
-
Re: Guys, really need your help
Am I right using "while" instead of "for"? Sorry, I am just new to programming...
-
Re: Guys, really need your help
The for loop is a better fit when you know how many times you want to loop.
-
Re: Guys, really need your help
but what about double num = Math,random() ? Where do I have to use it?
-
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.
-
Re: Guys, really need your help
what about double num = Math,random(); do I have to use it before loop?
-
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().
-
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
-
Re: Guys, really need your help
Quote:
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?
-
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...
-
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.
-
Re: Guys, really need your help
Quote:
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.
-
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]++;
}
}
}
-
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.
-
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...
-
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.
-
Re: Guys, really need your help
Alright! Here is my code:
Code java:
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();
}
}
}
-
Re: Guys, really need your help
Does it work as you want it to?