CS160: Foundations in Programming
Command-line arguments, methods
The purpose of this lab is to gain more experience in working with
command-line arguments, strings, and methods.
Instructions
Remember the Caesar Cipher?
In this lab, you will write a command-line version of that.
Your program will take, as command-line arguments:
- first argument:
- a number, indicating how far to rotate
- all subsequent arguments:
- strings to rotate
The program will take the strings, rotate them the appropriate
amount, and print the result.
Sample runs
% javac rotate.java
% java rotate 1 Jack Applin
Kbdl Bqqmjo
% java rotate 13 I love CSU!
V ybir PFH!
% java rotate 13 pyrex
clerk
% java rotate 25 Uibolthjwjoh
Thanksgiving
% java rotate
usage: <rotation> <strings>
Requirements
- Non-alphabetic characters, such as an exclamation mark, are unchanged.
- Upper/lower case is preserved.
- An error message must be produced if no arguments are given.
- You may assume that the first argument, if given, is an integer.
Method
To accomplish this, you must define a method called rotchar,
which takes, as arguments, the rotation amount, and a character to rotate.
rotchar will return the rotated character.
For extra fame & glory
- Handle invalid numbers such as xyz or 3X.
- Decide what to do for a rotation amount <1 or >25
Submit the printout of your program to your GTA to get credit for this lab.
© 2009 CSU