Question
Need help with this program. Everything I do seems to not work, it just shows me the same thing, even if I don't have any
Need help with this program. Everything I do seems to not work, it just shows me the same thing, even if I don't have any errors
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:
CurrencyPanel currently contains only two components -- a JLabel for the title and a JLabel to display the result of the calculations. It also contains two parallel arrays -- one 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!!).
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.
Modify actionPerformed in ComboListener so that index is set to be the index of the selected item.
Test what you have so far. It should show what one unit of the given currency is in U.S. dollars.
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.
Test your program.
Modify the layout to create a more attractive GUI (suggestion: use a box layout).
// ****************************************************************** // 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 JComboBox currencyCombo; 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, 0.948968, 0.646920, 0.00803616, 0.562082, 0.0204441, 0.103735}; result = new JLabel (" U.S. Dollars ????? "); add (title); add (result); } // ****************************************************** // 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. // -------------------------------------------------- public void actionPerformed (ActionEvent event) { int index = 0; result.setText ("1 " + currencyName[index] + " = " + rate[index] + " U.S. Dollars"); } } }
// ********************************************************************** // File name: CurrencyConverter.java // Name: Brittany Hines // Purpose: 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); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started