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

The objectives of this assignment are to: Gain experience using the Eclipse IDE.

ID: 3691798 • Letter: T

Question

The objectives of this assignment are to:

Gain experience using the Eclipse IDE.

Gain an understanding of creating Java GUIs using AWT and Swing.

Create a Java program that allows a user to convent distance measurements from inches to centimeters or vice versa using a graphical user interface (GUI) resembling the image below. (To obtain the same dimensions as below, the window is 250 x 180 pixels and the text boxes are 10 wide.)

The user may enter the distance (integer or decimal) in the Inches text box. When the user clicks the IN to CM button, the program will calculate the centimeter equivalent and display it in the Centimeter text box with one decimal place. Conversely, the user may enter the distance (integer or decimal) in the Centimeter text box and the CM to IN button, the program will calculate the inches equivalent and display it in the Inches text box with one decimal place.

Requirements

Your test program must be named Prog9.

Your JFrame class must be named Prog9Frame.

Your source code should be documented using Javadoc style according to the course standards.

Abide by good programming practices.

Use the default package in your project; do not specify a package in your code. Use of a package will interfere with testing and severely impact grading of your program.

Use the class names specified in the assignment.

Do not specify a path for the image file.

Prog8 | X 1 21. 2 1 ,63 Metric Converter Inches Centimeters IN to CM CM to IN

Explanation / Answer

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

/**

The MetricConverterWindow class lets the user enter a

distance in inhes and centimers. Radio buttons can be selected to

convert the inches to centimers and vice versa.

*/

public class MetricConverterWindow extends JFrame

{

private JPanel panel; // A holding panel

private JLabel messageLabel; // A message to the user

private JTextField InchesTextField;

private JTextField CentimetersTextField; // To hold user input

  

private JRadioButton IN TO CM Button; // To convert to IN TO CM

private JRadioButton CM TO IN Button; // To convert to CM TO IN

private ButtonGroup radioButtonGroup; // To group radio buttons

private final int WINDOW_WIDTH = 250; // Window width

private final int WINDOW_HEIGHT = 180; // Window height

/**

Constructor

*/

public MetricConverterWindow()

{

// Set the title.

setTitle("Prog9");

// Set the size of the window.

setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

// Specify an action for the close button.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Build the panel and add it to the frame.

buildPanel();

// Add the panel to the frame's content pane.

add(panel);

// Display the window.

setVisible(true);

}

/**

The buildPanel method adds a label, text field, and

and three buttons to a panel.

*/

private void buildPanel()

{

// Create the label, text field, and radio buttons.

messageLabel = new JLabel("inches");

messageLabel1 = new JLabel("centimeters");

InchesTextField = new JTextField(10);

CenimetersTextField = new JTextField(10);

centimetersButton = new JRadioButton("Convert to cenimeters");

inchesButton = new JRadioButton("Convert to inches");

// Group the radio buttons.

radioButtonGroup = new ButtonGroup();

radioButtonGroup.add(inchesButton);

radioButtonGroup.add(centimetersButton);

  

// Add action listeners to the radio buttons.

  

centimetersButton.addActionListener(new RadioButtonListener());

inchesButton.addActionListener(new RadioButtonListener());

// Create a panel and add the components to it.

panel = new JPanel();

panel.add(messageLabel);

panel.add(messageLabel1);

panel.add(InchesTextField);

panel.add(CentimetersTextField);

panel.add(inchesButton);

panel.add(centimersButton);

panel.add(Button)

}

/**

Private inner class that handles the event when

the user clicks one of the radio buttons.

*/

private class RadioButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String input; // To hold the user's input

String convertTo = ""; // The units we're converting to

double result = 0.0; // To hold the conversion

// Get the kilometers entered.

input = inchesTextField.getText();

input1 = centimersTextField.getText();

// Determine which radio button was clicked.

if (e.getSource() == centimersButton)

{

// Convert to cenimers.

convertTo = " centimets.";

result = Double.parseDouble(input) * 2.54;

}

else if (e.getSource() == centimeterButton)

{

// Convert to inches.

convertTo = " inches.";

result = Double.parseDouble(input1) *0.3937;

}

// Display the conversion.

JOptionPane.showMessageDialog(null, input +

" centimers is " + result + IN To CM);

JOptionPane.showMessageDialog(null, input1 +

" inches is " + result + CM TO IN);

}

}

/**

The main method creates an instance of the

MetricConverterWindow class, displaying its window.

*/

public static void main(String[] args)

{

new MetricConverterWindow();

}

}

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