Question

VII JAVA ASSIGNMENT. Write a program with a graphical interface that allows the user to convert...

VII JAVA ASSIGNMENT.

Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a label to show the result. Display a warning if the user does not choose different currencies. Use the following conversion rates:

1 EUR is equal to 1.42 USD.

1 GBP is equal to 1.64 USD.

1 GBP is equal to 1.13 EUR.

It is the programmer’s responsibility to ensure that no matter what the user enters, the program does not crash. Call the main program Converter.java. You will need another class for the

frame.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

// Converter.java

public class Converter {

                /**

                * constants for conversion rates

                */

                private static final double EUR_to_USD = 1.42;

                private static final double GBP_to_USD = 1.64;

                private static final double GBP_to_EUR = 1.13;

                /**

                * static method to convert an amount from source to target currency

                *

                * @param amount

                *            - amount to be converted

                * @param source

                *            - source unit (currency)

                * @param target

                *            - target unit

                * @return - converted value

                */

                public static double convert(double amount, Currency source, Currency target) {

                                /**

                                * performing conversions based on source and target currencies

                                */

                                if (source == Currency.USD && target == Currency.EUR) {

                                                return amount / EUR_to_USD;

                                }

                                if (source == Currency.USD && target == Currency.GBP) {

                                                return amount / GBP_to_USD;

                                }

                                if (source == Currency.EUR && target == Currency.USD) {

                                                return amount * EUR_to_USD;

                                }

                                if (source == Currency.EUR && target == Currency.GBP) {

                                                return amount / GBP_to_EUR;

                                }

                                if (source == Currency.GBP && target == Currency.USD) {

                                                return amount * GBP_to_USD;

                                }

                                if (source == Currency.GBP && target == Currency.EUR) {

                                                return amount * GBP_to_EUR;

                                }

                                return amount;// same source & target

                }

}

/**

* an enum to represent available Currency units

*/

enum Currency {

                USD, EUR, GBP

}

//end of Converter.java

// ConverterGUI.java

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class ConverterGUI extends JFrame {

                /**

                * defining all required components

                */

                // an array of currencies to initialize Jcombobox

                Currency currencies[] = { Currency.USD, Currency.EUR, Currency.GBP };

                JComboBox source, target;

                JTextField input, output;

                JButton convertBtn;

                /**

                * constructor to create the GUI

                */

                public ConverterGUI() {

                                setDefaultCloseOperation(EXIT_ON_CLOSE);

                                setSize(500, 300);

                                /**

                                * using grid layout with 1 columns and any number of rows

                                */

                                setLayout(new GridLayout(0, 1));

                               

                                /**

                                * Creating and adding all UI elements in order

                                */

                               

                                add(new JLabel("Enter amount:"));

                                input = new JTextField();

                                add(input);

                                add(new JLabel("Select source currency:"));

                                source = new JComboBox(currencies);

                                add(source);

                                add(new JLabel("Select target currency:"));

                                target = new JComboBox(currencies);

                                add(target);

                                add(new JLabel("Result:"));

                                output = new JTextField();

                                output.setEditable(false);

                                add(output);

                                convertBtn = new JButton("Convert");

                                add(convertBtn);

                                setVisible(true);

                                /**

                                * adding button click listener

                                */

                                convertBtn.addActionListener(new ActionListener() {

                                                @Override

                                                public void actionPerformed(ActionEvent ae) {

                                                                try {

                                                                                /**

                                                                                * getting the amount

                                                                                */

                                                                                double amount = Double.parseDouble(input.getText());

                                                                                /**

                                                                                * getting the selected units from combo boxes

                                                                                */

                                                                                Currency sourceCurrency = (Currency) source

                                                                                                                .getSelectedItem();

                                                                                Currency targetCurrency = (Currency) target

                                                                                                                .getSelectedItem();

                                                                                /**

                                                                                * validating source and target units

                                                                                */

                                                                                if (sourceCurrency == targetCurrency) {

                                                                                                JOptionPane.showMessageDialog(null,

                                                                                                                                "Source and target currencies are same!");

                                                                                } else {

                                                                                                /**

                                                                                                * performing the conversion using static method of the Converter class

                                                                                                */

                                                                                                double result = Converter.convert(amount,

                                                                                                                                sourceCurrency, targetCurrency);

                                                                                                /**

                                                                                                * displaying the results after rounding to 2 decimal places

                                                                                                */

                                                                                                output.setText(String.format("%.2f", result));

                                                                                }

                                                                } catch (Exception e) {

                                                                                JOptionPane.showMessageDialog(null, "Invalid input amount");

                                                                }

                                                }

                                });

                }

                public static void main(String[] args) {

                                //creating the GUI

                                new ConverterGUI();

                }

}

X Enter amount: 100 Select source currency: USD Message Select target curr (i USD Source and target currencies are same! Resu

Enter amount: 100 Select source currency: GBP Select target currency: EUR Result 113.00 Convert X

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
VII JAVA ASSIGNMENT. Write a program with a graphical interface that allows the user to convert...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please write code in java language and do not add any break, continue or goto statements...

    please write code in java language and do not add any break, continue or goto statements Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a...

  • JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The...

    JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The interface components will appear centered in the interface, and in two rows. o Row one will have a Label that reads “Please enter a valid integer:” and a Text Field to take in the integer from the user. o Row two will have a Button that when pressed converts the integer to binary  The conversion will be completed using recursion – A separate...

  • WRITE a program in JAVA using GRAPHICAL USER INTERFACE to make fruit and vegetable display panel with a toggle switch.(m...

    WRITE a program in JAVA using GRAPHICAL USER INTERFACE to make fruit and vegetable display panel with a toggle switch.(make two displays panel 1 for fruit and the other for vegetables , if we press the toggle switch then the 1st panel will show and if we again press the switch then the 1st will hide and other will show) and vice versa.

  • Create a currency converter java program. It will store conversion rates between some currency and the...

    Create a currency converter java program. It will store conversion rates between some currency and the US$ for a month. The program flow is as follows: (Part 1)Get user input for the historical data. Historical data will be the currency, day of the month (1-31) and conversion rate. Keep getting input until a null or space is entered for the currency.   Assume 3 currencies max will be entered. Store data in a 2-dimensional array. Maybe 32 rows by 5 columns...

  • Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature...

    Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature values between degrees Fahrenheit and degrees Celsius. The interface should have labeled entry fields for these two values. breezypythongui.py temperatureconvert... + 1 2 File: temperatureconverter.py 3 Project 8.3 4 Temperature conversion between Fahrenheit and Celsius. 5 Illustrates the use of numeric data fields. 6 • These components should be arranged in a grid where the labels occupy the first row and the corresponding fields...

  • In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student...

    In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student information system. The application should allow: 1. Collect student information and store it in a binary data file. Each student is identified by an unique ID. The user should be able to view/edit an existing student. Do not allow student to edit ID. 2. Collect Course information and store it in a separate data file. Each course is identified by an unique...

  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

  • This is a python question. from urllib import request import json """ Complete this program that...

    This is a python question. from urllib import request import json """ Complete this program that calculates conversions between US dollars and other currencies. In your main function, start a loop. In the loop, ask the user what currency code they would like to convert to And, how many dollars they want to convert. If you scroll down, you can see an example response with the available 3-letter currency codes. Call the get_exchange_rates() function. This function returns a dictionary with...

  • Write a MATLAB Graphical User Interface (GUI) to simulate and plot the projectile motion – the...

    Write a MATLAB Graphical User Interface (GUI) to simulate and plot the projectile motion – the motion of an object projected into the air at an angle. The object flies in the air until the projectile returns to the horizontal axis (x-axis), where y=0. This MATLAB program should allow the user to try to hit a 2-m diameter target on the x-axis (y=0) by varying conditions, including the lunch direction, the speed of the lunch, the projectile’s size, and the...

  • Assignment Overview This assignment will give you more experience on the use of strings and iterations....

    Assignment Overview This assignment will give you more experience on the use of strings and iterations. The goal of this project is to use Google’s currency converter API to convert currencies in Python and display the result to user by processing the returned results. Assignment Background The acronym API stands for “Application Programming Interface”. It is usually a series of functions, methods or classes that supports the interaction of the programmer (the application developer, i.e., you) with some particular program....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT