JAVA- How do I edit my code to replace the line to calculate BMI with an entire
ID: 3864428 • Letter: J
Question
JAVA- How do I edit my code to replace the line to calculate BMI with an entire a new method to calculate BMI similar to this---
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));
}
}
Here is my 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 %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 = (((double)weight)*0.453592d)/((((double)height)*0.0254)*(((double)height)*0.0254)); //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 + " ");
}
else{
System.out.printf("%11s", "!!" + systolicPressure + "/" + diastolicPressure + " ");
}
for(int i=0;i<score;i++)
System.out.print("*");
}
System.out.println();
}
}
}
Explanation / Answer
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 + " ");
}
else{
System.out.printf("%11s", "!!" + systolicPressure + "/" + diastolicPressure + " ");
}
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 3 4 5 80 4 120
Suresh !!3 !!4 5 80 2249.8 !!4/120 *
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.