Difference between revisions of "Palindromes"
(4 intermediate revisions by 2 users not shown) | |||
Line 18: | Line 18: | ||
The program that you need to complete is provided below.<br> | The program that you need to complete is provided below.<br> | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
public class Palindrome | public class Palindrome | ||
{ | { | ||
Line 28: | Line 29: | ||
// Your code goes here | // Your code goes here | ||
} | } | ||
− | } | + | } |
+ | }} | ||
|SideSectionTitle=Mid-term Review | |SideSectionTitle=Mid-term Review | ||
Line 65: | Line 67: | ||
You will need to <b>initialize</b> the strings, which means make the strings empty. <br> | You will need to <b>initialize</b> the strings, which means make the strings empty. <br> | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
String reverse1 = "" | String reverse1 = "" | ||
String reverse2 = "" | String reverse2 = "" | ||
− | + | }} | |
<br> | <br> | ||
Then, you will need a for loop to reverse the first word. <br> | Then, you will need a for loop to reverse the first word. <br> | ||
Line 75: | Line 78: | ||
Getting a length of a string can be done by using <br> | Getting a length of a string can be done by using <br> | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
str.length(); | str.length(); | ||
− | + | }} | |
<br> | <br> | ||
The loop will start from 0 and finish looping when i >= testWord1.length(). <br> | The loop will start from 0 and finish looping when i >= testWord1.length(). <br> | ||
The increment will be 1 since we are accessing each character from the string. <br> | The increment will be 1 since we are accessing each character from the string. <br> | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
for(int i = 0; i < testWord1.length(); i++) | for(int i = 0; i < testWord1.length(); i++) | ||
{ | { | ||
} | } | ||
− | + | }} | |
<br> | <br> | ||
− | Once you have the for loop set up, you will need to go through each letter of the string using < | + | Once you have the for loop set up, you will need to go through each letter of the string using <b>''charAt(i)''</b>, and concatenate the letters in reverse order. <br> |
− | charAt( | + | charAt(int index) is a string method that returns the i<sup>th</sup> character of a string.<br> |
− | The | + | The pseudo-code below concatenates each letter of the string in reversed order.<br> |
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
reverse1 = get the letters of the str by charAt(i) + reverse1 | reverse1 = get the letters of the str by charAt(i) + reverse1 | ||
− | + | }} | |
<br> | <br> | ||
Then, you will need another for loop to reverse testWord2. <br> | Then, you will need another for loop to reverse testWord2. <br> | ||
Line 100: | Line 106: | ||
It's strongly recommended to try this way, as it will give you a better understanding of how the for loop works. <br> | It's strongly recommended to try this way, as it will give you a better understanding of how the for loop works. <br> | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
for(int i = testWord2.length()-1; i >= 0; i--) | for(int i = testWord2.length()-1; i >= 0; i--) | ||
{ | { | ||
} | } | ||
− | + | }} | |
<br> | <br> | ||
The code above starts from the length of testWord2 - 1, and ends when i becomes less than 0.<br> | The code above starts from the length of testWord2 - 1, and ends when i becomes less than 0.<br> | ||
Line 114: | Line 121: | ||
It's 4! <br> | It's 4! <br> | ||
<br> | <br> | ||
− | If you try to get a character at str.charAt(5), you will get a run-time error. <br> | + | If you try to get a character at str.charAt(5), you will get a <b>run-time error</b>. <br> |
This is very important to know and you will be facing many variations of these errors later.<br> | This is very important to know and you will be facing many variations of these errors later.<br> | ||
<br> | <br> | ||
Line 121: | Line 128: | ||
How would concatenation work now?<br> | How would concatenation work now?<br> | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
reverse2 = reverse2 + letters of the str by charAt(i) | reverse2 = reverse2 + letters of the str by charAt(i) | ||
− | + | }} | |
str = "abcde"<br> | str = "abcde"<br> | ||
<br> | <br> | ||
Line 136: | Line 144: | ||
Once you have the two words reversed, you will need a condition statement to check if they are palindromes. | Once you have the two words reversed, you will need a condition statement to check if they are palindromes. | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
if testWord == reversedTestWord | if testWord == reversedTestWord | ||
{ | { | ||
Line 145: | Line 154: | ||
print out reservedTestWord | print out reservedTestWord | ||
} | } | ||
− | + | }} | |
<br> | <br> | ||
One thing to note: strings <b>cannot</b> be compared using the double equals operator '=='.<br> | One thing to note: strings <b>cannot</b> be compared using the double equals operator '=='.<br> | ||
You will need a special method called: | You will need a special method called: | ||
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
testWord.equals(reversedTestWord). | testWord.equals(reversedTestWord). | ||
− | + | }} | |
<br> | <br> | ||
− | + | Please note that if you want to ignore capitalization for matching two strings, use <b>''equalsIgnoreCase()''</b> method. | |
<br> | <br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
str.equalsIgnoreCase() | str.equalsIgnoreCase() | ||
− | + | }} | |
<br> | <br> | ||
+ | This problem was the review of 'String Methods and Debugging'.<br> | ||
See below for the solution code. | See below for the solution code. | ||
}} | }} |
Latest revision as of 03:24, 6 December 2011
Back to the Program-A-Day homepage
ProblemThis question will help you practice with string methods and for loops.
public class Palindrome { public static void main(String[] args) { String testWord1 = "Arewenotdrawnonward,wefew,drawnonwardtonewera" String testWord2 = "Murderforajarofredrum" // Your code goes here } } |
Mid-term Review | |
---|---|---|
SolutionFirst of all, you will need to declare two string objects to store the reversed words. String reverse1 = "" String reverse2 = ""
str.length();
for(int i = 0; i < testWord1.length(); i++) { }
reverse1 = get the letters of the str by charAt(i) + reverse1
for(int i = testWord2.length()-1; i >= 0; i--) { }
reverse2 = reverse2 + letters of the str by charAt(i) str = "abcde" Once you have the two words reversed, you will need a condition statement to check if they are palindromes.
if testWord == reversedTestWord { Then, print out success } else { print out reservedTestWord }
testWord.equals(reversedTestWord).
str.equalsIgnoreCase()
| ||
Code |