The Artima Developer Community
Sponsored Link

Java Answers Forum
Weight and Height Problem???

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ernie Douglas

Posts: 18
Nickname: y2j
Registered: Nov, 2002

Weight and Height Problem??? Posted: Nov 17, 2003 9:40 AM
Reply to this message Reply
Advertisement
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;
    
public final class Statistic {
    
    private int totalWeight;
    
    public static void main(String[] args) {
        new Statistic().run();
    }
    
    public void 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);
    }
    
    private int 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;
    }
    
    private int 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;
    }
    
    private void 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 {
    
    private int id;
    private int weight;
    public static final int MAX_WEIGHT_KILOS = 200;
    
    public Dieter(int id, int weight) {
        this.id = id;
 
        if (weight < 0 || weight > MAX_WEIGHT_KILOS) {
            throw new IllegalArgumentException(
                "Weight must be between 0 and "
                    + MAX_WEIGHT_KILOS
                    + " kilograms, inclusive.");
        }
        this.weight = weight;
    }
    
    public int getWeight() {
        return weight;
    }
}
    
class DieterGroup {
    
    private Dieter[] dieters;
    private int totalWeight;
    private double averageWeight;
    
    public static final int MAX_GROUP_SIZE = 25;
    
    public DieterGroup(Dieter[] dieters) {
    
        this.dieters = dieters;
    
        if (dieters.length > MAX_GROUP_SIZE) {
            throw new 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;
    }
    
    public int getGroupSize() {
        return dieters.length;
    }
    
    public int getTotalWeight() {
        return totalWeight;
    }   
    
    public double getAverageWeight() {
        return averageWeight;
    }
    
}

Topic: Anyone see the error? Previous Topic   Next Topic Topic: Help with Arrays

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use