Results 1 to 20 of 22
 03282012, 12:03 AM #1Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Already finished 15. Wondering if Someone can help with Steps 69. Please & Thanks!
EventFrequency.java
Java Code:import java.util.Scanner; public class EventFrequency { public static void main(String[] args) { // set up to get user input Scanner scan = new Scanner(System.in); // ask user for the average number of events per time interval System.out.println("What average number of events per time interval do you want?"); int lambda = scan.nextInt(); // ask user for the number of time intervals to observe System.out.println("How many time intervals do you want to observe?"); int times = scan.nextInt(); // instantiate a Poisson object named myDist with average number of events Poisson myDist = new Poisson(lambda); // step 1: // declare an int array named "counts" to count coin flip occurences // make its size one larger than 3 * lambda int counts [] = new int [3 * lambda + 1]; // step 2: // initialize all of the values in the counts array to 0 for ( int i =0; i < counts.length; i++) { counts[i] = 0; } // step 3: // count the number of events occurring in each time interval observed for (int i =0; i < times; i++) { int k = myDist.getValue(); counts[k]++; } // step 4: // print out the estimated probabilities of zero events and lambda events float zeroevents= counts[0] / (float)times; float lambdaevents = counts[lambda] / (float)times; System.out.println("Zero Events \t" +zeroevents); System.out.println("Lambda Events \t" +lambdaevents); // step 5: // instantiate an object of the Histogram class with // the array to be drawn, the indices of valid data, // and the maximum length of the bars in the histogram // // call its draw methods to draw the two histograms int minIndex = 0; int maxIndex = counts.length1; int maxLength = (100 * counts[lambda] / times); Histogram event = new Histogram(counts, minIndex,maxIndex,maxLength); event.drawHor(); event.drawVer(); } } /*201220*/ Poisson.java import java.util.Random; public class Poisson { private int lambda; private double values[]; public Poisson(int lambda) { this.lambda = lambda; this.values = new double [3 * lambda]; this.values[0] = functionOfKAndLambda(0); for (int i = 1; i < values.length; i++) this.values[i] = this.values[i1] + functionOfKAndLambda(i); } private double functionOfKAndLambda(int k) { return Math.pow(lambda, k) * Math.pow(Math.E, lambda) / factorial(k); } private long factorial(long n) { long factorial = 1; // write the code for a loop to calculate factorial of n here for (long i = 2; i <= n; i++) factorial = i * factorial; return factorial; } public int getValue() { double random = Math.random(); for (int i = 0; i < values.length; i++) if (random < values[i]) return i; return values.length; } } /*201220*/ Histogram.java public class Histogram { private int [] values; private int minIndex; private int maxIndex; private int maxLength; /** constructor for histogram drawing class * @param values the array of integer values to draw * @param minIndex the lowest index in the array for the range to draw * @param maxIndex the highest index in the array for the range to draw * @param maxLength the length of line to draw for the largest value */ public Histogram(int [] values, int minIndex, int maxIndex, int maxLength) { // initialize the values of the instance variables from the constructor parameters this.values = new int [maxIndex + 1]; // allocate memory for a copy of the values array this.minIndex = minIndex; this.maxIndex = maxIndex; this.maxLength = maxLength; // step 6: // find largest number in values array for scaling length of bars for (int i =0; i < values.length; i++); { int maxValue= values.length; } // step 7: // copy data from values array to this.values array while scaling to maximum length of bars for (int i=0; i < values.length; i++) { float bars = (float)maxLength / (float)maxIndex; } } /** draw a horizontal bar graph */ public void drawHor() { // step 8: // draw horizontal bar graph (one line per roll value) for(int i = 0; i < values.length; i++); { for(int j = 0; j < values[i]; j++); { values=System.out.println(" *"); } } } /** draw a vertical bar graph */ public void drawVer() { // step 9: // draw vertical bar graph (one column per roll value) } }/*201220*/
69(Histogram.java)
6. and 7. We may have very large values in the values array supplied as a parameter. Since we want to limit the size of the histogram bar graph to maxLength, we need to scale the data in the values array when we copy it into the instance copy of the data in the array. This consists of two loops. The first loop finds the largest value in the values array. (Declare, initialize, and use a variable named something like “maxValue”). The second loop multiplies each value in the values array by the max length we want for bars (maxLength) and divides by the largest value found (“maxValue”). Remember the limitations of multiplication and division for integer variables. You may need to cast the integer values to double for the calculation and cast the result back to integer to update the value in each counts array element with good “resolution”. You can also get good resolution by performing integer multiplications and divisions in the correct order. See if you can do that as it will have better run time performance.
8. In the drawHor method, your code must draw a horizontal bar graph of the data in the values array. (See Sample Output) This will require two nested loops. The outer loop will be a scan through each element of the values array and the inner loop will print an asterisk from 1 through the value in the outer loop element of the values array. Print the values array integer at the end of each bar of asterisks.
9. In the drawVer method, your code must draw a vertical bar graph of the data in the values array. (See Sample Output) This is a little trickier than the horizontal bar graph. You still need two nested loops. The outer loop will count down through all the values from maxLength to 1 to print each line. The inner loop will print a piece of each line for each value (index into the values array). If the count for the value is greater than or equal to the decreasing count of the outer loop, print an asterisk with a space on either side of it. If the count for the value is not greater than or equal to the value of the outer loop count, print an equivalent number of spaces to maintain the column alignment but not show the “bar” in this column.Last edited by xdrazkalnytex; 03282012 at 02:08 AM. Reason: added code tags
 03282012, 02:01 AM #2
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
Do you have any specific questions or problems with the code you are trying to write?
Please post them.
Can you edit the code in your post and properly indent it?
Normally nested code is indented 34 spaces for every level of nesting within a pair of {}s.
This makes it easier to see the program logic.Last edited by Norm; 03282012 at 02:03 AM.
If you don't understand my response, don't ignore it, ask a question.
 03282012, 02:12 AM #3Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
Yes, I need help with Steps 69.
Step 6 and 7 I have to do a pair of loops for maxValue and maxLength, but I don't know how to start or finish the loops. I try on it but wasn't sure if it is right or not.
and for Step 8 and 9. I do not know how to make a nested loops for the array to draw the verticle and horizontal bar graph.
Looking for someone to help me finish the program. Can you help me?
 03282012, 02:16 AM #4
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
What kind of loop are you trying to use? Post your code and explain what you want it to do.
Post any compiler errors you get also.If you don't understand my response, don't ignore it, ask a question.
 03282012, 03:34 AM #5Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
Step 6 and 7 require two loops according to the step lists.
the first loop is to find the largest value in the values array (Declare, initialize, and use a variable named something like “maxValue”). The second loop multiplies each value in the values array by the max length we want for bars (maxLength) and divides by the largest value found (“maxValue”)
I was thinking that this step reqiure me to use the for loop.
// step 6:
// find largest number in values array for scaling length of bars
for (int i =0; i < values.length; i++);
{
int maxValue= values.length;
}
// step 7:
// copy data from values array to this.values array while scaling to maximum length of bars
for (int i=0; i < values.length; i++)
{
float bars = (float)maxLength / (float)maxIndex;
}
}
/** draw a horizontal bar graph
*/
step 8 require me to do two nested loop. outer loop will be a scan through each element of the values array and the inner loop will print an asterisk from 1 through the value in the outer loop element of the values array. Print the values array integer at the end of each bar of asterisks. But i have no idea how to do it, i try it but not sure if it is right or not.
public void drawHor()
{
// step 8:
// draw horizontal bar graph (one line per roll value)
for(int i = 0; i < values.length; i++);
{
for(int j = 0; j < maxLength; j++);
{
System.out.println(" *");
}
}
}
/** draw a vertical bar graph
*/
public void drawVer()
{
step 9 require me to do two nested loop also. the outer loop will count down through all the values from maxLength to 1 to print each line. The inner loop will print a piece of each line for each value (index into the values array). If the count for the value is greater than or equal to the decreasing count of the outer loop, print an asterisk with a space on either side of it. If the count for the value is not greater than or equal to the value of the outer loop count, print an equivalent number of spaces to maintain the column alignment but not show the “bar” in this column.
// step 9:
// draw vertical bar graph (one column per roll value)
}
the errors i got is that i can't do array with double values which I am not sure why, So far that's how much I have gotten so far, and I have no idea how to finish it.
 03282012, 03:38 AM #6
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
If you get compiler errors, copy the full text and paste them here. Your edited version leaves off info.
You get an element in an array by using array notation: array[ix]
Take a look at the tutorial:
Arrays (The Java™ Tutorials > The Reflection API > Arrays and Enumerated Types)If you don't understand my response, don't ignore it, ask a question.
 03282012, 03:54 AM #7Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
public class Histogram
{
private int [] values;
private int minIndex;
private int maxIndex;
private int maxLength;
public Histogram(int [] values, int minIndex, int maxIndex, int maxLength)
{
// initialize the values of the instance variables from the constructor parameters
this.values = new int [maxIndex + 1]; // allocate memory for a copy of the values array
this.minIndex = minIndex;
this.maxIndex = maxIndex;
this.maxLength = maxLength;
// step 6:
// find largest number in values array for scaling length of bars
for (int i =0; i < values.length; i++);
{
int maxValue= values.length;
}
// step 7:
// copy data from values array to this.values array while scaling to maximum length of bars
for (int i=0; i < values.length; i++)
{
float bars = (float)maxLength / (float)maxIndex;
}
}
/** draw a horizontal bar graph
*/
public void drawHor()
{
// step 8:
// draw horizontal bar graph (one line per roll value)
for(int i = 0; i < values.length; i++);
{
for(int j = 0; j < maxLength; j++);
{
System.out.println(" *");
}
}
}
/** draw a vertical bar graph
*/
public void drawVer()
{
// step 9:
// draw vertical bar graph (one column per roll value)
}
}/*201220*/
******
// step 6:
// find largest number in values array for scaling length of bars
// step 7:
// copy data from values array to this.values array while scaling to maximum length of bars
********
I just compile this and I got no errors. Can you tell me if I am doing the for loop right or not in step 6 and 7? If I got it wrong can you correct it for me?
 03282012, 01:32 PM #8
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
am doing the for loop rightIf you don't understand my response, don't ignore it, ask a question.
 03282012, 04:21 PM #9Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
it supposed to link to Step 8 and 9 and make the histogram. But step 6 and 7 just find the values, which I don't know how to do
 03282012, 04:22 PM #10
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
Try doing one Step at a time.
What do you want to do for Step 6?If you don't understand my response, don't ignore it, ask a question.
 03282012, 04:31 PM #11Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
6. and 7. We may have very large values in the values array supplied as a parameter. Since we want to limit the size of the histogram bar graph to maxLength, we need to scale the data in the values array when we copy it into the instance copy of the data in the array. This consists of two loops. The first loop finds the largest value in the values array. (Declare, initialize, and use a variable named something like “maxValue”). The second loop multiplies each value in the values array by the max length we want for bars (maxLength) and divides by the largest value found (“maxValue”). Remember the limitations of multiplication and division for integer variables. You may need to cast the integer values to double for the calculation and cast the result back to integer to update the value in each counts array element with good “resolution”. You can also get good resolution by performing integer multiplications and divisions in the correct order. See if you can do that as it will have better run time performance.
Yeah. I am trying to get my loops to work for step 6 and 7 but i can't seem ti get it to work.
Step 6 require a for loop for largest value in the array.
 03282012, 04:35 PM #12
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
To find the largest element in an array:
Try using a for loop that loops for the number of elements in the array.
Use the loop index value to access each element in the array.
Compare each element in the array with another variable that holds the current max value using a if statement
If the current element is greater than the max value, save it as the max value
At the end of the loop the max value variable will hold the largest value found in the array.If you don't understand my response, don't ignore it, ask a question.
 03282012, 04:41 PM #13Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
I have no idea how to do that. Been trying on it for awhile now. Can you help me out?
 03282012, 04:46 PM #14
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
You know how to write a loop because I can see for loops in the code you posted.
What part of what I posted don't you understand?
Do you know how to get the contents of an array element? Here is a sample:
aVar = theArray[theIndexToArray]; // get the value of the array at theIndexToArray
Do you know how to write an if statement to compare two values to see which one is larger?
Do you know how to assign a value to a variable?If you don't understand my response, don't ignore it, ask a question.
 03282012, 04:53 PM #15Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
I know how to write the loop, the part I dont understand is what to put inside of the loop.
"Do you know how to write an if statement to compare two values to see which one is larger?
Do you know how to assign a value to a variable?"
I don't understand those.
for(int i=0; i < values.length; i++)
{
// I don't know what to put here
}
 03282012, 04:58 PM #16
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
Look at posts #12 & #14
Write some pseudo code describing the steps that need to be done in the loop to find the largest element in the array.
Doc on the if statement:
http://docs.oracle.com/javase/tutori...dbolts/if.html
Expressions:
http://docs.oracle.com/javase/tutori...pressions.html
how to assign a value to a variable
An assignment statement: x = 3; // assign 3 to xLast edited by Norm; 03282012 at 05:02 PM.
If you don't understand my response, don't ignore it, ask a question.
 03282012, 05:17 PM #17Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
Doesn't really help at all. Thanks anyway, I'm going to continue to work on it
 03282012, 05:25 PM #18
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
If you can not write down the steps a program is supposed to do, you will not be able to write the code for the program.
There are thousands of examples of code with loops that look at the elements of an array.If you don't understand my response, don't ignore it, ask a question.
 03282012, 05:35 PM #19Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Re: Already finished 15. Wondering if Someone can help with Steps 69. Please & Than
To find the largest element in an array:
Try using a for loop that loops for the number of elements in the array.
Use the loop index value to access each element in the array.
Compare each element in the array with another variable that holds the current max value using a if statement
If the current element is greater than the max value, save it as the max value
At the end of the loop the max value variable will hold the largest value found in the array.
You replay that to me earlier,
quick questions, what is the loop index value?
 03282012, 05:36 PM #20
Similar Threads

Just wondering if somebody could check my IF ELSE statement ?
By sim18 in forum New To JavaReplies: 4Last Post: 03272012, 10:47 AM 
I was wondering if it is possible to record with Java
By Ben132 in forum New To JavaReplies: 1Last Post: 03232012, 05:09 AM 
I was wondering if why Im getting a cant find symbol error can someone please help
By saba2005 in forum New To JavaReplies: 2Last Post: 09252011, 02:48 AM 
Wondering..~
By ryozkidz in forum Java AppletsReplies: 0Last Post: 01202011, 11:09 PM 
Hi, i was wondering if someone could help me
By deathnote202 in forum New To JavaReplies: 4Last Post: 04232010, 05:03 AM
Bookmarks