Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Edit this java code, adding a new method to calculate BMI, and a separate boolea

ID: 3863937 • Letter: E

Question

Edit this java code, adding a new method to calculate BMI, and a separate boolean method to validate the input data. Also, format the output using PrintF to match the screenshot.

import java.util.Scanner;
public class test {
public static void main(String[] args){

Scanner scanner = new Scanner(System.in);
System.out.println("Name Heart Rate Resp Rate Height Weight BMI BP Score");
while(scanner.hasNext()){
String name = scanner.next();
int heartRate = scanner.nextInt();
int respiratoryRate = scanner.nextInt();
int height = scanner.nextInt();
int weight = scanner.nextInt();
int systolicPressure = scanner.nextInt();
int diastolicPressure = scanner.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.print(name+" ");

int score = 1;
if(heartRate>=60 && heartRate<=100){
score++;
System.out.print(heartRate+" ");
}
else{
System.out.print("!!"+heartRate+" ");
}
if(respiratoryRate>=12 && respiratoryRate<=18){
score++;
System.out.print(respiratoryRate+" ");
}
else{
System.out.print("!!"+respiratoryRate+" ");
}


{
System.out.print(name+" ");

int score = 1;
if(heartRate>=60 && heartRate<=100){
score++;
System.out.print(heartRate+" ");
}
else{
System.out.print("!!"+heartRate+" ");
}
if(respiratoryRate>=12 && respiratoryRate<=18){
score++;
System.out.print(respiratoryRate+" ");
}
else{
System.out.print("!!"+respiratoryRate+" ");
}
System.out.print(height+" ");
System.out.print(weight+" ");
System.out.print((BMI)+" ");
if((systolicPressure>=90 && systolicPressure<=120) && (diastolicPressure>=60 && diastolicPressure<=80)){
score++;
System.out.print(systolicPressure+"/"+diastolicPressure+" ");
}
else{
System.out.print("!!"+systolicPressure+"/" +diastolicPressure+" ");
}
for(int i=0;i<score;i++)
System.out.print("*");
}
System.out.println();
}
}
}

Sample Input/Output File inl 1.dat Smith 70 15 72 168 110 70 Oracle 70 0 62 130 110 70 Switch 59 15 70 175 110 81 Trinity 100 11 68 140 110 70 Morpheus 60 19 72 175 110 70 Cypher 80 18 82 176 110 85 Dozer 80 15 82 177 89 70 Dujour 70 15 62 0 110 70 Tank 80 15 70 174 110 70 Neo 101 15 72 175 110 59 Jones 110 19 69 220 110 55 Apoc 80 15 62 130 121 70 Execution Name Heart Rate Resp Rate Height Weight Smith 70 15 72 168 Invalid record 70 175 Switch 59 15 Trinity 100 11 68 140 175 Morpheus 60 19 72 Cypher 80 82 176 80 15 82 Dozer Invalid record Tank 80 15 70 174 175 101 15 72 Neo 110 19 69 220 Jones 130 80 15 62 Apoc BMI BP 22.8 10/70 25.1 110/81 21.3 10/70 23.7 10/70 18.4 10/85 18.5 89/70 110/70 25.0 23.7 10/59 32.5 10/55 23.8 121/70 Score

Explanation / Answer

Solution: See the updated code

-------------------------------------------------------------

import java.text.DecimalFormat;
import java.text.Format;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("Name Heart Rate Resp Rate Height Weight BMI BP Score");
       while (scanner.hasNext()) {
           String name = scanner.next();
           if (name.equalsIgnoreCase("exit"))
               break;
           int heartRate = scanner.nextInt();
           int respiratoryRate = scanner.nextInt();
           int height = scanner.nextInt();
           int weight = scanner.nextInt();
           int systolicPressure = scanner.nextInt();
           int diastolicPressure = scanner.nextInt();
           double BMI = calculateBMI(height, weight);
           if (isInputDataValid(heartRate, respiratoryRate, height, weight, systolicPressure, diastolicPressure)) {
               System.out.println("valid");
               System.out.print(name + " ");
               int score = 1;
               if (heartRate >= 60 && heartRate <= 100) {
                   score++;
                   System.out.print(heartRate + " ");
               } else {
                   System.out.print("!!" + heartRate + " ");
               }
               if (respiratoryRate >= 12 && respiratoryRate <= 18) {
                   score++;
                   System.out.print(respiratoryRate + " ");
               } else {
                   System.out.print("!!" + respiratoryRate + " ");
               }
               System.out.print(height + " ");
               System.out.print(weight + " ");
               System.out.print((BMI) + " ");
               if ((systolicPressure >= 90 && systolicPressure <= 120)
                       && (diastolicPressure >= 60 && diastolicPressure <= 80)) {
                   score++;
                   System.out.print(systolicPressure + "/" + diastolicPressure + " ");
               } else {
                   System.out.print("!!" + systolicPressure + "/" + diastolicPressure + " ");
               }
               for (int i = 0; i < score; i++)
                   System.out.print("*");
           }
           System.out.println();
       }
       scanner.close();
   }

   /**
   * @param heartRate
   * @param respiratoryRate
   * @param height
   * @param weight
   * @param systolicPressure
   * @param diastolicPressure
   * @return
   */
   static boolean isInputDataValid(int heartRate, int respiratoryRate, int height, int weight, int systolicPressure,
           int diastolicPressure) {
       if (heartRate == 0 || respiratoryRate == 0 || height == 0 || weight == 0 || systolicPressure == 0
               || diastolicPressure == 0)
           return false;
       else
           return true;
   }

   /**
   * @param height
   * @param weight
   * @return
   */
   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));
   }
}

------------------------------------------------

Note: Output is already formatted using tabs.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote