import java.util.Scanner; /** * ii. - This program allows money to be input in one currency and then its converted to a second * currency. The user must input the amount of money and 2 currency codes which should be USD (for dollar), * GBP (pound) and EUR (Euro). If either of the currency codes are incorrect, the program should display an error message * and end. If the user types in two currency codes that are the same, they should be trapped in a loop until the codes are different. * The money should be converted to the other currancy and the value of the monies in both currencies should be displayed. public class ChangeCurrency { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double money = 0, convertedMoney = 0; String firstCurrency = "", secondCurrency = ""; //The following are the exchance rates so euroToDollar = 1.14 means 1 euro is equal to 1.14 dollars final double euroToDollar = 1.14, euroToPound = 0.84, dollarToEuro = 0.88; final double dollarToPound = 0.74, poundToEuro = 1.19, poundToDollar = 1.35; System.out.print("Please enter the amount of money to convert: "); money = scan.nextDouble(); scan.nextLine(); /** ADD CODE HERE 1. Ask the user to input values for the first and second currency (e.g. USD for dollars, EUR for euros or GBP for pounds) and keep user trapped until the two currencies are different. 2. If the first and second currency do not have valid values (i.e.USD, EUR, GBP) then display an error message otherwise write a series of IF statements that checks the values of the first and second currencies and converts money from first to second currency storing the answer in in convertedMoney. For example, if converting from USD to EUR, convertedMoney will be money * 0.88 */ System.out.println(money + firstCurrency + " converts to " + convertedMoney + secondCurrency); }//end main }//end class
import java.util.Scanner;
/**
* ii. - This program allows money to be input in one currency and then its converted to a second
* currency. The user must input the amount of money and 2 currency codes which should be USD (for dollar),
* GBP (pound) and EUR (Euro). If either of the currency codes are incorrect, the program should display an error message
* and end. If the user types in two currency codes that are the same, they should be trapped in a loop until the codes are different.
* The money should be converted to the other currancy and the value of the monies in both currencies should be displayed.
public class ChangeCurrency
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double money = 0, convertedMoney = 0;
String firstCurrency = "", secondCurrency = "";
//The following are the exchance rates so euroToDollar = 1.14 means 1 euro is equal to 1.14 dollars
final double euroToDollar = 1.14, euroToPound = 0.84, dollarToEuro = 0.88;
final double dollarToPound = 0.74, poundToEuro = 1.19, poundToDollar = 1.35;
System.out.print("Please enter the amount of money to convert: ");
money = scan.nextDouble();
scan.nextLine();
/** ADD CODE HERE
1. Ask the user to input values for the first and second currency (e.g. USD for dollars, EUR for euros or GBP for pounds) and
keep user trapped until the two currencies are different.
2. If the first and second currency do not have valid values (i.e.USD, EUR, GBP) then display an error message
otherwise write a series of IF statements that checks the values of the first and second
currencies and converts money from first to second currency storing the answer in
in convertedMoney.
For example, if converting from USD to EUR, convertedMoney will be money * 0.88
*/
System.out.println(money + firstCurrency + " converts to " + convertedMoney + secondCurrency);
}//end main
}//end class
Step by step
Solved in 3 steps with 3 images