OOP Java P1 Wk3 P2
Continuing OOP Java Week 3
In last week, we learned how to use methods and calling arguments from a method into another method. Exercise #47 to Exercise #60 focuses on how to manipulate a string or text using methods.
I am going to be concise. Exercise #47 asks to find how many characters are there in a name. Exercise #48 wants to identify what the first character’s element. Exercise #49 wants to find the element of the last character.
1
2
3
4
5
6
7
8
9
// Finding the length of a name (counting how many characters)
int length = name.length();
// Identifying the element of the first character
char letter = name.charAt(0);
// Identifying the element of the last character
char letter = name.charAt(name.length()-1);
}
I would like to review the Exercise #49, identifying the element of the last character. Supposedly the name is “Harry”, and name.length()
found 5 characters (i.e. 0, 1, 2, 3, 4). Let’s translate name.charAt(name.length()-1)
into numeric => name.charAt(5 - 1)
=> name.charAt(4)
. So if I want to find the last character of “Harry”, then I look for .charAt(4)
and it’s the fifth character.
Reversing Name
Jumping to Exercise #52, I usually use for
loop, but MOOC solutions often use while
loop. To be honest, while
loops often make more sense for these kind of problems than for
loop. Let’s see the difference.
for
loop solution:
1
2
3
4
for (int i = name.length() - 1; i > -1; i--) {
char letter = name.charAt(i);
System.out.print(letter);
} System.out.println("");
while
loop solution:
1
2
3
4
5
int i = name.length() - 1;
while (i >= 0) {
System.out.print(name.charAt(i));
i--;
} System.out.println("");
I prefer for
loop this time.
First Part & The End part
If I want to extract letters in the first part of the word, I could use this method .substring()
from String
class. It’s similar to JavaScript’s .slice()
.
1
2
3
4
5
6
7
8
9
10
11
System.out.print("Type a word: ");
String word = reader.nextLine();
System.out.print("Length of the first part: ");
int i = Integer.parseInt(reader.nextLine());
System.out.println("Result: " + word.substring(0, i));
// Type a word: cartwheel
// Length of the first part: 3
// Result: car
See how that works? .substring(beginning, ending)
, sometime can do just .substring(beginning)
, taking the first part off. Let’s extract letters from the end part of the word. This time, its a little trickier.
1
2
3
4
5
6
7
8
9
10
11
12
13
System.out.print("Type a word: ");
String word = reader.nextLine();
System.out.print("Length of the end part: ");
int i = Integer.parseInt(reader.nextLine());
int j = word.length() - i;
System.out.println("Result: " + word.substring(j));
// Type a word: example
// Length of the end part: 4
// Result: mple
Notice how there is a j
. It had to be done, because if I used .substring(i)
, I wouldn’t be able to get the end part that I want without doing any math. Let’s do the math with j
!
- want 4 characters at end of the word i.e.
mple
- if
.substring(4)
, will returnple
word.length()
= 7- 7 - 4 = 3
word.length() - i
=>word.length() - 4
=>3 = j
=>word.substring(j)
Might be little hard to get mind around to it at first.
All About Words
Skimming through from Exercise #55 to Exercise #60, same basic algorithms, different syntax in Java. I am going to cover all String’s methods.
.indexOf()
1
2
3
4
5
6
7
8
9
// word1 = glitter
//word2 = litter
int index = word1.indexOf(word2);
if (index == -1) {
System.out.println("The word '" + word2 + "' is not found in the word '" + word1 + "'.");
} else {
System.out.println("The word '" + word2 + "' is found in the word '" + word1 + "'.");
}
.indexOf()
finds the index of an element in an element. If indexOf()
cannot find the element in an element, then it will return -1
. In this case, word2
is found in word1
!
.contains()
1
2
3
4
5
6
7
8
//Using while(true) loop
if (words.contains(word)){
System.out.println("You gave the word " + word + " twice");
break;
} else {
words.add(word);
}
Coding above covers .contains()
, and what it does is like indexOf()
. There was a debate on whether’s faster..contains()
seems to be faster but it’s just more convenient. .indexOf()
is faster and is also implemented in .contains()
.
Other string methods
1
2
3
4
5
6
7
8
// often used as a boolean purpose, seeing whether word or input is empty
word.isEmpty();
// reverses the array
Collections.reverse(words)
// sorts the array in alphabetic order
Collections.sort(words)
Will need to do import java.util.Collections;
to use .reverse()
and .sort()
Comments powered by Disqus.