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

JAVA- This is the output that we are supposed to be getting- When I run my code,

ID: 3864498 • Letter: J

Question

JAVA- This is the output that we are supposed to be getting-

When I run my code, I am not getting the correct number of stars in the "score"column. Here is my code and my output:

import java.text.DecimalFormat;
import java.text.Format;
import java.util.Scanner;
public class test {
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 = calculateBMI(height, weight);
int score = 1;

if(heartRate == 0 ||respiratoryRate == 0 ||height == 0 ||weight == 0 ||systolicPressure == 0 || diastolicPressure== 0){
System.out.print("Invalid record!");
}
else{
System.out.printf("%10s", name);

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 + " ");
}
else{
System.out.printf("%11s", "!!" + systolicPressure + "/" + diastolicPressure + " ");
}

for(int i=0; i<=score; i++)
System.out.print("*");
}
System.out.println();
}
}
public static double calculateBMI(int height, int weight) {
double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254));
return (BMI);

}
}

OUTPUT:

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

Hi

I have fixed the issue 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
Smith 70 15 72 168 110 70
Smith 70 15 72 168 22.8 110/70   *****
Oracle 70 0 62 130 110 70
Invalid record
Switch 59 15 70 175 110 81
Switch !!59 15 70 175 25.1 !!110/81   ***
Trinity 100 11 68 140 110 70
Trinity 100 !!11 68 140 21.3 110/70   ****