Question

Create a program called Currency Conversion. This will convert one currency to other currencies (for example...

Create a program called Currency Conversion. This will convert one currency to other currencies (for example CAD to GBP, Euro and USD).  The program should prompt user to choose currency in hand and currency required. It should also ask the user to enter amount (currency value).  The conversion rate is fixed as mentioned below. You should store the exchange rate as final values.  Use Scanner for input and printf for formatting decimals (rounded to two decimal places)  Add comments for wherever needed for code maintenance and mathematical procedures  Your program should display well-articulated print statement with appropriate symbols. Exchange rate: As most of the currencies are traded against USD, the exchange rates are expressed in US dollars. One Euro (€) = 1.124 US dollar One CAD ($) = 0.758US dollars One GBP (£) = 1.296 US dollar

Programming Guidelines:  Beginning comments: Include your name and a short narrative as a comment at the beginning of your program stating what the program does, not how it does it.  Ensure variable names are meaningful. Use lower CamelCase. Do not use made up short forms for any part of a variable name.  Are brace brackets aligned? The opening brace bracket should be in the same column as the closing bracket. Did you indent inside the brace bracket?  Challenge: Validate user inputs to ensure it’s not inappropriate, negative entry or zero. Allow the user to re- enter invalid data.  Is the output informative and formatted properly? Use currency symbol and 2 decimal places. Output is well spaced not too congested. Include the blank lines to separate the outputs.  Ensure that the file you submitted can be opened successfully.  Ensure you do not get a divide by zero error when converting currency.

Homework Answers

Answer #1


/**
* CurrencyConverter class
* The program prompt the user to choose currency in hand and currency required.
* It also ask the user to enter amount (currency value)
* Then displays the exchange value
* This program validates every inputs for currency
* Only the positive value for the amount is accepted
*/
import java.util.Scanner;
public class CurrencyConverter {
    // final variables for exchange rate of each currency
    static final double EURO = 1.124;
    static final double CAD = 0.758;
    static final double GBP = 1.296;
    // main method
    public static void main(String[] args) {
        // scnnaer object to read input
        Scanner in = new Scanner(System.in);
        // variable for currency in hand
        String currencyFrom;
        // loop to get and validate input for currency in hand
        while(true){
            System.out.print("Enter currency in hand: ");
            currencyFrom = in.next().toUpperCase();
            if(checkCurrency(currencyFrom))
                break;
            System.out.println("Invalid currency");
        }
      
        // variable for currency required
        String currencyTo;
        // loop to get and validate input for currency required
        while(true){
            System.out.print("Enter required currency: ");
            currencyTo = in.next().toUpperCase();
            if(checkCurrency(currencyTo))
                break;
            System.out.println("Invalid currency");
        }
      
        // variable for amount
        double amount;
        // loop to get positive value for the amount
        while(true){
            System.out.print("Enter Amount: ");
            amount = in.nextDouble();
            if(amount>0)
                break;
            System.out.println("Invalid amount");
        }
      
        // convert given amount to dollar from currency in hand
        double dollar = convertToDollar(currencyFrom, amount);
        // convert dollar to required currency
        double exchangeValue = dollarToCurrency(currencyTo, dollar);
        // display the amount and exchange value in 2 decimal places
        System.out.printf("\n%.2f %s = %.2f %s\n",amount,currencyFrom,exchangeValue,currencyTo);
      
    }
  
    // method to convert given currency amount to dollar
    private static double convertToDollar(String currencyFrom, double value) {
        if(currencyFrom.equals("EURO"))
            return value*EURO;
        if(currencyFrom.equals("CAD"))
            return value*CAD;
        if(currencyFrom.equals("GBP"))
            return value*GBP;
        if(currencyFrom.equals("USD"))
            return value;
        return 0.0;
    }

    // method to convert dollar to given currency amount
    private static double dollarToCurrency(String currency, double dollar) {
        if(currency.equals("EURO"))
            return dollar/EURO;
        if(currency.equals("CAD"))
            return dollar/CAD;
        if(currency.equals("GBP"))
            return dollar/GBP;
        if(currency.equals("USD"))
            return dollar;
        return 0.0;
    }

    // method to check currency
    private static boolean checkCurrency(String currency) {
        return currency.equals("USD") || currency.equals("CAD") || currency.equals("EURO") ||currency.equals("GBP");
    }
}


Sample output

Know the answer?
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
Similar Questions
The following are quotes from a currency dealer in the New York currency market: Currency Spot...
The following are quotes from a currency dealer in the New York currency market: Currency Spot quote Australian dollar (AUD/USD) 0.7832 - 0.7834 Brazilian real (USD/BRL) 3.2335 - 3.2365 British pound (GBP/USD) 1.3507 - 1.3509 Canadian dollar (USD/CAD) 1.2555 - 1.2557 Euro (EUR/USD) 1.1948 - 1.1949 Japanese yen (USD/JPY) 111.44 - 111.45 Mexican peso (USD/MXP) 19.3653 - 19.3718 New Zealand dollar (NZD/USD) 0.7181 - 0.7184 Thai baht (USD/THB) 32.1240 - 32.1430 Egyptian pound (USD/EGP) 17.6860 - 17.8460 South Korean won...
Create a currency class, up to 5 individual currencies, with four attributes - currency name, whole...
Create a currency class, up to 5 individual currencies, with four attributes - currency name, whole parts and fractional parts and fractional's name such that 100 fractional parts equals 1 whole part, e.g. Pound, 1, 55, pence or Euro, 3, 33, cent. Define overloaded operators to add or subtract different currency objects - care should be taken that you can only perform these operation on objects of the same currency. Also, take care that fractional parts roll into whole parts....
Pricing foreign goods The nominal exchange rate is the price of one currency in terms of...
Pricing foreign goods The nominal exchange rate is the price of one currency in terms of another currency. A nominal exchange rate specifies how many units of one country's currency are needed to buy one unit of another country's currency. Suppose the following table presents nominal exchange rate data for November 26, 2014, in terms of U.S. dollars per unit of foreign currency. Use the information in the table to answer the questions that follow. Foreign Currency Cost of One...
Assignment 2 Create a Java program called UserNames that reads the user's first and last name...
Assignment 2 Create a Java program called UserNames that reads the user's first and last name (separately), then prints a string composed of the first 4 letters of the user's last name, followed by the first letter of the user's first name, followed by a random number in the range of 10 to 99 (inclusive). You can assume the first name is at least one letter long and the last name is at least 4 letters. After doing this once,...
Part One: Write a program to draw a picture by defining functions and using Turtle Graphics....
Part One: Write a program to draw a picture by defining functions and using Turtle Graphics. Use the following guidelines to write your program. Give your artwork a name. Print the name to the output Draw a picture using the turtle. You can pick the subject of your picture. Some suggestions include a house, a car, a face, a robot. At least two programmer-defined functions must be used. Use at least one color apart from black. The picture should include...
Question 1 (1 point) Which of the following can cause relative PPP to NOT hold in...
Question 1 (1 point) Which of the following can cause relative PPP to NOT hold in the short run? Question 1 options: frictionless markets state-sponsored monopolies types of labor and unique skill sets than can only be found in one area or certain areas shipping costs Question 2 (1 point) If relative PPP holds, absolute PPP must hold. Question 2 options: True False Question 3 (1 point) In 2019, the US had the highest nominal GDP in the world, before...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output. Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by...
Question 1 You are responsible for managing a US toy manufacturer. Recently your market share has...
Question 1 You are responsible for managing a US toy manufacturer. Recently your market share has dropped dramatically due to strong pricing competition from a China toy manufacturer. What could the US toy manufacturer do to offset this? What kind of help could the US toy manufacturer seek? Question 2 (4 points) The Canadian dollar per U.S. Dollar spot rate today, 3/1/2017 is 1.3500 / 1.3525 Canadian dollars per U.S. Dollar. The spot rate on 2/1/2017 was 1.3435 / 1.3450...
Foreign Exchange (FOREX) Problem Set 1. You in US have an accounts payable to a German...
Foreign Exchange (FOREX) Problem Set 1. You in US have an accounts payable to a German exporter for 200 Porsche Cayenne SUVs. The seller offers a 2 percent discount for payment within 10 days and full payment due in 30 days (2/10 net 30). Today the exchange rate is $1.40 per Euro. You notice that the 30 day forward rate for the $/Euro is $1.38. What should you do? Pay now with early payment discount or wait until end of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT