//Given the length and width of a rectangle, this Java //program determines its area and perimeter. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class RectangleProgram extends JFrame { JLabel lengthL, widthL, areaL, perimeterL; JTextField lengthTF, widthTF, areaTF, perimeterTF; JButton calculateB, exitB; CalculateButtonHandler calculateHandler; ExitButtonHandler exitHandler; private static final int WIDTH = 400; private static final int HEIGHT = 300; public RectangleProgram() { // Create four labels lengthL = new JLabel("Enter height in inches: ", SwingConstants.RIGHT); widthL = new JLabel("Enter the weight in lbs: ", SwingConstants.RIGHT); areaL = new JLabel("BMI: ",SwingConstants.RIGHT); perimeterL = new JLabel("Diagnosis: ", SwingConstants.RIGHT); //Create four textfields lengthTF = new JTextField(10); widthTF = new JTextField(10); areaTF = new JTextField(10); perimeterTF = new JTextField(10); //create Calculate Button calculateB = new JButton("Calculate"); calculateHandler = new CalculateButtonHandler(); calculateB.addActionListener(calculateHandler); //Create Exit Button exitB = new JButton("Exit"); exitHandler = new ExitButtonHandler(); exitB.addActionListener(exitHandler); //Set the title of the window setTitle("Body Mass Index Calculator"); //Get the container Container pane = getContentPane(); //Set the layout pane.setLayout(new GridLayout(5,2)); //Place all items created pane.add(lengthL); pane.add(lengthTF); pane.add(widthL); pane.add(widthTF); pane.add(areaL); pane.add(areaTF); pane.add(perimeterL); pane.add(perimeterTF); pane.add(calculateB); pane.add(exitB); //set the size of the window and display it setSize(WIDTH,HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } private class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { double weight, height, bmi; String diagnosis; height = Double.parseDouble(lengthTF.getText()); weight = Double.parseDouble(widthTF.getText()); bmi = 703 * (weight / Math.pow(height,2)); diagnosis = "What do you think?"; if(bmi < 18.5) { diagnosis = "underweight"; } else if (bmi < 25) { diagnosis = "normal"; } else if (bmi < 30) { diagnosis = "overweight"; } else { diagnosis = "obese"; } //Rectangle r = new Rectangle((int)length, (int)width); //area = r.computeArea(); //perimeter = r.computePerimeter(); areaTF.setText("You're bmi is " + bmi); perimeterTF.setText(diagnosis); } } private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main( String args[] ) { RectangleProgram rectObject = new RectangleProgram(); } }