Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Scenario/Summary Deliverables The student will submit the following to the Dropb

ID: 3623220 • Letter: S

Question

Scenario/Summary

Deliverables

The student will submit the following to the Dropbox.

1. Source code for program 1
2. Screenshot of output for program 1

i L A B S T E P S

Program 1: Temperature Conversion Program Version 2
Back to top

You will rewrite the temperature conversion program from the previous week. Your GUI application must inherit from the JFrame class. The GUI and event handling setup should be done in the constructor. Do not use any of the GUI editing capabilities of Eclipse for this assignment. The temperature conversion application should have a label and JTextField in which the user inputs a value which must appear in the upper part of the frame. There should be a set of three radio buttons which indicate the input scale of the value to be converted.

There should also be a set of three radio buttons which indicate the output scale to be converted to. The three input scale buttons must appear vertically aligned (i.e., use a JPanel) on the left side of the display, and the three output scale buttons must appear vertically aligned (i.e., use another JPanel) and appear on the right side of the display. Event handling should be set up so that selection of any input or output radio button causes an event which triggers the event handling code to determine which of nine possible conversions is needed. You can display the result in an output text field or in a Jlabel which appears in the bottom part of the display.

Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point. HINT: Use the ItemListener interface and use the isSelected method on the radio buttons to learn which buttons are turned on!

Explanation / Answer

Dear, Here is the code import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JRadioButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class TemperatureConvertor extends JFrame{ /** Display the result of computations*/ private JLabel resultLabel; /** Fahrenheit to Celsius Button*/ private JRadioButton FtoCBtn; /** Celsius to Fahreinhet Button*/ private JRadioButton CtoFBtn; /** Kelvin to Celsius button */ private JRadioButton KtoCBtn; /** Celsius to Kelvin button */ private JRadioButton CtoKBtn; /** Kelvin to Fahreinheit*/ private JRadioButton KtoFBtn; /** Fahreinheit to Kelvin*/ private JRadioButton FtoKBtn; /** user input space */ private JTextField valueField; /** * Public no arg constructor * */ public TemperatureConvertor( ){ //set frame Title setTitle("Temperature Convertor"); JLabel infoMessageLabel = new JLabel("Enter Temperature Value:"); //user will enter value here valueField = new JTextField( 5 ); JLabel resultMessageLabel = new JLabel("Converted value is:"); resultLabel = new JLabel( ); JPanel userInfoPanel = new JPanel( new GridLayout( 2, 2) ); userInfoPanel.add( infoMessageLabel ); userInfoPanel.add( valueField ); userInfoPanel.add( resultMessageLabel ); userInfoPanel.add( resultLabel ); //prepare listener for all Buttons clicks ActionListener buttonClickListener = new ButtonActionListener( ); //prepare buttons and our action listener to their list FtoCBtn = new JRadioButton("F to C"); FtoCBtn.addActionListener( buttonClickListener ); CtoFBtn = new JRadioButton("C to F"); CtoFBtn.addActionListener( buttonClickListener ); KtoCBtn = new JRadioButton("K to C"); KtoCBtn.addActionListener( buttonClickListener ); CtoKBtn = new JRadioButton("C to K"); CtoKBtn.addActionListener( buttonClickListener ); KtoFBtn = new JRadioButton("K to F"); KtoFBtn.addActionListener( buttonClickListener ); FtoKBtn = new JRadioButton("F to K"); FtoKBtn.addActionListener( buttonClickListener ); //build UI with buttons JPanel buttonsPanel = new JPanel( new GridLayout(1,6) ); buttonsPanel.add( FtoCBtn ); buttonsPanel.add( CtoFBtn ); buttonsPanel.add( KtoCBtn ); buttonsPanel.add( CtoKBtn ); buttonsPanel.add( KtoFBtn ); buttonsPanel.add( FtoKBtn ); setLayout( new BorderLayout( ) ); add( userInfoPanel, BorderLayout.CENTER ); add(buttonsPanel, BorderLayout.SOUTH ); //suggested frame size setSize( 500, 100); setResizable( false ); //exit app when frame closed setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); } /** * Displays result of computations with 3 decimal places * */ private void displayResult(double result){ DecimalFormat formatter = new DecimalFormat("####.###"); resultLabel.setText( formatter.format( result ) ); } /** * Displays a message to user instead of a value * */ private void displayErrorMessage(String message){ resultLabel.setText( message ); } //Inner Class which will process button clicks private class ButtonActionListener implements ActionListener{ public void actionPerformed(ActionEvent aev) { //who generated the event? Object source = aev.getSource( ); //prepare to store final result double result = 0.0; //store user entered value double userValue = 0.0; String textValue = null; //try to parse user entered value, // if fails to convert to Double show an error message and skip the rest try{ textValue = valueField.getText( ); userValue = Double.parseDouble( textValue ); } catch(NumberFormatException ne){ //user has input an invalid value displayErrorMessage("" + textValue + " is not a valid number"); return; } //which button the action comes from if( source == FtoCBtn ){ //f to c: 5/9 * (Fahrenheit - 32); note: .55555 = 5/9 result = 5.0/9.0 * ( userValue - 32 ); } else if( source == CtoFBtn ){ //c to f: (1.8 * Centigrade) + 32; note: 1.8 = 9/5 result = (1.8 * userValue ) + 32; }else if( source == KtoCBtn ){ // k to c: Kelvin - 273; result = userValue - 273; } else if ( source == CtoKBtn ){ //c to k: Centigrade + 273; result = userValue + 273; }else if( source == KtoFBtn ){ // k to f: ((Kelvin - 273) * 1.8 ) + 32; note: 1.8 = 9/5 result = ((userValue - 273) * 1.8) + 32; } else if( source == FtoKBtn ){ // f to k: (5/9 * (Fahrenheit - 32) + 273 ); note: .55555 = 5/9 result = 5.0/9.0 * ( userValue - 32 ) + 273; } //display convertion result to user displayResult( result ); } } /** * Application entry point method */ public static void main(String[] args) { //create frame and make it visible new TemperatureConvertor().setVisible( true ); } } Hope this will help you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote