How do I implement an error message if a number is not valid? import java.awt.*; import javax.swing.JButton; import java
Posted: Sun Jul 03, 2022 11:23 am
How do I implement an error message if a number is notvalid?
import java.awt.*;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingConstants;import javax.swing.JOptionPane;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;
public class GUI {
public static class Converter{
// attribute private double input;
// default constructor with no parameter which setsinput //to Double.NaN [Not a Number] Converter(){ this.input = Double.NaN; }
// overloaded constructor with input for parameter Converter(double input){ this.input = input; }
// get and set methods double getInput() { return this.input; } public void setInput(double input) { this.input = input; }
// method convert() to return input value double convert() { return this.input; }
}// end Converter
public static class TemperatureConverter extends Converter{
// constructs that call parent constructor TemperatureConverter(){ super(); }
TemperatureConverter(double input){ super(input); }
/* overridden convert() method to convert Fahrenheit toCelsius *and return value; if the instance has no input or not anumber, *it should return Double.NaN */ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else { return((super.getInput() - 32) * 5)/9; } }
}// end TemperatureConverter
public static class DistanceConverter extends Converter{
// constructors that call parent constructor DistanceConverter(){ super(); }
DistanceConverter(double input){ super(input); }
/* overridden convert() method to convert input (distancein miles) * to distance(km) and return value; if the instance has noinput *or not a number, it should return Double.NaN **/ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else { return super.getInput() * 1.609; } }
}// end DistanceConverter
public static class GUIConverter extends Converter{
GUIConverter(){
// attributes JButton distance, temperature, exit; // identifiers foraction events JLabel label1; // reference to guide user in the program
// implement GUIConverter class JFrame frame = new JFrame("Converter"); JPanel panel = new JPanel();
// creates instance of the layout GridBagLayout layout = new GridBagLayout(); panel.setLayout(layout); GridBagConstraints window = new GridBagConstraints();
// establish the pixels surrounding objects in thelayout window.insets = new Insets(10, 5, 5, 10);
// add constraints on specific buttons label1 = new JLabel("Please select which converter you wouldlike:", SwingConstants.CENTER); label1.setHorizontalAlignment(JLabel.RIGHT); window.fill = GridBagConstraints.HORIZONTAL; window.gridx = 0; window.gridy = 0; window.gridwidth = 10; panel.add(label1, window);
// add Distance GUI button distance = new JButton("Distance"); window.fill = GridBagConstraints.HORIZONTAL; window.gridx = 0; window.gridy = 1; window.gridwidth = 1; panel.add(distance, window);
// add Temperature GUI button temperature = new JButton("Temperature"); window.gridx = 1; window.gridy = 1; window.gridwidth = 4; panel.add(temperature, window);
// add Exit GUI button exit = new JButton("Exit"); window.gridx = 1; window.gridy = 2; window.fill = GridBagConstraints.HORIZONTAL; window.gridwidth = 2; panel.add(exit, window);
// set size and layout of frame frame.add(panel); frame.setLayout(new GridBagLayout()); frame.setSize(360, 150); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminate program when user closes GUI
// add action listener to buttons; allows user to inputvalue after // Distance Converter button is selected and get convertedresult distance.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // store input String str = JOptionPane.showInputDialog("Inputdistance in miles to" + " convert to kilometers: "); double miles = Double.parseDouble(str); // conversionto double
// instance creation of DistanceConverter objectusing miles DistanceConverter obj = newDistanceConverter(miles); // call convert() of object and display result JOptionPane.showMessageDialog(frame, miles + " milesequals " + obj.convert()+ " kilometers."); } });
// allows user to input value when Temperature button isselected and // returns converted result temperature.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // store input string String str = JOptionPane.showInputDialog("InputFahrenheit temp to" + " convert to Celsius: "); double fahren = Double.parseDouble(str); //conversion to double
// instance creation of TemperatureConverterobject TemperatureConverter obj = newTemperatureConverter(fahren); JOptionPane.showMessageDialog(frame, fahren + " Fequals " + obj.convert() + "C."); } });
// program terminates when user clicks Exit exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); // dispose frame } }); }
public static void main(String[] args) {
// call constructor to create GUI new GUIConverter(); }}
import java.awt.*;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingConstants;import javax.swing.JOptionPane;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;
public class GUI {
public static class Converter{
// attribute private double input;
// default constructor with no parameter which setsinput //to Double.NaN [Not a Number] Converter(){ this.input = Double.NaN; }
// overloaded constructor with input for parameter Converter(double input){ this.input = input; }
// get and set methods double getInput() { return this.input; } public void setInput(double input) { this.input = input; }
// method convert() to return input value double convert() { return this.input; }
}// end Converter
public static class TemperatureConverter extends Converter{
// constructs that call parent constructor TemperatureConverter(){ super(); }
TemperatureConverter(double input){ super(input); }
/* overridden convert() method to convert Fahrenheit toCelsius *and return value; if the instance has no input or not anumber, *it should return Double.NaN */ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else { return((super.getInput() - 32) * 5)/9; } }
}// end TemperatureConverter
public static class DistanceConverter extends Converter{
// constructors that call parent constructor DistanceConverter(){ super(); }
DistanceConverter(double input){ super(input); }
/* overridden convert() method to convert input (distancein miles) * to distance(km) and return value; if the instance has noinput *or not a number, it should return Double.NaN **/ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else { return super.getInput() * 1.609; } }
}// end DistanceConverter
public static class GUIConverter extends Converter{
GUIConverter(){
// attributes JButton distance, temperature, exit; // identifiers foraction events JLabel label1; // reference to guide user in the program
// implement GUIConverter class JFrame frame = new JFrame("Converter"); JPanel panel = new JPanel();
// creates instance of the layout GridBagLayout layout = new GridBagLayout(); panel.setLayout(layout); GridBagConstraints window = new GridBagConstraints();
// establish the pixels surrounding objects in thelayout window.insets = new Insets(10, 5, 5, 10);
// add constraints on specific buttons label1 = new JLabel("Please select which converter you wouldlike:", SwingConstants.CENTER); label1.setHorizontalAlignment(JLabel.RIGHT); window.fill = GridBagConstraints.HORIZONTAL; window.gridx = 0; window.gridy = 0; window.gridwidth = 10; panel.add(label1, window);
// add Distance GUI button distance = new JButton("Distance"); window.fill = GridBagConstraints.HORIZONTAL; window.gridx = 0; window.gridy = 1; window.gridwidth = 1; panel.add(distance, window);
// add Temperature GUI button temperature = new JButton("Temperature"); window.gridx = 1; window.gridy = 1; window.gridwidth = 4; panel.add(temperature, window);
// add Exit GUI button exit = new JButton("Exit"); window.gridx = 1; window.gridy = 2; window.fill = GridBagConstraints.HORIZONTAL; window.gridwidth = 2; panel.add(exit, window);
// set size and layout of frame frame.add(panel); frame.setLayout(new GridBagLayout()); frame.setSize(360, 150); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminate program when user closes GUI
// add action listener to buttons; allows user to inputvalue after // Distance Converter button is selected and get convertedresult distance.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // store input String str = JOptionPane.showInputDialog("Inputdistance in miles to" + " convert to kilometers: "); double miles = Double.parseDouble(str); // conversionto double
// instance creation of DistanceConverter objectusing miles DistanceConverter obj = newDistanceConverter(miles); // call convert() of object and display result JOptionPane.showMessageDialog(frame, miles + " milesequals " + obj.convert()+ " kilometers."); } });
// allows user to input value when Temperature button isselected and // returns converted result temperature.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // store input string String str = JOptionPane.showInputDialog("InputFahrenheit temp to" + " convert to Celsius: "); double fahren = Double.parseDouble(str); //conversion to double
// instance creation of TemperatureConverterobject TemperatureConverter obj = newTemperatureConverter(fahren); JOptionPane.showMessageDialog(frame, fahren + " Fequals " + obj.convert() + "C."); } });
// program terminates when user clicks Exit exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); // dispose frame } }); }
public static void main(String[] args) {
// call constructor to create GUI new GUIConverter(); }}