Difference between revisions of "Palindromes"

From CompSciWiki
Jump to: navigation, search
m
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>
<pre>
+
{{CodeBlock
 +
|Code=
 
public class Palindrome  
 
public class Palindrome  
 
{
 
{
Line 28: Line 29:
 
// Your code goes here
 
// Your code goes here
 
}
 
}
}</pre>
+
}}}
  
 
|SideSectionTitle=Mid-term Review
 
|SideSectionTitle=Mid-term Review
Line 65: Line 66:
 
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>
<pre>
+
{{CodeBlock
 +
|Code=
 
String reverse1 = ""
 
String reverse1 = ""
 
String reverse2 = ""
 
String reverse2 = ""
</pre>
+
}}
 
<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 77:
 
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>
<pre>
+
{{CodeBlock
 +
|Code=
 
str.length();
 
str.length();
</pre>
+
}}
 
<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>
<pre>
+
{{CodeBlock
 +
|Code=
 
for(int i = 0; i < testWord1.length(); i++)
 
for(int i = 0; i < testWord1.length(); i++)
 
{
 
{
 
}
 
}
</pre>
+
}}
 
<br>
 
<br>
 
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>
 
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>
Line 92: Line 96:
 
The pseudo-code below concatenates each letter of the string in reversed order.<br>
 
The pseudo-code below concatenates each letter of the string in reversed order.<br>
 
<br>
 
<br>
<pre>
+
{{CodeBlock
 +
|Code=
 
reverse1 = get the letters of the str by charAt(i) + reverse1
 
reverse1 = get the letters of the str by charAt(i) + reverse1
</pre>
+
}}
 
<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 105:
 
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>
<pre>
+
{{CodeBlock
 +
|Code=
 
for(int i = testWord2.length()-1; i >= 0; i--)
 
for(int i = testWord2.length()-1; i >= 0; i--)
 
{
 
{
 
}
 
}
</pre>
+
}}
 
<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 121: Line 127:
 
How would concatenation work now?<br>
 
How would concatenation work now?<br>
 
<br>
 
<br>
<pre>
+
{{CodeBlock
 +
|Code=
 
reverse2 = reverse2 + letters of the str by charAt(i)
 
reverse2 = reverse2 + letters of the str by charAt(i)
</pre>
+
}}
 
str = "abcde"<br>
 
str = "abcde"<br>
 
<br>
 
<br>
Line 145: Line 152:
 
     print out reservedTestWord
 
     print out reservedTestWord
 
}
 
}
</pre>
+
}}
 
<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>
Line 152: Line 159:
 
<pre>
 
<pre>
 
testWord.equals(reversedTestWord).
 
testWord.equals(reversedTestWord).
</pre>
+
}}
 
<br>
 
<br>
 
Please note that if you want to ignore capitalization for matching two strings, use <b>''equalsIgnoreCase()''</b> method.
 
Please note that if you want to ignore capitalization for matching two strings, use <b>''equalsIgnoreCase()''</b> method.
 
<br>
 
<br>
<pre>
+
{{CodeBlock
 +
|Code=
 
str.equalsIgnoreCase()
 
str.equalsIgnoreCase()
</pre>
+
}}
 
<br>
 
<br>
 
This problem was the review of 'String Methods and Debugging'.<br>
 
This problem was the review of 'String Methods and Debugging'.<br>
 
See below for the solution code.
 
See below for the solution code.
 
}}
 
}}

Revision as of 03:21, 6 December 2011

{{1010PrAD

|ProblemName=Palindromes

|Problem= This question will help you practice with string methods and for loops.

Complete the program titled Palindrome that checks if a word is a palindrome.
If the word is palindrome, print out "It's a palindrome." using System.out.println().
Otherwise, print out the reversed order of the word.

For example:

  • "deed" and "civic" are palindromes.
  • "tomato" and "mama" are not palindromes.


Find out whether the two strings provided are palindromes or not.
The program that you need to complete is provided below.

 public class Palindrome 
{
	public static void main(String[] args)
	{
		String testWord1 = "Arewenotdrawnonward,wefew,drawnonwardtonewera"
		String testWord2 = "Murderforajarofredrum"
			
		// Your code goes here
	} 
}

|SideSectionTitle=Mid-term Review

|SideSection=

Wiki trays01.jpg


|SolutionCode= String reversedWord1 = ""; String reversedWord2 = "";

for(int i = 0; i < testWord1.length(); i++) { reversedWord1 = testWord1.charAt(i) + reversedWord1; }

for(int i = testWord2.length() - 1; i >= 0; i--) { reversedWord2 = reversedWord2 + testWord2.charAt(i); }

if(reversedWord1.equalsIgnoreCase(testWord1)) { System.out.println("It's a palindrome!"); }

else { System.out.println(reversedWord1); }

|Solution=

First of all, you will need to declare two string objects to store the reversed words.
You will need to initialize the strings, which means make the strings empty.

 String reverse1 = ""
String reverse2 = "" 


Then, you will need a for loop to reverse the first word.
In order to loop through each character, you will need to set the for loop to run for the length of the string.

Getting a length of a string can be done by using

 str.length(); 


The loop will start from 0 and finish looping when i >= testWord1.length().
The increment will be 1 since we are accessing each character from the string.

 for(int i = 0; i < testWord1.length(); i++)
{
} 


Once you have the for loop set up, you will need to go through each letter of the string using charAt(i), and concatenate the letters in reverse order.
charAt(int index) is a string method that returns the ith character of a string.
The pseudo-code below concatenates each letter of the string in reversed order.

 reverse1 = get the letters of the str by charAt(i) + reverse1 


Then, you will need another for loop to reverse testWord2.
This time, we will use another approach and make the for loop start from the end.
It's strongly recommended to try this way, as it will give you a better understanding of how the for loop works.

 for(int i = testWord2.length()-1; i >= 0; i--)
{
} 


The code above starts from the length of testWord2 - 1, and ends when i becomes less than 0.
Why does it have to start from the string length - 1?
It's because there is no character at the length of the string. See the example below.
What is the length of the string, str = "apple"?
It's 5.
What is the index of the last character e in the string?
It's 4!

If you try to get a character at str.charAt(5), you will get a run-time error.
This is very important to know and you will be facing many variations of these errors later.

Now, back to the question.
We have to decrement it by 1 now, because we are reading from the last character of the string.
How would concatenation work now?

 reverse2 = reverse2 + letters of the str by charAt(i) 

str = "abcde"

reverse = reverse + charAt(i) of str from end would be
e
ed
edc
edcb
edcba

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
}
}}
<br>
One thing to note: strings <b>cannot</b> be compared using the double equals operator '=='.<br>
You will need a special method called:
<br>
<pre>
testWord.equals(reversedTestWord).
}}
<br>
Please note that if you want to ignore capitalization for matching two strings, use <b>''equalsIgnoreCase()''</b> method.
<br>
{{CodeBlock
|Code=
str.equalsIgnoreCase()
}}
<br>
This problem was the review of 'String Methods and Debugging'.<br>
See below for the solution code.
}}