Randomset & RandomSetTest


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Randomset & RandomSetTest

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    Randomset & RandomSetTest

    the randomset class works but i am having trouble trying to get the randomsetTest class. i have 2 errors in the randomSetTest. its complaining about my "Randomset" saying it does not recognise this. Another error is that its saying subtract smallest in RandomSet cannot be applied to an int. below is the code for the RandomSet and RandomSetTest programs

    import java.util.Random;

    public class RandomSet
    {
    private double[] my_array;

    public RandomSet(int size, long seed)
    {

    my_array = new double[size];

    Random r = new Random(seed);

    for(int i = 0;i<size;i++)
    {
    my_array[i] = r.nextDouble();
    }
    }

    public double get(int i)
    {
    if(i>=0 && i<my_array.length)
    {
    return my_array[i];
    }
    else
    return -1.0f;
    }

    public double getSum() // this function returns the sum of all the numbers in the array
    {
    double sum = 0.0;
    for(int i = 0;i<my_array.length;i++)
    {
    sum += my_array[i];
    }
    return sum;
    }
    public void addNumber(double x) // this function adds x to each element of the array
    {
    for(int i = 0;i<my_array.length;i++)
    {
    my_array[i] = my_array[i] + x;
    }
    }

    public double getSmallest()
    {
    double smallest = 1.0;
    // returns the smallest number in the array
    for(int i = 0;i<my_array.length;i++)
    {
    if(my_array[i]<smallest)
    {
    smallest = my_array[i];
    }
    }
    return smallest;
    }


    public void subtractSmallest() // subtracts the smallest number in the array from
    {
    double x = getSmallest();
    for(int i = 0;i<my_array.length;i++)
    {
    my_array[i] = my_array[i] - x;
    }
    }



    }




    import java.util.Random;


    public class RandomSetTest
    {


    /** Tests the methods of the RandomSet class */



    public static void main(String[] args)
    {
    RandomSet arraySet = new RandomSet();
    arraySet.addNumber(100);
    arraySet.subtractSmallest(90);
    System.out.println(arraySet.getSum());
    }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This one works.

    I have taken the liberty to "enhance" the program a little bit

    Code:
    import java.util.Random;
    
    public class RandomTest {
    
      private double[] my_array;
    
      // random number part
      public static Random rand=new Random(System.currentTimeMillis());
      public static int getRandom(java.util.Random rand, int min, int max) {
        double d=rand.nextDouble()*(double)max;
        return (int)(d+min);
      }
      public static int getRandomInt(int min, int max) {
        double d=rand.nextDouble()*(double)max;
        return (int)(d+min);
      }
      public static double getRandomDouble(int min, int max) {
        double d=rand.nextDouble()*(double)max;
        return d+(double)min;
      }
      // end random number part
    
      /**
       * Limitless constructor, defaults to 0->1
       */
      public RandomTest(int size) {
        my_array = new double[size];
    
        for (int i = 0; i < size; i++) {
          my_array[i] = getRandomDouble(0, 1);
        }
    
      }
      /**
       * Limit constructor
       *
       */
      public RandomTest(int size, int min, int max) {
    
        my_array = new double[size];
    
        for (int i = 0; i < size; i++) {
          my_array[i] = getRandomDouble(min, max);
        }
      }
    
      public double get(int i) {
        if (i >= 0 && i < my_array.length) {
          return my_array[i];
        }
        else
          return -1.0f;
      }
    
      public double getSum() { // this function returns the sum of all the numbers in the array
        double sum = 0.0;
        for (int i = 0; i < my_array.length; i++) {
          sum += my_array[i];
        }
        return sum;
      }
    
      public void addNumber(double x) { // this function adds x to each element of the array
        for (int i = 0; i < my_array.length; i++) {
          my_array[i] = my_array[i] + x;
        }
      }
    
      public double getSmallest() {
        double smallest = Integer.MAX_VALUE;
        // returns the smallest number in the array
        for (int i = 0; i < my_array.length; i++) {
          if (my_array[i] < smallest) {
            smallest = my_array[i];
          }
        }
        return smallest;
      }
      /**
       * No parameters defined for this method
       */
      public void subtractSmallest() { // subtracts the smallest number in the array from
        double x = getSmallest();
        for (int i = 0; i < my_array.length; i++) {
          my_array[i] = my_array[i] - x;
        }
      }
    
    
      /** Tests the methods of the RandomTest class */
    
      public static void main(String[] args) {
        /**
         * RandomTest has no empty constructor, use the (size) constructor
         * or the (size, min,max) constructor
         */
        RandomTest arraySet = new RandomTest(100, 20,50);
        System.out.println("smallest: "+arraySet.getSmallest());
        arraySet.addNumber(100);
        arraySet.subtractSmallest();
        System.out.println(arraySet.getSum());
      }
    }
    Last edited by sjalle; 08-15-2005 at 08:25 AM.
    eschew obfuscation

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