Hi so I have to make program that generates two random numbers that puts it in a DataSet and gives the average and maximum. Here is what I have so far if anyone is willing to help me please. I was not explictly told to make two programs for it but that is how my teacher usually wants it to be set up so I made two. One has also the abtract information while the other is the generator, that has the main method to make it run.
Here is the abract class...
import java.util.Random;
public class Data
{
public Data(int n)
{
sum = 0;
count = 0;
maximum = 0;
number=n;
generator = new Random();
}
public void add(double x)
{
sum = sum + x;
if (count == 0 || maximum < x)
maximum = x;
count++;
}
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
public double getMaximum()
{
return maximum;
}
public int DataSet()
{
return 1 +generator.nextInt(number);
}
private double sum;
private double maximum;
private Random generator;
private int number;
private int count;
}
And here is the other piece for it that has the main method to make it run...
import java.util.Scanner;
public class RandomNumberAnalyzer
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Data data = new Data(0-1000);
final int TRIES = 2;
for (int i = 1; i <= TRIES; i++) {
int n = data.DataSet();
System.out.print(n + " ");
System.out.println();
System.out.println("Average = " + data.getAverage());
System.out.println("Maximum = " + data.getMaximum());
}
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire