Results 1 to 3 of 3
  1. #1
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Need Help finishing. Steps 6-9 Please?

    EventFrequency.java

    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.length-1;
    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[i-1] + 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*/




    6-9(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; 03-28-2012 at 12:59 AM. Reason: Revise

  2. #2
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: I need some help with Java. If someone can help me, I would be happy. :)

    What type of help do you want? Do you want someone to code for you or solve a particular problem?

  3. #3
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Re: I need some help with Java. If someone can help me, I would be happy. :)

    I start on this problem. But I have no idea how to finish. If someone would look over at what I did and tell me if I did it right or not.
    Looking for help to finish the problem. Since im not sure what to write, if someone write a code then it would be nice.

Similar Threads

  1. Hello everybody and happy new year...
    By Maisondouf in forum NetBeans
    Replies: 0
    Last Post: 01-02-2012, 01:18 PM
  2. Happy New Year
    By sanjeevtarar in forum Entertainment
    Replies: 7
    Last Post: 12-14-2011, 06:12 AM
  3. Happy Independence Day
    By JosAH in forum Forum Lobby
    Replies: 9
    Last Post: 07-04-2011, 10:55 PM
  4. Happy Bijoya!
    By playwin2 in forum Forum Lobby
    Replies: 1
    Last Post: 11-16-2008, 10:24 PM
  5. Happy B'day Eranga
    By sanjeevtarar in forum Forum Lobby
    Replies: 13
    Last Post: 04-18-2008, 04:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •