Question: Part 1: write a Java program that inputs a temperature in degrees F and converts it to degrees C. Part 2: Write a Java GUI
Part 1: write a Java program that inputs a temperature in degrees F and converts it to degrees C.
Part 2: Write a Java GUI following the example of the Mileage GUI to do the same conversion.
Using code given.
//===================================================================== // Program to illustrate a simple GUI. // Computes Miles per gallon. // author: wallace rutkowski //=====================================================================
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.math.*;
public class Mileage { private static JTextField gallonsField; private static JTextField milesField; private static JTextField mpgField; private static JButton calculateButton;
//================================================================== // main method // public static void main(String args[]) { //------------------------------------------------------------ // Build the GUI JFrame app = new JFrame("Mileage Calculator"); Container c = app.getContentPane();
c.setLayout(new GridLayout(3, 2));
c.add(new JLabel("Gallons")); // gallons entry gallonsField = new JTextField(); c.add(gallonsField);
c.add(new JLabel("Miles")); // miles entry milesField = new JTextField(); c.add(milesField); calculateButton = new JButton("Calculate mpg"); // calculate button c.add(calculateButton); calculateButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){calculateMpg();} });
mpgField = new JTextField(); // result field mpgField.setEditable(false); c.add(mpgField); app.setSize(300, 120); app.setVisible(true); // do this after setting look and feel
}// end main //================================================================== // utility methods // //--------------------------------------------- // do the calculation public static void calculateMpg() { double gallons = getDouble(gallonsField); double miles = getDouble(milesField);; setDouble(mpgField, miles/gallons); } //--------------------------------------------- // get the floating point value of a text field public static double getDouble(JTextField t) { return Double.parseDouble(t.getText()); } //------------------------------------------- // set a text field to a floating point value public static void setDouble(JTextField t, double p) { t.setText((new Double(p)).toString()); } }// end class Mileage
//===================================================== // Console Java program to compute miles per gallon // import java.util.Scanner;
public class Mileage_Console { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print( "Enter gallons: " ); double gallons = s.nextDouble(); System.out.print( "Enter miles: " ); double miles = s.nextDouble(); System.out.println( miles/gallons + " mpg" ); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
