Hi everyone. I'm currently studying for a midterm I have coming up in my intro to Java course. The prof gave us practice coding questions so we can brush up on what it is we will need to know. I am stuck and I'm hoping someone here can help. I have a feeling it's an easy fix, but I can't see it..
******I am NOT asking you to do my homework for me! This is not a homework assignment. It's simply practice sheets my prof gave us to prepare for the practical side of our exam. So I'm not looking for you to do it for me. There are only TWO spots I am stuck on. ********
This is what we are required to do:
Write a program called CurrencyExchange that will either convert Canadian money to U.S.
funds or convert U.S. funds to Canadian.
This program will ask the user to enter the following three values:
1) The current exchange rate in Canadian dollars (e.g. $1.1151 Canadian dollars per US
dollar)
2) A single character to indicate which kind of currency to convert from (‘C’ for Canadian or
‘U’ for U.S.)
3) The amount of money in the specified currency to be converted (e.g. $235.50)
The program will then convert the amount of money to the other currency as follows:
- If the starting currency is in Canadian funds, divide the amount by the exchange rate
- If the starting currency is in U.S. funds, multiply the amount by the exchange rate
Next the program will ROUND the converted funds to TWO DECIMAL PLACES.
Finally, the program will report the amount of money before and after the conversion as shown
in the sample output below.
Anyways, without further ado, here is my code thus far:
/** * Program Name: CurrencyExchange.java * * Purpose: Write a program that will either convert Canadian money to U.S. funds or convert U.S. funds to Canadian. * * Coder: Bronwen * Date: Oct 5, 2014 */ import java.util.Scanner; public class CurrencyExchange { public static void main(String[] args) { // Scanner. Scanner input = new Scanner(System.in); // Get user input: current exchange rate. System.out.println("Please enter the current exchange rate in Canadian dollars, and press ENTER."); double exchangeRate = input.nextDouble(); System.out.println("Thank you. Now, which currency would you like to convert from?"); // Flush the buffer. input.nextLine(); // Get user input: Canadian or US? System.out.println("Please press C for Canadian, or U for US."); String letter = input.nextLine(); // Get user input: Amount to be converted? System.out.println("Thank you. Finally, please enter the total value which you would like converted."); double toConvert = input.nextDouble(); double endResult; // Process: use the if-else statement. if (letter == "C") endResult = toConvert / exchangeRate //<-- it's asking me to put a ; but when I do, it ALWAYS converts // resorts to using THIS calculation. HELP! else endResult = toConvert * exchangeRate; // Process: Round the converted funds to TWO decimal places. final double ROUNDED_CONVERSION = 0.005; // .005 rounds to TWO decimal places. endResult += ROUNDED_CONVERSION; endResult *= 100; // 100 for TWO decimal places. // Cast to an int to CUT OFF the excess decimals. Use a temporary variable called tempVar. int tempVar = (int)endResult; // Divide tempVar by 100 to move decimal TWO places to left. Assign result back to endResult. endResult = (double)tempVar/100; // Casting to a double to avoid the INTEGER SHARK. // Output. System.out.println("You can convert $" + toConvert + " to $" + endResult); //<-- Here, I'm having trouble getting // the program to print out the words // "Canadian" and "US" in the right spots }//end main }//end class
Problem 1:
I'm having trouble with the if-else part of this example. I have tried numerous ways of writing the statement so that it correctly decides which block of code to execute first. The problem is, I keep getting syntax errors.
Problem 2:
How do I get the program to output the last statement correctly so that it reads either "You can exchange $235.5 US for $262.61 Canadian." OR "You can exchange $262.61 Canadian for $235.5 US." depending on which conversion you have asked the program to do.
Thank you so much in advance for your help on this one!
As I said, this is an intro course. We are only one month in and this is the most programming I've done in my life. So feel free to explain to me in laymen terms how you'd go about writing the correct code. Pretend I'm stupid. Cause right now, I feel it.