In: Computer Science
Programming language is Java
In this second assignment, you will calculate currency conversions from United States Dollars (USD) to Indian Rupees (INR) or to Euros (EUR) or to British Pounds (GB) using methods and formatting the results.
Use these ratios: 1 USD to 72.282250 INR 1 USD to 0.913465 EUR 1 USD to 0.833335 GBP
Your task is to write a program that
• displays a menu to choose conversion from dollars to rupees, euros, or pounds.
• displays the exchange rate for the currency chosen
• prompts the user for amount of dollars to convert and displays the result
• prompts the user for amount of currency to convert to dollars and displays the result
• displays a message thanking the user.
• If user chooses a menu selection outside the range, display an error message.
You will use these two EXACT method headers to define methods that will calculate the conversions and return the results:
public static double calcUSDtoCurrency(double amountToConvert, double rate) public static double calcCurrencyToUSD(double amountToConvert, double rate)
Format each currency using the NumberFormat class. You may use the following parameters for the getCurrencyInstance method or you may research to find another way (be sure to document your sources):
Locale.US new Locale(“en”, “IN”) Locale.GERMANY new Locale(“en”, “GB”)
*Note: for those already familiar with Java programming, please keep this simple: no arrays, nothing fancy. Please use only the concepts discussed in class & in Chapters 1, 2, 3, 4, 6, and NumberFormat class.
Before starting this lab, be sure to refer to the “How to Submit Assignments” document in eLearn for proper zipping, documentation, indention, naming conventions, etc. Points may be deducted if submitted incorrectly.
Required project name: LastnameFirstname02 Required package name: chap46
Required class name: ConverterMethods
Here are 4 sample runs of the program. Your program should display something similar. User input is green text.
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do? 1
Exchange rate assumed to be 1 US Dollar to 72.28225 Indian Rupees
How many dollars would you like to convert to rupees: 123.45 You will receive Rs.8,923.24 for $123.45
How many rupees would you like to convert to dollars: 1234.56 You will receive Rs.17.08 for $1,234.56
Thank you for using the Currency Converter
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do? 2
Exchange rate assumed to be 1 US Dollar to
How many dollars would you like to convert
You will receive 112,77 € for $123.45
0.913465 Euros to euros: 123.45
How many euros would you like to convert to dollars: 1234.56 You will receive $1,351.51 for 1.234,56 €
Thank you for using the Currency Converter
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do? 3
Exchange rate assumed to be 1 US Dollar to
How many dollars would you like to convert
You will receive £102.88 for $123.45
0.833335 British Pounds to pounds: 123.45
How many pounds would you like to convert to dollars: 1234.56 You will receive $1,481.47 for £1,234.56
Thank you for using the Currency Converter
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do? 4
I'm sorry, 4 isn't a valid choice.
Helpful Notes:
Primitive type variables should begin with lowercase.
Constant variables should be all uppercase (that’s a big hint that you should include constants). To save resources, use the fewest number of variables possible. When getting user input, use just ONE variable named something like userInput or amountToConvert. Likewise, after all calculations, use just ONE variable named something like results or conversion.
If you expect a specific data type from the user, the input should be stored as that data type.
package currencyc;
import java.util.Scanner;
public class CurrencyConverter {
static Scanner s=new Scanner(System.in);
static double INR=72.282250;
static double EUR=0.913465;
static double GBP=0.833335;
static double amount;
static double dollar;
public static double calcUSDtoCurrency(double
amountToConvert,double rate)
{
amount=dollar*rate;
return amount;
}
public static double calcCurrencyToUSD(double
amountToConvert,double rate)
{
dollar=amountToConvert/rate;
return dollar;
}
public static void main(String args[])
{
int ch;
do
{
System.out.println("Currency
Converter");
System.out.println("1. Convert USD
to Indian Rupee");
System.out.println("2. Convert USD
to Euro");
System.out.println("3. Convert USD
to British Pounds");
System.out.println("What would you
like to do?");
ch=s.nextInt();
switch(ch)
{
case 1:
System.out.println("Exchange rate assumerd to be 1 US Dollar to
72.28225 Indian Rupees");
System.out.println("How many Dollars would you like to convert to
rupees:");
dollar=s.nextDouble();
amount=calcUSDtoCurrency(dollar,INR);
System.out.println("You will receive Rs."+amount+" for
$"+dollar);
System.out.println("How many Rupees would you like to convert to
Dollars:");
amount=s.nextDouble();
dollar=calcCurrencyToUSD(amount,INR);
System.out.println("You will receive $."+dollar+" for
Rs."+amount);
System.out.println("Thank you for using the Currency
Converter");
break;
case 2:
System.out.println("Exchange rate assumerd to be 1 US Dollar to
0.913465 Euros");
System.out.println("How many Dollars would you like to convert to
Euros:");
dollar=s.nextDouble();
amount=calcUSDtoCurrency(dollar,EUR);
System.out.println("You will receive € "+amount+" for
$"+dollar);
System.out.println("How many Euros would you like to convert to
Dollars:");
amount=s.nextDouble();
dollar=calcCurrencyToUSD(amount,EUR);
System.out.println("You will receive $."+dollar+" for
€"+amount);
System.out.println("Thank you for using the Currency
Converter");
break;
case 3:
System.out.println("Exchange rate assumerd to be 1 US Dollar to
0.833335 British Pounds");
System.out.println("How many Dollars would you like to convert to
British Pounds:");
dollar=s.nextDouble();
amount=calcUSDtoCurrency(dollar,GBP);
System.out.println("You will receive £ "+amount+" for
$"+dollar);
System.out.println("How many Euros would you like to convert to
Dollars:");
amount=s.nextDouble();
dollar=calcCurrencyToUSD(amount,GBP);
System.out.println("You will receive $."+dollar+" for
£"+amount);
System.out.println("Thank you for using the Currency
Converter");
break;
case 4:
System.out.println("I'm sorry, 4 isn't a valid choice.");
break;
}
}while(ch<=3);
}
}
output:
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do?
1
Exchange rate assumerd to be 1 US Dollar to 72.28225 Indian
Rupees
How many Dollars would you like to convert to rupees:
123.45
You will receive Rs.8923.2437625 for $123.45
How many Rupees would you like to convert to Dollars:
1234.56
You will receive $.17.079711824133863 for Rs.1234.56
Thank you for using the Currency Converter
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do?
2
Exchange rate assumerd to be 1 US Dollar to 0.913465 Euros
How many Dollars would you like to convert to Euros:
123.45
You will receive € 112.76725425 for $123.45
How many Euros would you like to convert to Dollars:
1234.56
You will receive $.1351.5131942657902 for €1234.56
Thank you for using the Currency Converter
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do?
3
Exchange rate assumerd to be 1 US Dollar to 0.833335 British
Pounds
How many Dollars would you like to convert to British Pounds:
123.45
You will receive £ 102.87520575 for $123.45
How many Euros would you like to convert to Dollars:
1234.56
You will receive $.1481.4690370619257 for £1234.56
Thank you for using the Currency Converter
Currency Converter
1. Convert USD to Indian Rupee
2. Convert USD to Euro
3. Convert USD to British Pounds
What would you like to do?
4
I'm sorry, 4 isn't a valid choice.