/**
* Programming Assignment #1 (MEANS)
* Anthony Yates
* For CSCE 1040 (Spring 2008)
*/
// import java.util.*; // for String tokenizer.
// import java.io.*; // File I/O package
class Means {
/* This class defines data and methods for computing means, using the
* arithmetic mean and the geometric mean.
*/
private float meanSum;
private float meanProduct;
private int meanItems;
public Means() {
meanSum = 0;
meanProduct = 1;
meanItems = 0;
} // end Means()
public float getMeanSum() {
return (meanSum);
} // end getMeanSum()
public float getMeanProduct() {
return (meanProduct);
} // end getMeanProduct()
public int getMeanItems() {
return (meanItems);
} // end getMeanItems()
public float getArithmeticMean(){
return( meanSum / meanItems );
} // end getArithmeticMean()
public float getGeometricMean(){
return (float)( Math.pow( meanProduct,
( 1.0 / meanItems ) ) );
} // end getGeometricMean()
public void addMeanItem(float x){
meanSum += x;
meanProduct *= x;
++meanItems;
} // end addMeanItem()
} // end class Means()
public class yatesProg1 {
static Means MyMeans = new Means();
final static int NumberForComputation = 10;
static int traceLevel = 0;
static int NumComp = NumberForComputation;
public static void main(String[] args)
{
if (args.length >= 1) {
NumComp = Integer.parseInt(args[0]);
if (args.length == 2) {
traceLevel = Integer.parseInt(args[1]);
}
}
if (traceLevel > 0) {
System.out.println("\nNumber of Items is " + MyMeans.getMeanItems());
System.out.println("The meanSum is " + MyMeans.getMeanSum());
System.out.println("The meanProduct is " + MyMeans.getMeanProduct()
+ "\n");
}
// Compute means for the first NumComp integers
for (int i = 1; i <= NumComp; i++) {
MyMeans.addMeanItem(i);
if (traceLevel > 0) {
System.out.println("AM = " + MyMeans.getArithmeticMean() + ", GM = "
+ MyMeans.getGeometricMean());
}
if (MyMeans.getGeometricMean() > MyMeans.getArithmeticMean()) {
System.out.println("The Geometric Mean is greater than the " +
"Arithmetic Mean.");
}
}
System.out.println("Means of the first " + MyMeans.getMeanItems()
+ " integers" );
System.out.println("The Arithmetic Mean is " + MyMeans.getArithmeticMean()
+ ", and the Geometric Mean is " + MyMeans.getGeometricMean() + ".\n");
// Compute means for first NumComp values of (26*i + 7) mod 31,
// for i=1,2, ...
// Student to fill this in.
// Copy and modify the loop and System.out statements above.
for (int i = 1; i <= NumComp; i++) {
MyMeans.addMeanItem(i);
if (traceLevel > 0) {
System.out.println("AM = " + MyMeans.getArithmeticMean() + ", GM = "
+ MyMeans.getGeometricMean());
}
if (MyMeans.getGeometricMean() > MyMeans.getArithmeticMean()) {
System.out.println("The Geometric Mean is greater than the " +
"Arithmetic Mean.");
}
}
System.out.println("Means of the first " + MyMeans.getMeanItems()
+ " integers" );
System.out.println("The Arithmetic Mean is " + MyMeans.getArithmeticMean()
+ ", and the Geometric Mean is " + MyMeans.getGeometricMean() + ".\n");
// Compute means for NumComp random integers from 1 to 100
// Student to fill this in.
for (int i = 1; i <= NumComp; i++) {
MyMeans.addMeanItem(i);
if (traceLevel > 0) {
System.out.println("AM = " + MyMeans.getArithmeticMean() + ", GM = "
+ MyMeans.getGeometricMean());
}
if (MyMeans.getGeometricMean() > MyMeans.getArithmeticMean()) {
System.out.println("The Geometric Mean is greater than the " +
"Arithmetic Mean.");
}
}
System.out.println("Means of the first " + MyMeans.getMeanItems()
+ " integers" );
System.out.println("The Arithmetic Mean is " + MyMeans.getArithmeticMean()
+ ", and the Geometric Mean is " + MyMeans.getGeometricMean() + ".\n");
// Compute means for the first NumComp values of the sequence 2,
// 1.5, 1.3333, 1.25, .... 1 1/k ...
// Student to fill this in.
for (int i = 1; i <= NumComp; i++) {
MyMeans.addMeanItem(i);
if (traceLevel > 0) {
System.out.println("AM = " + MyMeans.getArithmeticMean() + ", GM = "
+ MyMeans.getGeometricMean());
}
if (MyMeans.getGeometricMean() > MyMeans.getArithmeticMean()) {
System.out.println("The Geometric Mean is greater than the " +
"Arithmetic Mean.");
}
}
System.out.println("Means of the first " + MyMeans.getMeanItems()
+ " integers" );
System.out.println("The Arithmetic Mean is " + MyMeans.getArithmeticMean()
+ ", and the Geometric Mean is " + MyMeans.getGeometricMean() + ".\n");
} // end main()
} // end class yatesProg1