Help getting user input into methods


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Help getting user input into methods

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Help getting user input into methods

    OKay I have this program, The problem is I don't know what the idea of the seed value is? I have a rollDice Method and I want to Simulate the rolling of two dice and then add them together and then print out a result. The result is the result of the two random dice being rolled and then added together giving me a random number. Now what I want to do is with the GetMean Method, I want to Check to see and Find the mean, That is However many times the method RollDice was called I want to store that number into the index value in getMean, I then want to divide the rollDice number by the index? Is this possible? I'm just really stuck here, ANy Helps, Hints, Tips appreciated, Thank You!

    import java.util.Random;
    import java.util.*;

    public class Histogram
    {
    private Random randomGenerator;

    /**
    * The Histogram constructor gets a seed value for the
    * random number generator.
    *
    * The constructor needs to create a HistogramUI object. You
    * also need an array to keep track of the dice roll values.
    */
    public Histogram( long seed )
    {
    randomGenerator = new Random(seed);
    int r = randomGenerator.nextInt();
    }

    public int rollDice()
    {
    int result;
    int roll = randomGenerator.nextInt(6) + 1;
    int roll2 = randomGenerator.nextInt(6) + 1;
    return result = roll + roll2;
    }

    public double getMean()
    {
    int total = 0;
    for(int index = 0; index < rollDice(); index ++){
    total = rollDice() / index;
    // total = rollDice();
    }
    return total;
    }
    //public double getMedian()
    //{
    //}

    /**
    * This method needs to call the HistogramUI methods to set up and
    * display a Histogram.
    */
    public void displayHistogram()
    {
    HistogramUI histogram = new HistogramUI( "Dice" );
    histogram.setMax(rollDice());

    histogram.setRollCount(2, 12);

    histogram.setMean(getMean());

    histogram.setMedian(3.2);

    histogram.setMode("23");

    histogram.setVisible();
    }


    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I have adjusted the code a bit. The way you were calculating the mean
    would yield unprecise results. You should do the mean calculation after
    you have accumulated the total, and then divide by the count.
    Code:
    import java.util.Random;
    import java.util.*;
    import java.text.*;
    
    public class Histogram {
      private Random randomGenerator;
      private static DecimalFormat dF=new DecimalFormat("0.000");
      /**
       * The Histogram constructor gets a seed value for the
       * random number generator.
       *
       * The constructor needs to create a HistogramUI object. You
       * also need an array to keep track of the dice roll values.
       */
      public Histogram(long seed) {
        randomGenerator = new Random(seed);
      }
    
      private int rollDice() {
        int result;
        int roll = randomGenerator.nextInt(6) + 1;
        int roll2 = randomGenerator.nextInt(6) + 1;
        return result = roll + roll2;
      }
      public double getMean() {
        int total = 0;
        int rolls=rollDice();
        for (int i=0; i < rolls; i++) {
          total += rollDice();
        }
        return (double)total/(double)rolls;
      }
      public static void main(String[] args) {
        Histogram h=new Histogram(System.currentTimeMillis());
        double mean=h.getMean();
        System.out.println("Mean: "+dF.format(mean));
      }
      //public double getMedian()
      //{
      //}
    
      /**
       * This method needs to call the HistogramUI methods to set up and
       * display a Histogram.
       */
      public void displayHistogram() {
        HistogramUI histogram = new HistogramUI("Dice");
        histogram.setMax(rollDice());
    
        histogram.setRollCount(2, 12);
    
        histogram.setMean(getMean());
    
        histogram.setMedian(3.2);
    
        histogram.setMode("23");
    
        histogram.setVisible();
            
      }
    
    }
    eschew obfuscation

Similar Threads

  1. Replies: 2
    Last Post: 10-17-2002, 04:56 AM
  2. Hiding user controls vs dynamic creation
    By Senkwe in forum ASP.NET
    Replies: 2
    Last Post: 06-11-2002, 06:54 AM
  3. validating input in java??
    By atif in forum Java
    Replies: 1
    Last Post: 05-22-2002, 07:48 AM
  4. Replies: 0
    Last Post: 03-01-2002, 04:17 PM
  5. Replies: 0
    Last Post: 07-20-2001, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center