Lab 3: Currency Conversion

Objective

The objective of this project is to provide experience with designing a basic Java program. This project will cover programming with conditionals and loops, retrieving user input either from the keyboard with the Scanner class and with JOptionPane factory methods, and basic arithmetic expressions. You will also learn how to develop your code incrementally and make use of classes and methods to organize your code. Coming into this lab, you should be familiar with the material from the lectures, recitations, and book readings up to this point.

Lab Setup

Before you begin, you need to create a folder "called lab03" in the CS180 folder to hold your .java files for this lab. Use the terminal to create this folder:

>> pwd
/home/yourlogin/CS180
>> mkdir lab03
>> cd lab03

Overview

For this lab, you will be developing a program which can convert between various selected currencies around the world. The following exchange rates of five other currencies to US Dollars (USD) were taken from last week and are shown below:

Currency
Rate per $1 USD
British Pound
0.630
Euro
0.744
Japanese Yen
82.996
Singapore Dollar
1.289
Chinese Yuan
6.584

Naturally, exchange rates vary on a day-to-day basis. Thus, you should not look up the current exchange rates and instead use the rates in the above table. Failure to use these rates will yield incorrect values and thus result in points lost. These rates are provided in the skeleton code provided for this lab. You should start this lab by downloading this code and understanding what we have already provided for you. The code mainly provides constants for the above currencies and conversion rates along with a method stub for your main method.

Use the constants whenever possible! For a programmer, setting values to constants yields more readable code (the same can be said about using descriptive variable, class, and method names). When arbitrary values are used in code, it is often difficult to comprehend the meaning behind expressions. Thus, the use of constants translates these arbitrary values into words which conceptually make sense to the code's reader. You should get into the habit of using constants whenever possible for this class and for future programming endeavors.

Ultimately, your program will be able to continuously ask the user for conversions between any two of the six currencies (including the US Dollar). However, we will design our program in logical steps and incrementally test each step's functionality (in general, while we will not always show you how to incrementally design your lab and project code, we recommend you always take an incremental approach to developing your code).

Step 1: Converting From USD to British Pounds

Your first step is to write a program which converts an amount of money in USD into an equivalent amount in British Pounds. Your program should be placed in a class called CurrencyConverter. The program should begin by asking the user to input an amount of money (a decimal number, in USD), computing the amount of British Pounds equivalent to the user's input, and output a statement describing the conversion. Here's an example of the program's exection:

>>java CurrencyConverter
Enter the amount of US dollars to convert: $2.5
2.50 US Dollars is equal to 1.58 British Pounds.

Your program should also ensure that the amount of USD input is a valid number. Thus, if the input is a negative amount of money, then the following should happen in your program (you may assume that the user always inputs a number, so a non-number such as "x1.4a" will not be input):

>>java CurrencyConverter
Enter the amount of US dollars to convert: $-1.4
ERROR: invalid monetary amount! (-1.40)

Note that the resulting output statement is formatted to two decimal points in both examples. You can use the java.text.DecimalFormat class to help you format your print statements (you will have to import it, just like java.util.Scanner is imported). A simple example formatting an output two two decimal places is as follows:

DecimalFormat formatTwoDecimals = new DecimalFormat("0.00");
double x = 4.3333;
System.out.println(formatTwoDecimals.format(x));

Foreward thinking: We know our ultimate goal is to convert between any of the six currencies. Thus, you should take a few minutes to think about how to organize your code. When designing your USD to British Pound conversion program, you should think about providing flexibility in your code such that you will easily be able to add in the remaining steps to support the later functions to your program (e.g., converting between any of the currencies, converting multiple times). Here are a couple things you should keep in mind:

Step Submission

Each step of the lab should be turned in electronically after its completion. You should turn in CurrencyConverter.java and any other java files you may have used (if any) in designing your program.

First, remove all the .class files from your lab folder:

>> pwd
/home/yourlogin/CS180/lab03
>> rm *.class

Next, chnge your current folder to your CS180 folder and run the turnin command:

>> cd ..
>> turnin -v -c <your lab section> -p lab03 lab03

In later steps, you will turning an updated set of files which reflects each step's completion. You should only overwrite your previous step's submission if you are satisfied with your next step's program and want the next step's program to be graded! Recall that subsequent submissions will overwrite previous submissions. Therefore, be sure to check your code thoroughly before committing to turn in each successive step. For your own bookkeeping purposes, you may also find it helpful to make copies of each completed step's code. Do not, however, turn in any code which doesn't reflect your latest completed step. Doing so may confuse the grader and hurt your grade.

If you are having trouble submitting your files, talk to your TA for help.

Step 2: Converting From USD to Arbitrary Currencies

Next, you will expand your program to support converting from USD to any of the five other currencies (i.e., not just British Pounds). In addition to asking the user to input the amount of money in USD to convert, your program should also ask the user to input which currency to convert to. If you've designed your program well, this step should only require inserting and/or modifying a few lines of code in your program. After this step, your program's execution should look like the following:

>>java CurrencyConverter
Enter the amount of US dollars to convert: $2.5
What currency do you want to convert to?
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
3
2.50 US Dollars is equal to 207.49 Japanense Yen.

If the USD amount is negative, the behavior should be the same as before. If the other currency to convert to is an invalid number, then the output should be as follows:

>>java CurrencyConverter
Enter the amount of US dollars to convert: $3.75
What currency do you want to convert to?
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
8
ERROR: invalid currency! (8)

As with the input for the USD amount, you do not have to worry about a non-number being input as the currency to convert to.

Once you complete this step, turnin your program's file(s) as you did for step 1. Remember to test your code carefully as your previous submission will be overwritten!

Step 3: Converting Between Arbitrary Currencies

Now that your program can convert from USD to any of the other five currencies, you should add the capability to convert between any two currencies (a well designed program should have the ability to convert from one currency into itself as well). To do this, replace the input for the USD amount with a request for the initial currency and the amount of money in that currency. The accompanying text should reflect the change in the currencies used. Here's an example output after step 3 is completed:

>>java CurrencyConverter
What is your starting currency?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
5
Enter the amount of Chinese Yuan to convert: 2.75
What currency do you want to convert to?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
3
2.75 Chinese Yuan is equal to 34.67 Japanense Yen.

Like before, you can assume that all input will be integers or decimal numbers as appropriate, but you should check that the numbers themselves are valid. Here's an example of entering an invalid currency type:

>>java CurrencyConverter
What is your starting currency?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
87
ERROR: invalid currency! (87)

Here's an example of entering an invalid currency amount:

>>java CurrencyConverter
What is your starting currency?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
2
Enter the amount of Euros to convert: -11
ERROR: invalid monetary amount! (-11.00)

Once you complete this step, turnin your program's file(s) as you did previously. Remember to test your code carefully as your previous submission will be overwritten!

Step 4: Converting Between Arbitrary Currencies Multiple Times

With a program which can now convert between any two of the given currencies, you should now add the ability to convert between any two currencies multiple times until the user decides to quit. You can do this by wrapping a loop around the necessary code. After each currency conversion, your program should ask the user whether or not he would like to convert currencies again (thus, your program should always convert currencies at least once). Similar to before, all erroneous input should be handled. If such a case occurs, your program should ask if the user wants to convert currencies again. Here's an example execution of the program after implementing this step:

>>java CurrencyConverter
What is your starting currency?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
-1
ERROR: invalid currency! (-1)
Would you like to convert again? (y/n) y
What is your starting currency?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
4
Enter the amount of Singapore Dollars to convert: 32.44
What currency do you want to convert to?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
5
32.44 Singapore Dollars is equal to 165.70 Chinese Yuan.
Would you like to convert again? (y/n) y
What is your starting currency?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
0
Enter the amount of US Dollars to convert: 98.5
What currency do you want to convert to?
(0) US Dollars
(1) British Pounds
(2) Euros
(3) Japanese Yen
(4) Singapore Dollars
(5) Chinese Yuan
2
98.50 US Dollars is equal to 73.28 Euros.
Would you like to convert again? (y/n) n
Good-bye!

Once you complete this step, turnin your program's file(s) as you did previously. Remember to test your code carefully as your previous submission will be overwritten!

Step 5: Using JOptionPanes for Input

Your final step to completing this lab is to replace the keyboard input and console display interface with a window-based interface using the JOptionPane factory methods. You can assume that the 'Cancel' option will not be selected for any of the input dialogs. Like step 4, your program should be able to convert currencies multiple times, but for brevity here are some example windows for one currency conversion:

Here are some example error windows:

Once you complete this finalstep, turnin your final currency conversion program's file(s) as you did previously. Remember to test your code carefully as your previous submission will be overwritten!

Grading Rubric

For this lab, your grade will be based on the last working completed step you submitted:

Step Completed
Maximum Points Awarded
Step 1
25
Step 2
50
Step 3
75
Step 4
90
Step 5
100

If you submit a step which does not work completely, partial credit may be awarded. For example, if you submit a program intended to be a working version of Step 4, but it has some errors, you may still receive 75-90 points if your program can adequately demonstrate that Step 3's requirements are satisfied. If you are unsure whether to submit a partially working step at the end of the lab, consult your lab instructor for advice.