
Patrick B. answered 05/23/20
Math and computer tutor/teacher
import java.io.*;
class AverageWeight
{
private int numWeights;
private double cumulativeWeight;
private Console console;
AverageWeight()
{
numWeights=0;
cumulativeWeight=0;
console = System.console();
} //constructor
private void Input()
{
String more="y";
double weight;
String inbuff;
cumulativeWeight=0;
numWeights=0;
while (more.equals("y") || more.equals("Y"))
{
weight=-1;
while (weight<0)
{
System.out.println(" Item number " + (numWeights+1) );
System.out.print(" Please input the kg weight :>");
inbuff = console.readLine();
weight = Double.parseDouble(inbuff);
}
cumulativeWeight += weight;
numWeights++;
do
{
System.out.print(" more??? (y/n) :>");
more = console.readLine();
more.trim();
} while (!(more.equals("N") || more.equals("n") || more.equals("Y") || more.equals("y")));
} //while
} //Input
void Go()
{
Input();
if (numWeights>0)
{
System.out.println(" Average weight = " + (cumulativeWeight)/numWeights + " kg");
}
else
{
System.out.println(" Average weight = 0 kg");
}
} //Go
public static void main(String args[])
{
new AverageWeight().Go();
}
} //AverageWeight