Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with JAVA? The question is highlighted with red highlight Here's my code import java.awt.event.*; /** @SuppressWarnings(serial) public class MainPanel extends JPanel

Can someone help me with JAVA? The question is highlighted with red highlight

image text in transcribed

Here's my code

import java.awt.event.*;

/** @SuppressWarnings("serial") public class MainPanel extends JPanel {

private final static String[] list = { "inches/cm" }; private JTextField textField; private JLabel label; private JComboBox combo;

MainPanel() {

ActionListener listener = new ConvertListener();

combo = new JComboBox(list); combo.addActionListener(listener); //convert values when option changed

JLabel inputLabel = new JLabel("Enter value:");

JButton convertButton = new JButton("Convert"); convertButton.addActionListener(listener); // convert values when pressed

label = new JLabel("---"); textField = new JTextField(5); add(combo); add(inputLabel); add(textField); add(convertButton); add(label);

setPreferredSize(new Dimension(800, 80)); setBackground(Color.LIGHT_GRAY); }

private class ConvertListener implements ActionListener {

@Override public void actionPerformed(ActionEvent event) {

String text = textField.getText().trim();

if (text.isEmpty() == false) { double value = Double.parseDouble(text);

// the factor applied during the conversion double factor = 0;

// the offset applied during the conversion. double offset = 0;

// Setup the correct factor/offset values depending on required conversion switch (combo.getSelectedIndex()) {

case 0: // inches/cm factor = 2.54; break; }

double result = factor * value + offset;

label.setText(Double.toString(result)); } } }

}

----------

import javax.swing.JFrame;

/** * The main driver program for the GUI based conversion program. * * @author mdixon */ public class Converter { public static void main(String[] args) { JFrame frame = new JFrame("Converter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MainPanel panel = new MainPanel(); frame.setJMenuBar(panel.setupMenu()); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }

Show transcribed image text

Expert Answer

  • Anonymous answered this

    Was this answer helpful?

    1

    0

    102 answers

    I have modified MainPanel class to load combo box with currency data from file. Inline comments are added wherever I have made changes. Screenshot of sample output is also attached for your reference. Let me know if further clarification/modification of program is required.

    PROGRAM

    Converter.java

    import javax.swing.JFrame; /** * The main driver program for the GUI based conversion program. * * @author mdixon */ public class Converter { public static void main(String[] args) { JFrame frame = new JFrame("Converter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MainPanel panel = new MainPanel(); // frame.setJMenuBar(panel.setupMenu()); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }

    MainPanel.java

    import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Vector; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; @SuppressWarnings("serial") public class MainPanel extends JPanel { // private final static String[] list = { "inches/cm" }; private JTextField textField; private JLabel label; // Declaring combo of type currency private JComboBox combo; /* * Method to load currency data from file */ private void initCombo(String file) { // creating vector to provide as data of combobox Vector model = new Vector(); try { // Reading data from file BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8")); String line; // iterating through the file line by line while ((line = br.readLine()) != null) { // splitting line using comma String arr[] = line.split(","); // creating instance of currency object Currency currency = new Currency(); // setting values to currency object currency.setName(arr[0].trim()); currency.setValue(Double.parseDouble(arr[1].trim())); currency.setSymbol(arr[2].trim()); // adding currency object to model model.add(currency); } // closing reader br.close(); } catch (IOException e) { e.printStackTrace(); } // initializing combo box with elements in the Vector. combo = new JComboBox(model); } MainPanel() { ActionListener listener = new ConvertListener(); initCombo("currency.txt"); // calling method to create combo box combo.addActionListener(listener); // convert values when option changed JLabel inputLabel = new JLabel("Enter value:"); JButton convertButton = new JButton("Convert"); convertButton.addActionListener(listener); // convert values when pressed label = new JLabel("---"); textField = new JTextField(5); add(combo); add(inputLabel); add(textField); add(convertButton); add(label); setPreferredSize(new Dimension(800, 80)); setBackground(Color.LIGHT_GRAY); } private class ConvertListener implements ActionListener { @Override public void actionPerformed(ActionEvent event) { String text = textField.getText().trim(); if (text.isEmpty() == false) { double value = Double.parseDouble(text); // the factor applied during the conversion double factor = 0; // the offset applied during the conversion. double offset = 0; // Setup the correct factor/offset values depending on required conversion // switch (combo.getSelectedIndex()) { // // case 0: // inches/cm // factor = 2.54; // break; // } // // Setting factor using the value of selected item factor = ((Currency) combo.getSelectedItem()).getValue(); double result = factor * value + offset; // Setting symbol using the symbol of selected item label.setText(((Currency) combo.getSelectedItem()).getSymbol() + " " + Double.toString(result)); } } } /** * Class to keep currency data read from the file * */ private class Currency { private String name; // name of currency private Double value; // value of currency private String symbol; // symbol of currency // Getters and Setters of the class public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getValue() { return value; } public void setValue(Double value) { this.value = value; } public String getSymbol() { return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } /* * Overriding toString, the value returned from this method will be shown inside * combobox */ @Override public String toString() { return name; } } }
For this requirement the program should be improved so that it no longer relies on hard coded conversion values for the various currencies. In reality the conversion rates constantly change. In order to better support this the available currencies, conversion factors, and currency symbol should be stored in a text file. When the application first starts the contents of this text file (called currency.txt) should be accessed and imported into the program, thus changing the list of supported currencies and the current conversion rates. You will be provided with sample files to use during the development. Typical file content is shown below. Your program should assume the file is always in this format. Euro (EUR), 1.06, South African Rand (ZAR), 17.96, R New Zealand Dollar (NZD), 1.8, NZ$ Note: the file will be UTF-8 encoded (to handle the special non-ASCII characters), so to read the file use something like - Buffered Reader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8")); In some cases, the file contents may be corrupt, so your program must be able to identify invalid entries and discard them. Any invalid entries found should be reported via a dialogue box when the application accesses the file contents. For example, a file with invalid entries may look like the following. Canadian Dollars (CAD), 4a, c$ United Arab Emirates Dirham (AED) 4.2, la 45.2: % Thai Baht (THB), 44.1, B r In the above example the first three entries are invalid, but the final one is correct. Hence only the final currency information should be made available by the program. The other three should be reported as invalid when the program first starts. A final part of this requirement is to add a "Load" option within the application's "File" menu. When selected this should allow the user to select a conversion file via a dialogue box (file chooser). This should be then loaded and the contents used to replace the previously stored conversion information 6

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

Step: 3

blur-text-image

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions