import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; interface Patient { public String displayBMICategory(double bmi); public String displayInsuranceCategory(double bmi); } public class BMI implements Patient { public static void main(String[] args) { /* * local variable for saving the patient information */ double weight = 0; String birthDate = "", name = ""; int height = 0; Scanner scan = new Scanner(System.in); String cont = ""; Queue<String> patients = new LinkedList<>(); // do while loop for keep running program till user not entered q do { System.out.print("Press Y for continue (Press q for exit!) "); cont = scan.nextLine(); if (cont.equalsIgnoreCase("q")) { System.out.println("Thank you for using BMI calculator"); break; } System.out.print("Please enter patient name: "); /* * try catch block for valid data for weight catch block for null value catch * block for divide by zero exception if height is zero finally block */ try { name = scan.nextLine(); System.out.print("Please enter the weight of the patient in kg: "); weight = scan.nextDouble(); scan.nextLine(); System.out.print("Please entet the birth date of patient: "); birthDate = scan.nextLine(); System.out.print("Please enter the height of patient in cm: "); height = scan.nextInt(); scan.nextLine(); } catch (NumberFormatException e) { System.out.println("Please enter valid data!"); } catch (ArithmeticException e) { System.out.println("Height can not be zero!"); } catch (NullPointerException e) { System.out.println("Name can not be blank!"); } finally { System.out.println("Welcome to BMI calculator!!"); } // calculate height in meter double heightInMeter = (double) height / 100; // calculate BMI double bmi = weight / (heightInMeter * heightInMeter); /* * Print the patient name and date of birth print the patient BMI category print * the patient insurance payment category */ Patient patient = new BMI(); patients.add(name); System.out.println("Patient " + name + " and Date of birth is - " + birthDate + "\nPatient BMI category is - " + patient.displayBMICategory(bmi)); System.out.println("And Patient insurance payment category is - " + patient.displayInsuranceCategory(bmi)); } while (!cont.equalsIgnoreCase("q")); System.out.println("WaitingQueue : " + patients); String patientName = patients.remove(); System.out.println("Removed from WaitingQueue : " + patientName + " | New WaitingQueue : " + patients); scan.close(); } @Override public String displayBMICategory(double bmi) { String bmiCategory = ""; if (bmi <= 18.5) { bmiCategory = "underweight"; } else if (bmi > 18.5 && bmi <= 24.9) { bmiCategory = "Normal"; } else if (bmi > 25 && bmi <= 29.9) { bmiCategory = "Overweight"; } else if (bmi > 29.9) { bmiCategory = "Obesity"; } return bmiCategory; } @Override public String displayInsuranceCategory(double bmi) { /* * set Insurance Payment category as BMI category */ String insuranceCategory = ""; if (bmi <= 18.5) { insuranceCategory = "Low"; } else if (bmi > 18.5 && bmi <= 24.9) { insuranceCategory = "Low"; } else if (bmi > 25 && bmi <= 29.9) { insuranceCategory = "High"; } else if (bmi > 29.9) { insuranceCategory = "Highest"; } return insuranceCategory; } }
Write, Run & Share Jshell code online using OneCompiler's Jshell online compiler for free. It's one of the robust, feature-rich online compilers for Jshell language, running the Jshell version 17. Getting started with the OneCompiler's Jshell editor is easy and fast. The editor shows sample boilerplate code when you choose language as Jshell and start coding.
Jshell is Java’s first official REPL (READ-EVAL-PRINT-LOOP) tool which was introduced in JDK 9 as part of Java Enhancement Proposal. It is suitable to learn the language and also to understand unfamiliar code. With Jshell, you can test the functionality in isolation of a class.
In short, Jshell creates a simple and easy programming environment in the command line which can take input from user, read it and then prints the result.
When you evaluate any valid java expression, the result will be stored in the system defined variables.
You can also create your own variables
// datatype variable name = value;
int age = 16; // example
By default Jshell creates a new VM to run the code which makes the System.in unavialble to use. OneCompiler has a workaround to this by adding --execution local
param. User need to mention this in comments to make use of this option. Following is an example program to demonstrate this
//--execution local
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String inp = input.next();
System.out.println("Hello, " + inp);
When ever you want to perform a set of operations based on a condition If-Else is used.
Syntax:
if(condition) {
// code when condition true
} else {
// code when condition false
}
Example:
int i = 3
if( i%2 == 0 ) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.
Syntax:
for( Initialization; Condition; Increment/decrement ){
//code
}
Example:
for(int i = 1; i <= 10; i++ ){
Systen.out.println(i);
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition){
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);