This document contains the code for a Java application that converts a person's height entered in feet and inches to centimeters. It creates a GUI with labels, text fields, sliders and a button to trigger the conversion. When the slider values change, the stateChanged method updates the text fields. The HeightFtInToCm method performs the actual conversion calculation. The main method creates the frame and makes it visible.
Measures of Dispersion and Variability: Range, QD, AD and SD
Convert height values to cm
1. Value isn't changing and I can't seem to get the conversion to work.package UI;import
java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import
javax.swing.*;import javax.swing.event.ChangeEvent;import
javax.swing.event.ChangeListener;public class HeightConverterFrame extends JFrame
implements ActionListener, ChangeListener {private JTextField heightCmField; // Holds height
output value in cmprivate JTextField heightFtField; // Holds height input value in feetprivate
JTextField heightInField; // Holds heigth input value in inchesprivate JLabel feetLabel; // Label
for heigth input in feetprivate JLabel inchesLabel; // Label for height input in inchesprivate
JLabel cmLabel; // Label for heigth in cmprivate JButton convertButton; // Triggers heigth
conversionprivate JSlider heightFtSlider; // Slider for feet inputprivate JSlider heightInSlider; //
Slider for inches inputfinal static double CM_PER_IN = 2.54; // Centimeters per inchfinal static
int IN_PER_FT = 12; //inch per foot/*constructor creates GUI components. It adds GUI
componentsusing a GridBagLayout*/HeightConverterFrame(){int feetMin = 0; //feet slider min
valueint feetMax = 10; //feet slider max valueint feetInch = 5; //feet slider initial valueint feetInit
= 5; // feet slider intial valueint inchesMin = 0;int inchesMax = 11;int inchesInit = 8; // inches
slider intial valueGridBagConstraints layoutConst = null; // GUI component layout//set frame
titlesetTitle("Height converter");//Create labelsfeetLabel = new JLabel("enter feet");inchesLabel
= new JLabel("enter inches");cmLabel = new JLabel("centimeters");heightCmField = new
JTextField(10);heightCmField.setEnabled(false);convertButton = new
JButton("convert");convertButton.addActionListener(this);//create slider that enables user to
enter height in feetheightFtSlider = new JSlider(feetMin, feetMax,
feetInit);heightFtSlider.addChangeListener(this);heightFtSlider.setMajorTickSpacing(10);height
FtSlider.setMinorTickSpacing(1);heightFtSlider.setPaintTicks(true);heightFtSlider.setPaintLabel
s(true);heightFtField = new
JTextField(10);heightFtField.setEditable(false);heightFtField.setText("5");//create slider that
enables user to enter height in inchesheightInSlider = new JSlider(inchesMin, inchesMax,
inchesInit);heightInSlider.addChangeListener(this);heightInSlider.setMajorTickSpacing(10);heig
htInSlider.setMinorTickSpacing(1);heightInSlider.setPaintTicks(true);heightInSlider.setPaintLab
els(true);heightInField = new
JTextField(10);heightInField.setEditable(false);heightInField.setText("8");// create frame and
add components using GridBaglayoutsetLayout(new GridBagLayout());//01layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(10, 10, 1, 1);layoutConst.anchor =
GridBagConstraints.LINE_START;layoutConst.gridx = 0;layoutConst.gridy =
0;layoutConst.gridwidth = 1;add(feetLabel, layoutConst);//02layoutConst = new
2. GridBagConstraints();layoutConst.insets = new Insets(10, 10, 1, 1);layoutConst.anchor =
GridBagConstraints.LINE_START;layoutConst.gridx = 2;layoutConst.gridy =
0;layoutConst.gridwidth = 1;add(inchesLabel, layoutConst);//03layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(10, 1, 1, 10);layoutConst.fill =
GridBagConstraints.HORIZONTAL;layoutConst.gridx = 1;layoutConst.gridy =
0;layoutConst.gridwidth = 1;add(heightFtField, layoutConst);//04layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(10, 10, 1, 10);layoutConst.fill =
GridBagConstraints.HORIZONTAL;layoutConst.gridx = 3;layoutConst.gridy =
0;layoutConst.gridwidth = 1;add(heightInField, layoutConst);//05layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(1, 10, 10, 10);layoutConst.fill =
GridBagConstraints.HORIZONTAL;layoutConst.gridx = 0;layoutConst.gridy =
1;layoutConst.gridwidth = 2;add(heightFtSlider, layoutConst);//06layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(1, 10, 10, 10);layoutConst.fill =
GridBagConstraints.HORIZONTAL;layoutConst.gridx = 2;layoutConst.gridy =
1;layoutConst.gridwidth = 2;add(heightInSlider, layoutConst);//07layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(10, 10, 10, 5);layoutConst.anchor =
GridBagConstraints.LINE_END;layoutConst.gridx = 0;layoutConst.gridy =
2;layoutConst.gridwidth = 1;add(convertButton, layoutConst);//08layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(10, 10, 10, 1);layoutConst.anchor =
GridBagConstraints.LINE_END;layoutConst.gridx = 1;layoutConst.gridy =
2;layoutConst.gridwidth = 1;add(cmLabel, layoutConst);//09layoutConst = new
GridBagConstraints();layoutConst.insets = new Insets(10, 1, 10, 10);layoutConst.fill =
GridBagConstraints.HORIZONTAL;layoutConst.gridx = 2;layoutConst.gridy =
2;layoutConst.gridwidth = 2;add(heightCmField, layoutConst);}/* converts a height in feet/
inches to centimeters*/public static double HeightFtInToCm(int ft, int in){int totIn;double
cmHeight;totIn = (ft * IN_PER_FT) + in; //total inchescmHeight = totIn * CM_PER_IN; //
convert to cmreturn cmHeight;}//called as slider value changes. Update field to
display//conversion of numerical value@Overridepublic void stateChanged(ChangeEvent
event){int sliderVal;String strSliderVal;JSlider sourceEvent = (JSlider)
event.getSource();if(sourceEvent == heightFtSlider){sliderVal =
heightFtSlider.getValue();strSliderVal = Integer.toString(sliderVal);} else if(sourceEvent ==
heightInSlider){sliderVal = heightInSlider.getValue();strSliderVal =
Integer.toString(sliderVal);heightInField.setText(strSliderVal);}}public static void main(String[]
args) {HeightConverterFrame myFrame = new
HeightConverterFrame();myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);myFr
ame.pack();myFrame.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e)