// A Swing applet import javax.swing.*; public class BodyMassIndexUK extends JApplet { public void init() { String Height; String Weight; String AddOn; double h=2, w=80; Double Bmi; int index,rep; boolean error=true; boolean again=true; while (again==true) { while (error==true) { error=false; Height = JOptionPane.showInputDialog("Enter your height\nin meters"); while (Height==null||Height.length()==0) { Height = JOptionPane.showInputDialog("Enter your height\nin meters"); } Weight = JOptionPane.showInputDialog("Enter your weight\nin kilograms"); while (Weight==null||Weight.length()==0) { Weight = JOptionPane.showInputDialog("Enter your weight\nin kilograms"); } try { h = Double.parseDouble(Height); w = Double.parseDouble(Weight); if (h>2.5||h<1.3||w>200||w<20) { error=true; JOptionPane.showMessageDialog(null,"Are these the right numbers?\nTry again","INPUT?",JOptionPane.ERROR_MESSAGE); } } catch(Exception e) { JOptionPane.showMessageDialog(null,"Input error", "ERROR",JOptionPane.ERROR_MESSAGE); error=true; } } // end while error Bmi = new Double((w/(h*h))+0.5); index = Bmi.intValue(); if (index>30) AddOn="This is far too much!"; else if (index>25) AddOn="That is somewhat high"; else if (index>21) AddOn="That is OK"; else if (index>19) AddOn="That is excellent"; else if (index>15) AddOn="You may have a problem"; else AddOn="Something is wrong\nSeek medical advice!"; JOptionPane.showMessageDialog(null,"Your Body Mass Index is "+index+"\n"+AddOn, "Body Mass Index",JOptionPane.INFORMATION_MESSAGE); Object[] options = { "Yes", "No" }; rep=JOptionPane.showOptionDialog(null, "Try again?", "Body Mass Index",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null, options, options[0]); if (rep==1)again=false; error=true; } // end while again } // end init() } // end all