Notes about Strings

advertisement
Late February 2014
Notes involving Chapter 8 Strings:
The students should have started on Chapter 8 below last week and posted work on their
academic sites. Encourage students to work together, but post own work.
Students have been working on Strings. PLEASE LET THEM KNOW THAT THE
MIDTERM EARLY NEXT MONTH WILL ONLY INVOLVE MATERIAL UP
TO AND INCLUDING CHAPTER 8 STRINGS!
Much of what comes in class now are exercises and assignments that help tie all the
previous material together. Like the ones for today listed below!
Please post this URL on the board! (The hardest part will be getting the file paths
correct on each student's program. Ask for student volunteers to help!)
http://poudre.us/Words.docx
These assignments were from last week (and are here in case a student still needs help on
the work from last week): http://staffweb.psdschools.org/sdurkin/jnotes.html#eight
The next assignment is to read Chapter 8, Strings and things, and follow the directions on
the first deliverables file as it contains what you should do as you read the chapter.
Below are solutions for much of that work. You may share as needed.
Thursday, February 20
Ask students what problem(-s) they are stuck on and share the information that follows as
appropriate. (These pages are also linked at
(http://tinyurl.com/fossilJava008) and you are welcome to share this
resource with the students.)
Solution to backwards:
The idea is to traverse the original string – but starting at the end of the string and going
to the beginning.
What is the last index of ANY string? It is ALWAYS ONE LESS than the length of
the string.
The string "Fossil" has six characters. Therefore, its length is 6.
Remember, characters within a string are indexed starting with zero (0).
F
o
s
s
i
0
1
2
3
4
l
5
Therefore, you can use the length of the string to find out what the last index is and then
just use a loop to go from that last index DOWN to zero, printing each char as you go:
public static void printBackwards(String s) {
int index = s.length()-1;
while (index >= 0) {
System.out.print(s.charAt(index));
index = index – 1; //index goes DOWN, we are going backwards!
}
}
Solution for countLetters method:
Wrap the following in a method that can take in the string AND the character to look for.
String fruit = "banana";
int length = fruit.length();
int count = 0;
int index = 0;
while (index < length) {
if (fruit.charAt(index) == 'a') {
count = count + 1;
}
index = index + 1;
}
System.out.println(count);
Solution
(you don't need to do the last "indexOf" part of the problem, unless you want to):
public static void countLetters(String s, char c) {
int length = s.length();
int count = 0;
int index = 0;
while (index < length) {
if (s.charAt(index) == c) {
count = count + 1;
}
index = index + 1;
}
System.out.print("The word " + s + " contains the character ");
System.out.println(c + " this many times " + count);
}
Solution to Exercise #7 in Chapter 8:
Exercise 7 A friend of yours shows you the following method and
explains that if number is any two-digit number, the program will
output the number backwards. He claims that if number is 17, the
method will output 71.
Is he right? If not, explain what the program actually does and
modify it so that it does the right thing.
int number = 17;
int lastDigit = number%10;
int firstDigit = number/10;
System.out.println(lastDigit + firstDigit);
The problem is the above code simply adds the two number together, which is 8. Not
what we wanted. Instead, to get the desired output … one way would be to place the two
numbers in separate print statements:
System.out.print (lastDigit);
System.out.println(firstDigit);
Solutions to Chapter 8 B deliverables:
Chapter 8 – Interesting Objects
Draw a box around a text string
This assignment is practice in writing Java methods. Other programming languages use
the words "function", "procedure", or "subroutine". In Java, the correct word is "method"
because Java is an object-oriented language and methods can be attached to objects.
This assignment has three steps.
Step #1:
Write a small main() method that calls a boxLine() static class method with one integer
parameter. The parameter is the length of a horizontal line. For example, the call
boxLine(10) would print the following output:
+----------+
Test your boxLine()
method by calling it from your main() method with various
parameters greater than or equal to zero.
SOLUTION:
public static void boxLine(int n){
System.out.print("+");
int index = 0;
while (index < n) {
System.out.print("-"); //if n is 10, then index goes from 0 to 9, which is 10
index = index + 1;
}
System.out.println("+");
}
public static void main(String [] args){
boxLine(10);
}
Step #2:
Now write a boxText() method that takes a string parameter and prints a box around the
string. For example, the call
boxText("Hello. How are you?");
would print the following output:
+-------------------+
|Hello. How are you?|
+-------------------+
Test your boxText() method by calling it from your main() method with various
parameters. The boxText() method must call the boxLine() method to draw the
horizontal lines. You should put a blank line before the first horizontal line.
SOLUTION:
public static void boxText(String s){
System.out.println(); // blank line
int length = s.length(); // find the length!
boxLine(length);
System.out.print("|" + s + "|");
boxLine(length);
}
public static void main(String [] args) {
//boxLine(10); // comment out old line of code
boxText("Help! I am inside a box!");
}
string
Step #3:
Now write a boxInfo() method that takes two parameters. The first parameter is a string
with a person's name. The second parameter is an integer with that person's age. The
boxInfo() method calls the boxText() method with a message string created from the
person's name and age. For example, the call
boxInfo("Mickey Mouse", 84);
will call the boxText() method with the following string:
boxText("Mickey Mouse is 84 years old.");
and the output from boxText() would be:
+-----------------------------+
|Mickey Mouse is 84 years old.|
+-----------------------------+
Test your boxInfo() method by calling it from your main()
method with various names
and ages. Use interesting names and correct ages. For example, how old is
Tyrannosaurus rex? (Mickey Mouse made his screen debut on 18 November 1928 in
"Steamboat Willie", a short black-and-white animated musical.)
public static void boxInfo(String name, int age) {
//just concatenate everything
String sentence = name + " is " + age + " years old.";
boxText(sentence);
}
public static void main(String [] args) {
boxInfo("Mickey Mouse", 84);
}
Download