Solved
Restrict to 2 decimal places
Posted on 2002-04-03
I would appreciate advice as to how to limit the return from a calculation to 2 decimal places.
WTA
LRH
/*
Java Class for calculating and comparing the range and average of figures input by the user
*/
public class AverageRangeClass{
private double sum, max, min;
private int count;
public class AverageRangeClass(){
sum=0;
count=0;
max=0;
min=0;
}
public void InputValue(double number){
sum+=number;
count++;
if(count==1){
max=number;
min=number;
}
else if(number > max)
max=number;
else if(number < min)
min=number;
}
public double rangeToAverage(){
return ((max-min)/(sum/count));
}
}
/*
Java program to check samples are
maintained within a specified range,
using KiloConv class, AverageRange class
*/
public class SampleCheck{
public static void main(String arg[]){
double kilograms;
int stns, lbs, i, numToSample;
KiloConvClass kcc = new KiloConvClass();
AverageRangeClass arc = new AverageRangeClass();
SampleRulesClass src = new SampleRulesClass();
System.out.println("How many samples are there?\n");
numToSample=Input.readInt();
for(i=1; i<=numToSample; i++){
System.out.print("Please enter stones weight:");
stns = Input.readInt();
System.out.print("Please enter pounds weight:");
lbs = Input.readInt();
kilograms = kcc.ConvToKilo(stns,lbs);
arc.InputValue (kilograms);
System.out.println("\nRange to Average = "+arc.rangeToAverage());
}
}
}