In: Computer Science
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI?
BMI Method:
public static double calculateBMI(int height, int weight)
{
double BMI = (((double) weight) * 0.453592d) / ((((double) height)
* 0.0254) * (((double) height) * 0.0254));
Format f = new DecimalFormat("##.######");
return (f.format(BMI));
}
Code:
import java.text.DecimalFormat;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
DecimalFormat f = new DecimalFormat("##.0");
Scanner reader = new Scanner(System.in);
System.out.printf("%10s %15s %12s %8s %8s %8s %9s %8s %n", "Name", "Heart Rate", "Resp Rate", "Height", "Weight", "BMI", "BP", "Score");
while(reader.hasNext()){
String name = reader.next();
int heartRate = reader.nextInt();
int respiratoryRate = reader.nextInt();
int height = reader.nextInt();
int weight = reader.nextInt();
int systolicPressure = reader.nextInt();
int diastolicPressure = reader.nextInt();
double BMI =
(((double)weight)*0.453592d)/((((double)height)*0.0254)*(((double)height)*0.0254));
if(heartRate == 0 ||respiratoryRate == 0 ||height == 0 ||weight
== 0 ||systolicPressure == 0 || diastolicPressure== 0){
System.out.print("Invalid record!");
}
else{
System.out.printf("%10s", name);
int score = 1;
if(heartRate>=60 && heartRate<=100){
score++;
System.out.printf("%16s", heartRate);
}
else{
System.out.printf("%16s", "!!" +heartRate);
}
if(respiratoryRate>=12 &&
respiratoryRate<=18){
score++;
System.out.printf("%13s", respiratoryRate);
}
else{
System.out.printf("%13s", "!!" +respiratoryRate);
}
System.out.printf("%9s", height);
System.out.printf("%9s", weight);
if(BMI>=18.5 && BMI <=25.0){
System.out.printf("%9s", f.format(BMI));
}
else{
System.out.printf("%9s", "!!" + f.format(BMI));
}
if((systolicPressure>=90 && systolicPressure<=120)
&& (diastolicPressure>=60 &&
diastolicPressure<=80)){
score++;
System.out.printf("%11s", systolicPressure + "/" +
diastolicPressure + "\t");
}
else{
System.out.printf("%11s", "!!" + systolicPressure + "/" +
diastolicPressure + "\t");
}
for(int i=0;i<score;i++)
System.out.print("*");
}
System.out.println();
}
}
}
Hi
I have updated the code and highlighted the code changes below.
test2.java
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
DecimalFormat f = new DecimalFormat("##.0");
Scanner reader = new Scanner(System.in);
System.out.printf("%10s %15s %12s %8s %8s %8s %8s %9s %n", "Name",
"Heart Rate", "Resp Rate", "Height", "Weight", "BMI", "BP",
"Score");
while(reader.hasNext()){
String name = reader.next();
int heartRate = reader.nextInt();
int respiratoryRate = reader.nextInt();
int height = reader.nextInt();
int weight = reader.nextInt();
int systolicPressure = reader.nextInt();
int diastolicPressure = reader.nextInt();
double BMI = calculateBMI(height, weight);
//Replace this line with a new method
if(heartRate == 0 ||respiratoryRate == 0 ||height == 0
||weight == 0 ||systolicPressure == 0 || diastolicPressure==
0)
{
System.out.print("Invalid record");
}
else
{
System.out.printf("%10s", name);
int score = 1;
if(heartRate>=60 && heartRate<=100){
score++;
System.out.printf("%16s", heartRate);
}
else{
System.out.printf("%16s", "!!" +heartRate);
}
if(respiratoryRate>=12 && respiratoryRate<=18){
score++;
System.out.printf("%13s", respiratoryRate);
}
else{
System.out.printf("%13s", "!!" +respiratoryRate);
}
System.out.printf("%9s", height);
System.out.printf("%9s", weight);
System.out.printf("%9s", f.format(BMI));
if((systolicPressure>=90 && systolicPressure<=120)
&& (diastolicPressure>=60 &&
diastolicPressure<=80)){
score++;
System.out.printf("%11s", systolicPressure + "/" +
diastolicPressure + "\t");
}
else{
System.out.printf("%11s", "!!" + systolicPressure + "/" +
diastolicPressure + "\t");
}
for(int i=0;i<score;i++)
System.out.print("*");
}
System.out.println();
}
}
static double calculateBMI(int height, int weight) {
double BMI = (((double) weight) * 0.453592d) / ((((double) height)
* 0.0254) * (((double) height) * 0.0254));
Format f = new DecimalFormat("##.##");
return Double.parseDouble(f.format(BMI));
}
}
Output:
Name Heart Rate Resp Rate Height Weight BMI BP Score
Suresh
72
75
6
80
4
6
Suresh 72 !!75 6 80 1562.4 !!4/6 **