|
|
|
 |
 |
 |
|
Car Rental
|
|
|  |
by Mike Clancy University of California Berkeley, California
 |
|
|  |
More Nifty Assignments...
Background
When you rent a car from an agency, the key ring has several pieces of information: license plate, make and year of car, and usually a special code. This code could be used for some data processing within the company's computers. This lab will practice determining that special car rental code from the license plate.
Objectives
- Practice with simple for loop, several methods in the String class, simple if statement
- Awareness of ASCII values, integer remainder operator
Assignment
The following sequence of steps is used to convert a license plate into a car rental code:
- A license plate consists of digits and capital letters of the alphabet only, for example, 607CPR21. In other words, you can type in any vanity plate you wish (with no limit on the number of digits and letters), as long as you begin and end with digits and have only letters between.
Legal examples would be: 123ABC4567, 1F24, 98ZWXTY2.
- Add up the ASCII values of the letters and the integer values of the digits: 6 + 0 + 7 + 67 + 80 + 82 + 2 + 1 = 245.
- Take this sum and map it onto a numeric representation of one of the 26 letters of the alphabet (where A's numeric representation is 0, B's numeric representation is 1, and so on). For example, 245 should map to an 11.
(Note: What you are looking for here is an operator that maps any integer -- which you get in step (b) -- onto a smaller set of integers, as discussed in class.)
- Use this number (an 11 in this case) to determine the eleventh letter in the alphabet after the letter A: eleventh letter after A is L.
- Combine the letter and the sum; the car ID so far is L245.
- Extract the substring of letters out of the original license plate -- in this case, CPR. Convert the letters to lowercase and place the letters at the end of the car ID to get the final answer: L245cpr. You should have this answer as a single String variable that you then print.
Instructions
- Prompt the user for the license plate. It should be entered with no spaces and be the only thing on the line. You must read this as a String.
- Print the run output in this format:
Plate 607CPR21 = L245cpr
- On the day this assignment is due, your instructor will give you some inputs to run. Copy the output from the console window and paste it at the end of your source code before you hand it in.
- Make sure to adhere to all style guidelines in the handout given out in class.
Teacher's Comments
I like this lab because it is fairly straightforward, and it covers a good bit of topics. For example, loop, if, ASCII knowledge, several methods in the String class, and the use of % -- all in the context of decoding a car's license plate.
This assignment is also available in PDF at the author's Web site:
Lab Exercise: Car Rental (.pdf)
Contribute
If you would like to contribute your suggestions for nifty assignments, please submit your ideas.
|
|
|
|
|
|