Using Java Write a program that calculates and displays a person’s body mass ind
ID: 3759520 • Letter: U
Question
Using Java
Write a program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person is overweight or underweight for his or her height.
A person’s BMI is calculated with the following formula: BMI = weight X 703/height^2
Where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A person’s weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight. Use an input data file to hold a couple of people’s names, weights, and heights. The program should produce an output file that has each person’s name and then indicate whether the person’s BMI is optimal, underweight, or overweight.
Example Input file:
K. Dunst
250
60
A. Jolie
100
67
H. Berry
122
68
Example output file:
K. Dunst Overweight
A. Jolie Underweight
H. Berry Optimal weight
Explanation / Answer
/* package whatever; // don't place package name! */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw=null;
try {
String sCurrentLine,name,height,weight,bmi;
br = new BufferedReader(new FileReader("testing.txt"));
bw = new BufferedWriter(new FileWriter("Output.txt"));
while ((sCurrentLine = br.readLine()) != null) {
for(int i=0;i<3;i++){
if(i==0){
name=sCurrentLine;
}else if(i==1){
weight=Double.parseDouble(br.readLine());
}else{
height=Double.parseDouble(br.readLine());
}
}
bmi=(weight*703);
bmi=bmi/(height*height);
if(bmi<18.5){
bw.write(name);
bw.write(" ");
bw.write)("UnderWeight");
bw.newLine();
}else if(bmi>25){
bw.write(name);
bw.write(" ");
bw.write)("OverWeight");
bw.newLine();
}else{
bw.write(name);
bw.write(" ");
bw.write)("OptimalWeight");
bw.newLine();
}
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.