Docsity

CSIS 110 Lecture 35: String Manipulation and StringBuffer - Prof. Brian F. Hanks, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 โ€“ Lecture 35
Announcements
No class Friday. Bug Hunt assignment still due.
Comments about Testing
Your diary lab showed the importance of regression testing. Although you made changes
to the class to support two-hour tests, your changes broke some of the one-hour
functionality. For example, in at least one case it was no longer possible to make a one-
hour appointment at 5PM.
Your TriangleTest lab had other problems. Many of you had a test case like this:
Triangle t1 = new Triangle( 1, 2, 3 );
assertEquals( "scalene", t1.getTriangleVariety() );
Unfortunately, this is not correct.
When you are creating your tests, you have to think! Don't just blindly accept the value
returned by the method. You need to define your tests to test for the correct behavior, not
the actual behavior.
So, what is the correct assertEquals statement?
String and StringBuffer
Letโ€™s go back a revisit the String class. What are some methods provided by this class?
What do methods such as toUpperCase and toLowerCase do? They return new Strings โ€“
the original String is unchanged.
Strings are said to be immutable โ€“ once you have created one, you can't modify it. (You
can change what the String references, but you can't modify the String itself).
The StringBuffer class is similar to String, except that StringBuffer objects can be
modified. Let's look at some methods provided by the StringBuffer class (use API
documentation โ€“ ask what package it is in).
Constructor: unlike Strings, we can only create StringBuffer objects using the
constructor:
StringBuffer()
StringBuffer( String s )
Some useful methods:
pf3

Partial preview of the text

Download CSIS 110 Lecture 35: String Manipulation and StringBuffer - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 35

Announcements No class Friday. Bug Hunt assignment still due. Comments about Testing Your diary lab showed the importance of regression testing. Although you made changes to the class to support two-hour tests, your changes broke some of the one-hour functionality. For example, in at least one case it was no longer possible to make a one- hour appointment at 5PM. Your TriangleTest lab had other problems. Many of you had a test case like this: Triangle t1 = new Triangle( 1, 2, 3 ); assertEquals( "scalene", t1.getTriangleVariety() ); Unfortunately, this is not correct. When you are creating your tests, you have to think! Don't just blindly accept the value returned by the method. You need to define your tests to test for the correct behavior , not the actual behavior. So, what is the correct assertEquals statement? String and StringBuffer Letโ€™s go back a revisit the String class. What are some methods provided by this class? What do methods such as toUpperCase and toLowerCase do? They return new Strings โ€“ the original String is unchanged. Strings are said to be immutable โ€“ once you have created one, you can't modify it. (You can change what the String references, but you can't modify the String itself). The StringBuffer class is similar to String, except that StringBuffer objects can be modified. Let's look at some methods provided by the StringBuffer class (use API documentation โ€“ ask what package it is in). Constructor: unlike Strings, we can only create StringBuffer objects using the constructor: StringBuffer() StringBuffer( String s ) Some useful methods:

append โ€“ add some characters to the end of the StringBuffer. Many varieties of append to allow you to append various types to StringBuffers. insert โ€“ insert characters in the middle of the StringBuffer. Two parameters: index where you want to insert and the item to insert setCharAt โ€“ change the character at position index to a new value deleteCharAt โ€“ delete the character at position index toString โ€“ return a String representation of the StringBuffer replace(int start, int end, String str) โ€“ replace the characters in the substring from start upto (but not including end) with the characters from str. The StringBuffer class also includes many methods that are familiar from the String class: length, charAt, substring. But it doesn't include all of them โ€“ there is no toUpperCase, toLowerCase, or trim. Also, StringBuffer only includes a subset of the indexOf methods that are in the String class.