LittleRedHat
Instructor
I am trying to write a program that, following user input (user determines no of samples),
1. converts the input to kilograms
2. calculates the average
3. identifies the range i.e. highest and lowest input
4. makes a further calculation based on both the average and the range
The input, conversion and average are working correctly, but I'm stumped as to how to identify the highest and lowest inputs.
Ideally I would like to include this in the AverageClass, but would settle for a seperate RangeClass that works!
I would be very grateful for any help or guidance.
WTA
LRH
/*
Java Class for Weight (Stones/Pounds:Kilograms) Converter
*/
public class KiloConvClass{
private double kilograms;
private int stones, pounds;
public double ConvToKilo(int stns, int lbs){
return (double)(stns*14+lbs)/2.2;
}
}
/*
Java Class for calculating averages
*/
public class AverageClass{
private double sum;
private int count;
public AverageClass(){
sum=0;
count=0;
}
public void InputValue(double number){
sum+=number;
count++;
}
public double Average(){
return sum / count;
}
}
/*
Java program to check samples are
maintained within a specified range,
using KiloConv class, Average class,
*/
public class SampleCheck{
public static void main(String arg[]){
double kilograms;
int stns, lbs, i, numToSample;
KiloConvClass kcc = new KiloConvClass();
AverageClass ac = new AverageClass();
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);
ac.InputValue (kilograms);
System.out.println("\nAverage="+ac.Average());
}
}
}
1. converts the input to kilograms
2. calculates the average
3. identifies the range i.e. highest and lowest input
4. makes a further calculation based on both the average and the range
The input, conversion and average are working correctly, but I'm stumped as to how to identify the highest and lowest inputs.
Ideally I would like to include this in the AverageClass, but would settle for a seperate RangeClass that works!
I would be very grateful for any help or guidance.
WTA
LRH
/*
Java Class for Weight (Stones/Pounds:Kilograms) Converter
*/
public class KiloConvClass{
private double kilograms;
private int stones, pounds;
public double ConvToKilo(int stns, int lbs){
return (double)(stns*14+lbs)/2.2;
}
}
/*
Java Class for calculating averages
*/
public class AverageClass{
private double sum;
private int count;
public AverageClass(){
sum=0;
count=0;
}
public void InputValue(double number){
sum+=number;
count++;
}
public double Average(){
return sum / count;
}
}
/*
Java program to check samples are
maintained within a specified range,
using KiloConv class, Average class,
*/
public class SampleCheck{
public static void main(String arg[]){
double kilograms;
int stns, lbs, i, numToSample;
KiloConvClass kcc = new KiloConvClass();
AverageClass ac = new AverageClass();
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);
ac.InputValue (kilograms);
System.out.println("\nAverage="+ac.Average());
}
}
}