Do these steps:
- In your workspace, make sure you have the latest homework files from
the base repository. This will create a hw08 directory in which you
will do your work.
- You may have noticed that the main method always has this signature:
public void main(String [] args)
The notation implies that main() accepts an array of strings as a
parameter.
- Modify the Palindrome.java file so that in main() it prints
out the number of elements in the array called args. You should see
0 as the value. The reason is that you are not passing any parameters to
main().
- The way you pass parameters to main() is through the command-line.
Normally when you run the program from the command-line you would invoke it
like this:
java Palindrome
If you run it like this instead:
java Palindrome hello there
The program will claim that the array args now has two elements because
the strings “hello” and “there” were passed to main() via the
command-line.
- A natural question at this point would be: “That's great, but how do I
pass parameters to main if I am using jGrasp (or some other IDE)?”
In jGrasp you can specify parameters by clicking on the Build menu and
then checking the Run Arguments check box. Doing so will put a text
box just above the code. You can type whatever parameters you want to pass
to main there (separated by spaces).
If you use a different IDE, do an internet search to find out how to accomplish
this.
- Once you have verified you can pass command-line parameters to main(),
modify Palindrome.java so that it will indicate the number of parameters
that were passed and will display all of them to the screen (one per line)
regardless of how many are provided.
- Once it is working, commit and push your changes.
NOTE: A palindrome is a string that reads the same forwards and backwards.
Begin by showing your prelab work to the instructor. Then do these steps:
- Continue working on Palindrome.java by adding a static
method named isPalindrome that accepts a single String as a
parameter and returns a boolean value. Eventually this function should
return true if the passed parameter is a palindrome and false
otherwise. For now, though, just have it display the parameter to
the screen and then return false.
- Add JavaDoc comments that describe the eventual behavior of the
method.
- In main() modify your handling of command-line parameters
so that if the number of values in args is anything but 1
it will print an error message. If exactly one command-line parameter
is given then pass it to your not-yet-working isPalindrome
method as a parameter. If the string “hello” is passed to main()
Your output should read something like this:
Evaluating string 'hello': false
Of course the value false printed should be displaying what is returned
by isPalindrome().
- NOTE: Do NOT look up an answer to this step. If you get stuck on this
step, spend at least 5 minutes thinking about possible answers before
giving up. After your thinking period spend time talking with fellow
classmates about it. See if together you can figure this out. If someone
feels like they understand they can explain the concept to others.
Consider how to state recursively the problem of determining whether a
string is a palindrome or not. Remember to state it recursively you will use
the original problem in your answer. Try filling in the blanks:
A string is a palindrome if _______________ and if ___________ is a palindrome.
Base condition: ________________________
- Once you think you have a recursive statement, go ahead and modify
isPalindrome() to match.
- Test your method using these strings:
- racecar (true)
- mm (true)
- a (true)
- hello (false)
- abcdecba (false)
- hi (false)
- Once your recursive solution is working show your work to the
instructor. Be sure to push your latest commit to your bitbucket
repository.
- If you finish early begin work on the homework assignment.