嘿伙计们,我在弄清楚如何为我的 Array 类编写驱动程序类时遇到问题。
我的问题
1.如何让我的Array类中的数组引用我的Driver类中的数组?
2.如何将我的类方法从我的 Array 类传递给我的 Driver 类,以便打印出来?
这是我的两段代码
主类:
import javax.swing.*;
import java.util.*;
public class Array
{
double sum = 0;
int max = 0;
int min = numbers[0];
double sd = 0;
int mode = 0;
int modeCount = 0;
public double average()
{
for(int i=0; i<numbers.length; i++)
{
sum = sum + numbers[i];
}
double average = sum / numbers.length;
return average;
}
public int max()
{
for(int i=0; i<numbers.length; i++)
{
if(numbers[i] > max)
{
max = numbers[i];
}
}
return max;
}
public int min()
{
for(int i=0; i<numbers.length; i++)
{
if(numbers[i] < min)
{
min = numbers[i];
}
}
return min;
}
public double standardDeviation()
{
for (int i=0; i<numbers.length;i++)
{
sum = sum + numbers[i];
double average = sum / numbers.length;
{
sd += ((numbers[i] - average)*(numbers[i] - average)) / (numbers.length - 1);
}
}
double standardDeviation = Math.sqrt(sd);
return standardDeviation;
}
public int mode()
{
for (int i = 0; i < numbers.length; ++i)
{
int count = 0;
for (int j = 0; j < numbers.length; ++j)
{
if (numbers[j] == numbers[i]) ++count;
}
if (count > modeCount) {
modeCount = count;
mode = numbers[i];
}
}
return mode;
}
}
驱动类:
import javax.swing.*;
import java.util.*;
public class ArrayTest
{
public static void main(String [] args)
{
int[] numbers;
numbers = new int [20];
Random rand = new Random(2621);
int maxRange = 65;
int minRange = 20;
for(int i=0; i<20; i++)
{
numbers[i] = rand.nextInt(maxRange - minRange + 1) + minRange;
}
Arrays.sort(numbers);
}
}