Docsity

CSIS 110 - Lecture 10 Homework: String Methods and Programming Exercise - Prof. Brian F. H, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 โ€“ Lecture 10
Homework
Problems 3.2, 3.13, 3.16, 3.18, 3.21 - You don't need to 'explain' your answers.
Due Wednesday, Feb 2.
Programming Assignment
Do the Harvester programming exercise on page 134 of the text. Due on Monday, Feb 7.
Review - String methods/operations
Concatenation (+)
Look at program that prints out first and last names - Example1.java
First, notice the nextLine() method that is part of the Scanner class. It gets all of the
characters typed by the user until they press the enter key.
What is going on in the println statement?
Now look at Example2.java - concatenation can be used in expressions outside of
println().
Be careful: Look at Example3.java - distinction between addition and concatenation.
length() method
WordLength.java
Show how it works.
Note that the method is called using an instance identifier followed by a '.' followed by
the method. This is how you call or invoke a method.
Then enter a 'word' with leading and trailing spaces.
Introduce trim() method.
charAt() method - takes an integer index as a parameter, and returns the character in that
position in the string.
Point out the Java starts counting at 0! So, s.charAt(0) is the first character in the String. I
will try to call it the 'zeroth' character. What if index is outside of the range of valid
indices?
pf3

Partial preview of the text

Download CSIS 110 - Lecture 10 Homework: String Methods and Programming Exercise - Prof. Brian F. H and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 10

Homework Problems 3.2, 3.13, 3.16, 3.18, 3.21 - You don't need to 'explain' your answers. Due Wednesday, Feb 2. Programming Assignment Do the Harvester programming exercise on page 134 of the text. Due on Monday, Feb 7. Review - String methods/operations Concatenation (+) Look at program that prints out first and last names - Example1.java First, notice the nextLine() method that is part of the Scanner class. It gets all of the characters typed by the user until they press the enter key. What is going on in the println statement? Now look at Example2.java - concatenation can be used in expressions outside of println(). Be careful: Look at Example3.java - distinction between addition and concatenation. length() method WordLength.java Show how it works. Note that the method is called using an instance identifier followed by a '.' followed by the method. This is how you call or invoke a method. Then enter a 'word' with leading and trailing spaces. Introduce trim() method. charAt() method - takes an integer index as a parameter, and returns the character in that position in the string. Point out the Java starts counting at 0! So, s.charAt(0) is the first character in the String. I will try to call it the 'zeroth' character. What if index is outside of the range of valid indices?

subString() method - extracts sub-strings from Strings. Takes two parameters - the first one is the index of the first character position. The second parameter is another character position - it identifies the first character NOT in the substring. The length of the extracted substring is the difference of the two parameters. What if second parameter takes us past end of string? Also, version with no second parameter ***** END OF REVIEW *** indexOf() method** - finds starting position of a substring within a string. first parameter - the search string second parameter - the index to start searching from What if substring is not contained in the String? lastIndexOf() method Same thing, but starts at right end of string and goes towards beginning. *** Note that these operations only access the value of the String. None of the methods change the String's value. Thus Strings are immutable. This is unusual - most types have operations that change an object's value. We'll see this later. *** Class methods The above methods all require an instance of a String. They operate on a particular, specific String. These are called instance methods - they require an instance of the type to operate on. Java also provides class methods - these are methods that provide some sort of service related to the class, but they do not require an instance of the class. The String class provides a set of methods named valueOf. The valueOf methods produce a String from a primitive type. int aValue = -45; double pi = 3.1416; char letter = 'Q'; String thisOne = String.valueOf( aValue ); String piString = String.valueOf( pi ); String it = String.valueOf( letter );