Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

In the following code, I need to ensure that the The window should be centered in the computer screen. I have used the hint that

In the following code, I need to ensure that the The window should be centered in the computer screen. I have used the hint that the professor gave [Hint: frame.setLocationRelativeTo(null);] and i've added that to the script but it doesn't seem to be coming out correctly.

I also need to add exceptions?? that will throw out an error message to the user in the console should they enter an invalid characters such as letters instead of numbers when prompted for the values.

Is it possible to ensure that the output on the messages for the GUI's are limited to only 2 decimal places? If so, how?

*************************************

package CMIS242ASG3AfetseM;

import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;

public class CMIS242ASG3AfetseM { public static class Converter { private double input; //private attribute //default constructor Converter() { this.input=Double.NaN; } //overload constructor Converter(double input) { this.input = input; } //get and set methods for input double getInput() { return this.input; } void setInput(double input) { this.input = input; } //method converter double convert() { return this.input; } } //Temperature Converter Class public static class TemperatureConverter extends Converter { //child constructors which calls parent constructor TemperatureConverter() { super(); } TemperatureConverter(double input) { super(input); } //override convert() method to convert input and returns value. If no value returns Double.NaN. double convert() { //get method of super to obtain input and verify if default value and calculate temp in Celsius if needed if(super.getInput() == Double.NaN) { return Double.NaN; } else { return ((super.getInput()-32.0)*5.0)/9.0; } } } //Distance Converter Class public static class DistanceConverter extends Converter { //child constructors which call parent constructor DistanceConverter() { super(); } DistanceConverter(double input) { super(input); } //override convert() method to convert input and returns value. If no value returns Double.NaN. double convert() { //get method of super to obtain input and verify if default and calculate distance in KM if needed if(super.getInput() == Double.NaN) { return Double.NaN; } else { return super.getInput() * 1.609; } } } //GUI Converter Class public static class GUIConverter { GUIConverter() { //implement GUIConverter class using JFrame and JPanel JFrame frame = new JFrame("Converter"); frame.setLocationRelativeTo(null); frame.setLayout (new FlowLayout ()); JPanel panel=new JPanel(); //add 3 buttons for the GUI JButton button1=new JButton("Distance Converter"); JButton button2=new JButton("Temperature Converter"); JButton button3=new JButton("Exit"); //add buttons to panel, add panel to frame, and set parameters of the frame panel.add(button1); panel.add(button2); panel.add(button3); frame.add(panel); frame.setLocationRelativeTo(null); frame.setLayout(new FlowLayout ()); frame.setSize(450,80); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //adding actionlistener to the buttons //when user clicks distancecoverter, an input dialog box will pop up in which the user can type value and click ok; message //dialog box will then display result after conversion button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { String str = JOptionPane.showInputDialog("Please Enter Distance in Miles:"); Double miles = Double.parseDouble(str); DistanceConverter obj = new DistanceConverter(miles); JOptionPane.showMessageDialog(frame,"Distance in KM is "+obj.convert()); } }); //when user clicks on temperature button, an input dialog box will pop up in which the user can type value and click ok; message //dialog box will then display result after conversion button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { String str = JOptionPane.showInputDialog("Please Enter Temperature in Fahrenheit:"); Double temp = Double.parseDouble(str); TemperatureConverter obj = new TemperatureConverter(temp); JOptionPane.showMessageDialog(frame, "Temperature in Celsius is " +obj.convert()); } }); //when user clicks Exit, the program terminates button3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { frame.dispose(); } }); } public static void main(String[] args) { new GUIConverter(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Organizational Behaviour Concepts Controversies Applications

Authors: Nancy Langton, Stephen P. Robbins, Timothy A. Judge, Katherine Breward

6th Canadian Edition

132310317, 978-0132310314

More Books

Students explore these related Programming questions