CPTR 124 Fundamentals of Programming
In this lab we will create some simple computational objects similar to the CircleCalculator and RectangleCalculator objects in the book.
- Becoming acquainted with the lab
As in Lab 2, quickly read over this entire lab before you begin to work on the computer. This will give you a good overview of what we will be doing today.
- Teams
You are encouraged to work with a partner for this lab. You may elect to work alone, if you prefer.
- Preparing your working folder
Make a subfolder named Lab3 within your H:\cptr124 folder.
- Preliminary Research
Go to the web and find the formulas to do the following:
- Temperature conversions: Fahrenheit to Celsius and Celsius to Fahrenheit
- Simple (not compound) interest computation: interest given the principle, interest rate, and time
- Distance conversions: Miles to kilometers and kilometers to miles.
- Class construction
In the editor, create three class definitions. Each class will be saved in its own file.
- TemperatureConversion.java
In the class TemperatureConversion, add the methods fahrenheitToCelsius() and celsiusToFahrenheit(). Each of these methods should accept a single parameter of type double. Each should return a double result that equals the correctly converted temperature value.
- InterestCalculation.java
In the class InterestCalculation, add the method computeInterest(). This method should accept a three parameters of type double. The first parameter represents the principal, the second parameter is the annual interest rate, and the third parameter is the number of years. The method should return a double result that equals the correct interest amount for the given parameters.
You will choose whether to require the client to use a percentage (for example, 3 for 3%) or a decimal (for example, 0.03 for 3%) for the rate. Please put a comment in your method indicating which way you are doing it; otherwise, a client would get an incorrect answer based on assuming the other way is being used.
- DistanceConversion.java
In the class DistanceConversion, add the methods milesToKilometers() and kilometersToMiles(). Each of these methods should accept a single parameter of type double. Each should return a double result that equals the correctly converted distance value.
Your classes must compile successfully before they can be tested.
- TemperatureConversion.java
- Testing your classes
- In the Interactions pane,
create a TemperatureConversion object and exercise its
methods. Make sure that it performs the conversions correctly.
- In the Interactions pane,
create a InterestCalculation object and exercise its
computeInterest() method. Throw at it a variety of
principal, rate, and time scenarios, and make sure that it
performs the conversions correctly.
- If any of your objects behave incorrectly, fix the problems
and retest. Continue until you can detect no problems with
your code.
- Print your interactive session that shows the tests you
performed. You do not need to include failed tests that you
subsequently fixed.
- Copy the TemperatureConversionGUI and
LabeledSlider clases from
http://perl.cs.southern.edu/repository/124Labs/Lab3.
It should be able to use your TemperatureConversion
class without modification, like the
CircleCalculatorGUI example we saw in class used our
CircleCalculator class.
Do not be alarmed; for now you do not need to understand what
makes these graphical classes work. Later on in the course,
however, you will be developing such classes!
Note: If you are using your own computer, you need to get http://perl.cs.southern.edu/repository/124Labs/oopj.jar and install it in the proper place in your Java directory. The exact location may vary depending on how you installed Java on your commputer. If you installed Java at c:\Program Files\Java\jdk_1.5.0_07, then copy oopj.jar into the c:\Program Files\Java\jdk_1.5.0\jre\lib\ext folder.
If you are using a Mac, put the oopj.jar file into the /System/Library/Java/Extensions folder.
Important note for Mac users: Mac OS 10.4.x comes with Java 1.5.0 intalled, but by default Java 1.4.2 is active. For your code to play with my oopj.jar file, you must use the 1.5.0 Java tools. The right side of the Compiler Output pane shows the version of Java that you are using. If you do not know how to change your configuration to use 1.5.0, please see me and I can tell you how to do it.
Important note: The oopj.jar file may change slightly as the semester progresses, so if you are using your own computer you should periodically check for updates. Be sure you are using the most recent version of oopj.jar when you are working on labs that require it.
- In the Interactions pane,
create a TemperatureConversion object and exercise its
methods. Make sure that it performs the conversions correctly.
- Check out
Print out each of your class definitions and a transcript of your interactive testing session. Be sure your name is on each sheet and staple them together. If you worked with a partner, please submit only one set of printouts with both names on it.
- Log out
Don't forget to log out on your lab workstation before you leave.