Week 4 Programming Assignment Write a Java GUI application to do temperature con
ID: 3527731 • Letter: W
Question
Week 4 Programming Assignment
Write a Java GUI application to do temperature conversions between Celcius, Fahranheit, and Kelvin. The GUI display should look something like the following:
Your program must meet the following requirements:
1. Do not use any of the GUI editing capabilities of Eclipse for this assignment. Do all the GUI layout work based on what you have learned in class in the last 2 weeks.
2. The GUI and event handling setup should be done in the constructor of your GUI class or in private methods called from the constructor.
3. The display must have a label and JTextField where the user inputs a value which must appear in the upper part of the frame as shown above.
4. There should be a set of 3 radio buttons which indicate the input scale of the value to be converted. The 3 input scale buttons must be vertically aligned on the left side of the display as shown above.
5. There should be a set of 3 radio buttons which indicate the output scale to be converted to. The 3 output scale buttons must be vertically aligned and appear on the right side of the display as shown above.
6. Event handling must be setup so that selection of any input or output button causes an event which triggers the event handling code to determine which of 9 possible conversions is needed.
7. Event handling must also respond to the user hitting the enter key in the input textfield! For this event, the event handling code must also determine which conversion is needed based on which buttons are selected.
8. Display the conversion result in an output text field or in a JLabel which appears in the bottom part of the display as shown above. Output the degree symbol and output scale information as shown above. You can output a degree symbol by putting the character value 176 into a formatted string.
9. If a radio button is clicked when there is nothing in the input textfield, the event handler must display the string
Explanation / Answer
01 import javax.swing.*; 02 03 /** 04 * This program allows the user to convert temperatures 05 * 06 */ 07 08 public class ConvertTemp { 09 public static void main(String[] args) { 10 JFrame frame = new ConvertTempFrame(); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 frame.setTitle("Temperature Converter"); 13 frame.setVisible(true); 14 } 15 } 001 import java.awt.*; 002 import java.awt.event.*; 003 import javax.swing.*; 004 import java.text.*; 005 006 @SuppressWarnings("serial") 007 public class ConvertTempFrame extends JFrame 008 { 009 /** 010 * Constructs the frame 011 */ 012 public ConvertTempFrame() 013 { 014 //adding input panel 015 JPanel inputPanel = new JPanel(); 016 inputPanel.add(inlabel); 017 inputPanel.add(infield); 018 //adding output panel 019 JPanel outputPanel = new JPanel(); 020 outputPanel.add(outlabel); 021 outputPanel.add(outfield); 022 outfield.setEditable(false);//removes ability to enter a value in the output field 023 add(inputPanel, BorderLayout.NORTH); //adding input panel to frame 024 add(outputPanel, BorderLayout.SOUTH); //adding output panel to frame 025 add(createRadioButtons(), BorderLayout.CENTER); 026 //This is the listener shared among all components 027 class ChoiceListener implements ActionListener 028 { 029 public void actionPerformed(ActionEvent event) 030 { 031 outputConvertedTemp(); 032 } 033 } 034 listener = new ChoiceListener(); 035 setSize(FRAME_WIDTH, FRAME_HEIGHT); 036 } 037 038 /** 039 * Creates the panel for selecting converting option 040 */ 041 public JPanel createRadioButtons() 042 { 043 /** 044 * Creates radio buttons to select the conversion type 045 */ 046 CtoK = new JRadioButton("Celsius to Kelvin"); 047 CtoK.addActionListener(listener); 048 FtoK = new JRadioButton("Fahrenheit to Kelvin"); 049 FtoK.addActionListener(listener); 050 KtoF = new JRadioButton("Kelvin to Fahrenheit"); 051 KtoF.addActionListener(listener); 052 CtoF = new JRadioButton("Celsius to Fahrenheit"); 053 CtoF.addActionListener(listener); 054 FtoC = new JRadioButton("Fahrenheit to Celsius"); 055 FtoC.addActionListener(listener); 056 KtoC = new JRadioButton("Kelvin to Celsius"); 057 KtoC.addActionListener(listener); 058 059 ButtonGroup group = new ButtonGroup(); 060 group.add(CtoK); 061 group.add(FtoK); 062 group.add(KtoF); 063 group.add(CtoF); 064 group.add(FtoC); 065 group.add(KtoC); 066 067 JPanel radioButtons = new JPanel(); 068 radioButtons.setLayout(new GridLayout(3, 2)); 069 radioButtons.add(CtoK); 070 radioButtons.add(FtoK); 071 radioButtons.add(KtoF); 072 radioButtons.add(CtoF); 073 radioButtons.add(FtoC); 074 radioButtons.add(KtoC); 075 return radioButtons; 076 } 077 078 public void outputConvertedTemp() 079 { 080 int temp; 081 double newtemp = 0; 082 String inputString; 083 inputString = infield.getText();//pulls the number that was entered in the input field 084 temp = Integer.parseInt(inputString);// makes a new variable to handle the input 085 //tells the output how to calculate the input based on the button that was pressed 086 if (CtoK.isSelected()) 087 newtemp = (temp + 273);//Celsius to Kelvin equation 088 else if (FtoK.isSelected()) 089 newtemp = ((.55555) * (temp - 32) + 273);//Fahrenheit to Kelvin equation 090 else if (KtoF.isSelected()) 091 newtemp = (((temp - 273) * (1.8)) + 32);//Kelvin to Fahrenheit equation 092 else if (CtoF.isSelected()) 093 newtemp = (((1.8) * temp) + 32);//Celsius to Fahrenheit equation 094 else if (FtoC.isSelected()) 095 newtemp = ((.55555) * (temp - 32));//Fahrenheit to Celsius equation 096 else if (KtoC.isSelected()) 097 newtemp = (temp - 273);//Kelvin to Celsius equation 098 //sets newtemp value to output field 099 outfield.setText(" "+ three.format(newtemp)); 100 } 101 DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output 102 private ActionListener listener; 103 private JTextField infield = new JTextField(10); 104 private JTextField outfield = new JTextField(10); 105 private JLabel inlabel = new JLabel("Temperature Value:"); 106 private JLabel outlabel = new JLabel("Converted Temperature Value:"); 107 private static final int FRAME_WIDTH = 400; 108 private static final int FRAME_HEIGHT = 200; 109 private JRadioButton CtoK; 110 private JRadioButton FtoK; 111 private JRadioButton KtoF; 112 private JRadioButton CtoF; 113 private JRadioButton FtoC; 114 private JRadioButton KtoC; 115 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.