/** * Assignment 2, COMP268 Class: Person.java * * @description A class used to shame a person by calculating a BMI which has no value whatsoever. * @author: mo khan Student ID: 3431709 * @date Jul 19, 2019 * @version 1.0 */ package Q7; import java.util.*; public class Person { private double bmi; private double height; private double weight; private String name; /** * Create an instance of a person and calculates their BMI. * * @param name the name of the person * @param weight the weight of the person * @param height the height of the person */ public Person(String name, double weight, double height) { this.name = name; this.weight = weight; this.height = height; this.updateBMI(); } /** * Returns the category the person is categorized as based on BMI. * * @return A category of "Underweight", "Normal", "Overweight" or "Obese" */ public String getCategory() { return this.getCategory(this.bmi); } /** * Returns the category for the BMI. * * @param bmi the BMI to determine a category for. * @return A category of "Underweight", "Normal", "Overweight" or "Obese" */ public String getCategory(double bmi) { if (bmi < 18.5) return "Underweight"; else if (bmi < 25.0) return "Normal"; else if (bmi < 30.0) return "Overweight"; return "Obese"; } /** * Returns the persons name * * @return the persons name */ public String getName() { return this.name; } /** * Returns the persons BMI * * @return the persons BMI */ public double getBMI() { return this.bmi; } /** * Returns the persons height * * @return the persons height */ public double getHeight() { return this.height; } /** * Returns the persons weight * * @return the persons weight */ public double getWeight() { return this.weight; } /** * Change the BMI * * @param bmi the new BMI */ private void setBMI(double bmi) { this.bmi = bmi; } /** * Change the Height * * @param height the new height */ public void setHeight(double height) { this.height = height; this.updateBMI(); } /** * Change the name * * @param name the new name */ public void setName(String name) { this.name = name; } /** * Change the weight * * @param weight the new weight */ public void setWeight(double weight) { this.weight = weight; this.updateBMI(); } private void updateBMI() { this.setBMI((this.weight * 703) / Math.pow(height, 2)); } /** The entry point to the console application. */ public static void main(String[] args) { ArrayList people = new ArrayList(); people.add(new Person("Andrew", 125.5, 55.1)); people.add(new Person("Boyd", 150.0, 67.0)); people.add(new Person("Cathy", 135.0, 72.3)); people.add(new Person("Donna", 190.0, 64.0)); System.out.println(String.format("%-20s Weight Height BMI Category", "Name")); System.out.println("-----------------------------------------------"); for (Person person : people) { System.out.println( String.format( "%-20s %+5.1f %+6.1f %+3.0f %s", person.getName(), person.getWeight(), person.getHeight(), person.getBMI(), person.getCategory())); } } }