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

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class F

ID: 3657274 • Letter: I

Question

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FahrenheitPanel extends JPanel { private JLabel inputLabel, outputLabel, resultLabel,commentLabel, comment; private JTextField fahrenheit; // Constructor: Sets up the main GUI components. public FahrenheitPanel() { inputLabel = new JLabel ("Enter Fahrenheit temperature:"); outputLabel = new JLabel ("Temperature in Celsius: "); resultLabel = new JLabel ("---"); commentLabel = new JLabel ("Comment:"); comment = new JLabel("---"); fahrenheit = new JTextField (5); fahrenheit.addActionListener (new TempListener()); add (inputLabel); add (fahrenheit); add (outputLabel); add (resultLabel); add (commentLabel); add (comment); setPreferredSize (new Dimension(300, 75)); setBackground (Color.WHITE); } // Represents an action listener for the temperature input field. private class TempListener implements ActionListener { // Performs the conversion when the enter key is pressed in // the text field. public void actionPerformed (ActionEvent event) { int fahrenheitTemp, celsiusTemp; String text = fahrenheit.getText(); fahrenheitTemp = Integer.parseInt (text); celsiusTemp = (fahrenheitTemp-32) * 5/9; resultLabel.setText (Integer.toString (celsiusTemp)); if(celsiusTemp > 30) { comment.setForeground(Color.BLACK); comment.setBackground(Color.RED); comment.setText("It is Hot!"); } else if(celsiusTemp >20 && celsiusTemp <=30 ) { comment.setBackground(Color.ORANGE); comment.setText("It is warm."); } else if(celsiusTemp >10 && celsiusTemp <=20) { comment.setBackground(Color.YELLOW); comment.setText("It is cool."); } else if(celsiusTemp >0 && celsiusTemp <=10) { comment.setBackground(Color.WHITE); comment.setText("It is chilly."); } else if(celsiusTemp <=0) { comment.setBackground(Color.GRAY); comment.setText("It is cold!"); } } } public static void main (String[] args) { JFrame frame = new JFrame ("Fahrenheit to Celsius Converter"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); FahrenheitPanel panel = new FahrenheitPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Anonymous - 7 hours later for displaying.. .............. import javax.swing.JFrame; 02 03 public class Fahrenheit 04 { 05 06 // Creates and displays the temperature converter GUI. 07 08 public static void main (String[] args) 09 { 10 JFrame frame = new JFrame ("Fahrenheit"); 11 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 12 13 FahrenheitPanel panel = new FahrenheitPanel(); 14 15 frame.getContentPane().add(panel); 16 frame.pack(); 17 frame.setVisible(true); 18 } 19

Explanation / Answer

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; /** The TempConverter class is an applet that converts Fahrenheit temperatures to Celsius. */ public class TempConverter extends JApplet { private JPanel fPanel; // To hold a text field private JPanel cPanel; // To hold a text field private JPanel buttonPanel; // To hold a button private JTextField fahrenheit; // Fahrenheit temperature private JTextField celsius; // Celsius temperature /** init method */ public void init() { // Build the panels. buildFpanel(); buildCpanel(); buildButtonPanel(); // Create a layout manager. setLayout(new GridLayout(3, 1)); // Add the panels to the content pane. add(fPanel); add(cPanel); add(buttonPanel); } /** The buildFpanel method creates a panel with a text field in which the user can enter a Fahrenheit temperature. */ private void buildFpanel() { // Create the panel. fPanel = new JPanel(); // Create a label to display a message. JLabel message1 = new JLabel("Fahrenheit Temperature: "); // Create a text field for the Fahrenheit temp. fahrenheit = new JTextField(10); // Create a layout manager for the panel. fPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); // Add the label and text field to the panel. fPanel.add(message1); fPanel.add(fahrenheit); } /** The buildCpanel method creates a panel that displays the Celsius temperature in a read-only text field. */ private void buildCpanel() { // Create the panel. cPanel = new JPanel(); // Create a label to display a message. JLabel message2 = new JLabel("Celsius Temperature: "); // Create a text field for the celsius temp. celsius = new JTextField(10); // Make the text field read-only. celsius.setEditable(false); // Create a layout manager for the panel. cPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); // Add the label and text field to the panel. cPanel.add(message2); cPanel.add(celsius); } /** The buildButtonPanel method creates a panel with a button that converts the Fahrenheit temperature to Celsius. */ private void buildButtonPanel() { // Create the panel. buttonPanel = new JPanel(); // Create a button with the text "Convert". JButton convButton = new JButton("Convert"); // Add an action listener to the button. convButton.addActionListener(new ButtonListener()); // Add the button to the panel. buttonPanel.add(convButton); } /** Private inner class that handles the action event that is generated when the user clicks the convert button. */ private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { double ftemp, ctemp; // To hold the temperatures // Create a DecimalFormat object to format numbers. DecimalFormat formatter = new DecimalFormat("0.0"); // Get the Fahrenheit temperature and convert it // to a double. ftemp = Double.parseDouble(fahrenheit.getText()); // Calculate the Celsius temperature. ctemp = (5.0 / 9.0) * (ftemp - 32); // Display the Celsius temperature. celsius.setText(formatter.format(ctemp)); } } }

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