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());
}
}
Bookmarks