Question

In: Computer Science

JAVA- How do I edit the following code as minimally as possible to add this method...

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();
}
}
}

Solutions

Expert Solution

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
i
f(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   **


Related Solutions

In Java please: 1) Part 1: Edit the getTileList method (add length method too) Dear Developer,...
In Java please: 1) Part 1: Edit the getTileList method (add length method too) Dear Developer, It seems an overzealous programmer tried to create a Fibonacci slider puzzle from our old code. This brought up the fact there is a data integrity issue in our SlidingSquarePuzzle class. It makes sense because the class’s data only consists of an int[]. Since, you are new this is a good opportunity to get your feet wet. I want you to change the offending...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION? Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999)....
In Java I need a Flowchart and Code. Write the following method that tests whether the...
In Java I need a Flowchart and Code. Write the following method that tests whether the array has four consecutive numbers with the same value: public static boolean isConsecutiveFour(int[] values) Write a test program that prompts the user to enter a series of integers and displays it if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size—i.e., the number of values in the series.
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory. The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z')....
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
I need to understand how I would edit this code without changing anything, only adding. I...
I need to understand how I would edit this code without changing anything, only adding. I am not looking for the exact answer, just information on how I would use the given code to complete some of the todo statements. Thank you! // include this header file so we can use `printf()` #include <stdio.h> // every C program must implement the `main()` function int main(int argc, char *argv[]) {    //TODO: check for enough arguments       // save the...
How do i get the code below to add an element before another without making the...
How do i get the code below to add an element before another without making the array off one element? (javascript) public void addBefore(double element) { itemCount++; double data[] = new double[this.data.length]; if(currentIndex <= itemCount) { if(currentIndex != 0) { for(int index = currentIndex; index >= itemCount; index ++) { data[index] = this.data[index]; } currentIndex--; data[currentIndex] = element; } if(currentIndex == 0) { data[0] = element; currentIndex = 0; } } }
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT