ESc 014
Lab 7 -- Currency Converter
Thursday, May 23, 2002
Objectives
- To practice writing a class
- To gain experience working in the UNIX operating system
environment
Introduction
This week, you'll gain some experience working with the UNIX
operating system. Unlike Windows, UNIX is command-line based,
which means you interact with it mainly through keyboard
commands instead of the mouse. You'll be referring to your
"UNIX for Dummies" reference guide throughout this lab.
Setup
UNIX is running on the computer named blackduck which
is where you've been transferring your starter code from all
these past weeks. This time, though, you'll login to
blackduck and work directly from there. Follow
these steps:
- On your desktop is an icon for "SSH Secure Shell Client"
(NOT "SSH Secure File Transfer Client"). Launch it.
- From the File menu, click "Quick Connect".
- Type in blackduck.union.edu for the Host Name,
and then the same username you've been using
whenever you transferred files from blackduck
previously. Press the "Connect" button.
- Type in your blackduck password (not your regular email password)
- You should now be logged in to your account on
blackduck. Lots of information about the
machine's status should have quickly scrolled by on
the screen and then a prompt (a cursor) appears
at the bottom of the window. It is awaiting your
next command. The prompt where the cursor is should have
your username next to it. This is your home directory.
It's the one you always start in whenever you log into blackduck.
- Look up "ls" in your UNIX reference guide. This
is how you view the files in your current directory.
Try using the command to see what files and directories
you have in your home directory.
Note that regular files appear in black while directories
appear in either light or dark blue. (Remember, a directory
in UNIX is like a folder in Windows).
You can use options too. The "-l" option
(that's lower-case letter "l") will list details
about your files. Try it by typing "ls -l" (without
the quotes) and see what happens.
Try some of the other options listed in your guide.
- One of the directories you should see after typing "ls"
should be the "esc014pub" directory. This is the one
you've been going to in previous labs to get the starter code.
You need to "open up" this directory to get at what's inside by
using the cd command to change directories. Look it
up in your reference guide now. Then use it to move to the
"esc014pub" directory by typing
cd esc014pub
Your prompt should change to reflect the new directory you're in.
- Look at the contents of this "esc014pub" directory (using ls).
One of them should be named "lab7". That's the directory that
contains the files you need. We want to copy that folder and all of
its contents back to your home directory so you can work with
your own personal copy. You can do this with the cp command
which stands for "copy". Type
cp -R lab7 ~
This will copy the lab7 directory and everything in it to ~
(the tilde character -- it should be in the upper-left of your
keyboard underneath the esc key).
What's ~? It's UNIX shorthand for your home directory.
What's that -R for? Look up "cp" in your reference guide now.
What does it say about -R and why did I need it?
- Now that the copy is complete (we hope), we should be able to
go back to our home directory and see if it's there or not.
We're going to use "cd" again to change back to our home directory.
Look it up in your guide again and learn how to get back to your
home directory easily. Careful! Your home directory is different
from
/home
which is a directory that happens to be named "home" (as opposed
to your home directory which is named with your username).
Once you've changed directories back to your home directory, list
the contents and see if "lab7" is now a new directory in the listing.
If so, it worked! Otherwise, try to perform the copy again or
ask your instructor for help.
- Type cd lab7 to change directories into your copy of lab7
- Look at the files within it. There should be two. One is
"yen.h" and the other is "yenTest.cpp". I have provided hard copies
of both of these files so you can refer to them.
- You'll be using a text editor called "pico" to write your
code for this lab. "pico" is an easy-to-use editor described in
your reference guide starting at the bottom of p. 124. Refer to
it as you need to when writing your code.
Your Mission
Your job is to complete a program that uses a class called
yenConverter to convert dollars into Japanese
yen and back again.
Of the three files required to make this work,
you only need to write one
of them: the class file that contains the member function
definitions. I've already written the header file (yen.h)
and the tester file (yenTest.cpp) for you. You should
not alter these files. The class file you will write
will be named yen.cpp. You can begin editing a new file
with this name by typing
pico yen.cpp
at the prompt. In addition to the commands given in the
reference guide, you can also use the arrow keys on
your keyboard to move the cursor around the screen in pico.
The Details
Take a look at the code in the header file (yen.h).
Listed here in the "public" section are the
prototypes for the member functions and comments explaining
what the functions are supposed to do. The four functions
you'll be writing will use and manipulate the two variables
listed in the "private" section. convertToDollars
is a boolean variable controlling the direction of conversion:
from yen to dollars or vice versa. yenPerDollar
is the exchange rate.
Once you've studied the comments and function
prototypes in the header file,
you should be able to write the function
definitions in your new yen.cpp file. Classes
present many new changes in syntax and meaning, so
don't forget about these possible pitfalls:
- Don't forget to type in the line that includes the code from "yen.h"
- Remember to use the "double colon" notation for your functions.
- When you write the constructor, remember its job: to
simply initialize the private variables. What their
initial values should be are listed in the header file.
- Remember that member functions automatically
receive access to the private variables listed in the
header file. You don't need to redeclare them. You just
use them!
Compiling and Debugging
Once you've finished your yen.cpp file, save it (Ctrl-O)
and exit pico (Ctrl-X). The C++ compiler that blackduck
has is named g++. You can type it followed by
the names of the source code files (NOT the header file)
to compile them. You can also specify the output file with
the "-o" option (that's the lower-case letter "o"). So
typing:
g++ yen.cpp yenTest.cpp -o yen
will compile the two source code files (including the
header file because you included it, remember?)
The resulting executable file will be output to a file
called yen. If there are compiler errors, they
will show up now. Just like Visual C++, some of the
compiler errors will be direct and helpful and others
will be cryptic. It should at least tell you what line the problem
is on or near. If you get compiler errors, go back into
yen.cpp with pico and fix them. When using
pico to edit, you can press Ctrl-c
to display the line number that your cursor is on
so you can find your error. Then exit and
recompile.
If no errors occur, a new command prompt will appear
after you compile. Do an "ls" to check out your files.
A new one should appear called yen. That's the
completed program you can run! To run it, type
./yen
(that's a period, a slash, and then yen.)
Hopefully, it'll work. As always, you should
study the test file yenTest.cpp yourself
to figure out by hand what the correct output
should be.
How to turn in this lab
When you are done, you can logout of your blackduck
account by typing
logout
Then use the file transfer program to transfer
just the new file (yen.cpp) to your desktop. Upload it
to BlackBoard
and print out a hard copy. Don't forget your opening
comment block!
Remember, you will be graded on the correctness
of your implementation, neatness, presentation, style of your program
code,
thoroughness of your testing, and good use of the C++ language. It's
important
to comment your code where appropriate and to do little things like
space things
properly, use readable indentation, and also to make sure the overall
design and
logic of the program are coherent.
Return to Lab Index