Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. _____ _____ GUI and Event Handling - A Currency Converter Your are headed off on a world-wide trip and need a program to help

1. _____ _____ GUI and Event Handling - A Currency Converter

image text in transcribed

Your are headed off on a world-wide trip and need a program to help figure out how much things in other countries cost in dollars. You plan to visit Canada, several countries in Europe, Japan, Australia, India, and Mexico so your program must work for the currencies in those countries. The files CurrencyConverter.java and RatePanel.java contain a skeleton of a program to do this conversion. Complete it as follows:

1. CurrencyPanel currently contains only two componentsa JLabel for the title and a JLabel to display the result of the calculations. It also contains two parallel arraysone is an array of currency names (an array of strings) and the other an array of corresponding exchange rates (the value of one unit of the currency in U.S. Dollars). Compile and run the program to see what it looks like (not much!!).

2. Add a combo box to let the user select the currency. The argument to the constructor should be the array of names of the currencies. Note that the first currency name is actually an instruction to the user so there is no need to have an additional label.

3. Modify actionPerformed in ComboListener so that index is set to be the index of the selected item.

4. Test what you have so far. It should show what one unit of the given currency is in U.S. dollars.

5. Now add a text field (and label) so the user can enter the cost of an item in the selected currency. You need to update the ComboListener so that it gets this value from the textfield and computes and displays the equivalent amount in dollars.

6. Test your program.

7. Modify the layout to create a more attractive GUI.

// ***********************************************************************

// CurrencyConverter.java

//

// Computes the dollar value of the cost of an item in another currency.

// ***********************************************************************

import java.awt.*;

import javax.swing.*;

public class CurrencyConverter

{

public static void main (String[] args)

{

JFrame frame = new JFrame ("Currency Converter");

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

RatePanel ratePanel = new RatePanel ();

frame.getContentPane().add(ratePanel);

frame.pack();

frame.setVisible(true);

}

}

// ***********************************************************************

// RatePanel.java

//

// Panel for a program that converts different currencies to

// U.S. Dollars

// ***********************************************************************

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class RatePanel extends JPanel { private double[] rate;

// exchange rates private String[] currencyName; private JLabel result;

// -----------------------------------------------------------------

// Sets up a panel to convert cost from one of 6 currencies

// into U.S. Dollars. The panel contains a heading, a text

// field for the cost of the item, a combo box for selecting

// the currency, and a label to display the result.

// -----------------------------------------------------------------

public RatePanel ()

{

JLabel title = new JLabel ("How much is that in dollars?");

title.setAlignmentX (Component.CENTER_ALIGNMENT);

title.setFont (new Font ("Helvetica", Font.BOLD, 20));

// Set up the arrays for the currency conversions

currencyName = new String[] {"Select the currency..",

"European Euro", "Canadian Dollar",

"Japanese Yen", "Australian Dollar",

"Indian Rupee", "Mexican Peso"};

rate = new double [] {0.0, 1.2103, 0.7351,

0.0091, 0.6969,

0.0222, 0.0880};

result = new JLabel (" ------------- ");

}

// ******************************************************

// Represents an action listener for the combo box.

// ******************************************************

private class ComboListener implements ActionListener

{

// --------------------------------------------------

// Determines which currency has been selected and

// the value in that currency then computes and

// displays the value in U.S. Dollars.

// --------------------------------------------------

Currency Converter What does it cost in dollars? Enter cost of item 100 Select a currency: European Euro 100 European Euro-121.03 U.S. Dol

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

Students also viewed these Databases questions

Question

Make reduced cast matrix & calculate reduced wast.

Answered: 1 week ago

Question

Persuasive Speaking Organizing Patterns in Persuasive Speaking?

Answered: 1 week ago