Berikut adalah program untuk mengira BMI dan menyatakan status yang sepadan. Tukarkan kepada kaedah OOP.
import java.util.Scanner;
public class ComputeAndInterpretBMI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter weight in pounds: ");
double weight = input.nextDouble();
System.out.print("Enter height in inches: ");
double height = input.nextDouble();
final double KILOGRAMS_PER_POUND = 0.45359237;
final double METERS_PER_INCH = 0.0254;
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
double heightInMeters = height * METERS_PER_INCH;
double bmi = weightInKilograms /
(heightInMeters * heightInMeters);
System.out.println("BMI is " + bmi);
if (bmi < 18.5)
System.out.println("Underweight");
else if (bmi < 25)
System.out.println("Normal");
else if (bmi < 30)
System.out.println("Overweight");
else
System.out.println("Obese");
}
}
--------------------------Cara 1----------------------------------------
import java.util.Scanner;
class BMI{
double weight,height;
void getinput(){
Scanner input = new Scanner(System.in);
// Prompt the user to enter weight in pounds
System.out.print("Enter weight in pounds: ");
weight = input.nextDouble();
// Prompt the user to enter height in inches
System.out.print("Enter height in inches: ");
height = input.nextDouble();
}
double tukarpoundtokilo(){
final double KILOGRAMS_PER_POUND = 0.45359237; // Constant
return ( weight * KILOGRAMS_PER_POUND);
}
double tukarinchtometer(){
final double METERS_PER_INCH = 0.0254; // Constant
return (height * METERS_PER_INCH);
}
void printoutput(){
double bmi;
bmi= tukarpoundtokilo() / (tukarinchtometer() * tukarinchtometer());
// Display result
System.out.println("BMI is " + bmi);
if (bmi < 18.5)
System.out.println("Underweight");
else if (bmi < 25)
System.out.println("Normal");
else if (bmi < 30)
System.out.println("Overweight");
else
System.out.println("Obese");
}
}
public class carapertama {
public static void main(String[] args) {
BMI yourBMI = new BMI();
yourBMI.getinput();
yourBMI.printoutput();
}
}
No comments:
Post a Comment