Could someone please show me how to include height, i am having trouble with putting Height in once the user has input the weight, i need Height to go in after the user has entered weight. i need the height to be from 0-200 metres, can anybody help?
import javax.swing.JOptionPane;
publicfinalclass Statistic {
privateint totalWeight;
publicstaticvoid main(String[] args) {
new Statistic().run();
}
publicvoid run() {
int groupSize = getNumberOfDietersInput();
Dieter[] dieters = new Dieter[groupSize];
for (int dieterNumber = 0; dieterNumber < groupSize; dieterNumber++) {
dieters[dieterNumber] = getNewDieter(dieterNumber + 1);
}
if (dieters.length > 0) {
DieterGroup summary = new DieterGroup(dieters);
showSummaryInformation(summary);
}
JOptionPane.showMessageDialog(
null,
"The Program Is Closing Down",
"Exit Message",
JOptionPane.INFORMATION_MESSAGE);
}
privateint getNumberOfDietersInput() {
int numberOfDieters = -1;
while (numberOfDieters < 0
|| numberOfDieters > DieterGroup.MAX_GROUP_SIZE) {
String input =
JOptionPane.showInputDialog(
"Enter the Number of Dieters in the Class (0 - "
+ DieterGroup.MAX_GROUP_SIZE
+ "): ");
try {
numberOfDieters = Integer.parseInt(input);
} catch (NumberFormatException nfe) {
System.out.println("User entered invalid group size: " + input);
}
if (numberOfDieters < 0 || numberOfDieters > DieterGroup.MAX_GROUP_SIZE) {
JOptionPane.showMessageDialog(
null,
"Must be between 0 and " + DieterGroup.MAX_GROUP_SIZE,
"Input Error",
JOptionPane.ERROR_MESSAGE);
}
}
return numberOfDieters;
}
private Dieter getNewDieter(int dieterNumber) {
int weight = getDieterWeightInput(dieterNumber);
Dieter dieter = new Dieter(dieterNumber, weight);
return dieter;
}
privateint getDieterWeightInput(int dieterNumber) {
int weight = -1;
while (weight < 0 || weight > Dieter.MAX_WEIGHT_KILOS) {
String input =
JOptionPane.showInputDialog(
"Enter weight for Dieter #"
+ dieterNumber
+ " (0 - "
+ Dieter.MAX_WEIGHT_KILOS
+ "kg): ");
try {
weight = Integer.parseInt(input);
} catch (NumberFormatException nfe) {
System.out.println("User entered invalid weight: " + input);
}
if (weight < 0 || weight > Dieter.MAX_WEIGHT_KILOS) {
JOptionPane.showMessageDialog(
null,
"Must be between 0 and " + Dieter.MAX_WEIGHT_KILOS,
"Input Error",
JOptionPane.ERROR_MESSAGE);
}
}
return weight;
}
privatevoid showSummaryInformation(DieterGroup summary) {
StringBuffer output = new StringBuffer();
output.append(
"Total number of dieters in class: " + summary.getGroupSize());
output.append(
"\nTotal accumulated weight of dieters: "
+ summary.getTotalWeight()
+ "kg");
output.append(
"\nAverage weight of dieters: "
+ summary.getAverageWeight()
+ " kg");
JOptionPane.showMessageDialog(
null,
output,
"Summary",
JOptionPane.INFORMATION_MESSAGE);
}
}
class Dieter {
privateint id;
privateint weight;
publicstaticfinalint MAX_WEIGHT_KILOS = 200;
public Dieter(int id, int weight) {
this.id = id;
if (weight < 0 || weight > MAX_WEIGHT_KILOS) {
thrownew IllegalArgumentException(
"Weight must be between 0 and "
+ MAX_WEIGHT_KILOS
+ " kilograms, inclusive.");
}
this.weight = weight;
}
publicint getWeight() {
return weight;
}
}
class DieterGroup {
private Dieter[] dieters;
privateint totalWeight;
privatedouble averageWeight;
publicstaticfinalint MAX_GROUP_SIZE = 25;
public DieterGroup(Dieter[] dieters) {
this.dieters = dieters;
if (dieters.length > MAX_GROUP_SIZE) {
thrownew IllegalArgumentException(
"Dieter group size cannot be greater than "
+ MAX_GROUP_SIZE
+ " members.");
}
for (int j = 0; j < dieters.length; j++) {
totalWeight += dieters[j].getWeight();
}
averageWeight = (double) totalWeight / dieters.length;
}
publicint getGroupSize() {
return dieters.length;
}
publicint getTotalWeight() {
return totalWeight;
}
publicdouble getAverageWeight() {
return averageWeight;
}
}