Lab 2
Lab 2
Lab 2
OBJECT-ORIENTED PROGRAMMING
LAB 2: ARRAY CLASS, STRING CLASS
I. Objective
After completing this tutorial, you can:
This class contains various static methods for manipulating arrays. Many of the methods have unique
specifications for each of the primitive types (boolean, byte, char, short, int, long, float, double).
Though only the methods for the primitive types are specifically discussed, many of the methods also
support an array of elements of type Object and generic types.
Example:
Example:
3. toString(ptype[ ] a)
Return the string representation of the contents of the specified array, The resulting string consists of a
list of the array's elements, separated by a comma and a space, enclosed in square brackets ("[ ]"). It
returns null if the array is null.
Example:
4. sort(ptype[ ] a)
Sorts the array into ascending order. For floating point values, the method uses the total order imposed
by the appropriate compareTo() method and all NaN values are considered equivalent and equal. This
method is not defined for boolean and short.
Example:
Example:
1. length()
A String in Java is actually an object, which contains methods that can perform certain operations on
strings. For example, the length of a string can be found with the length()method:
2. charAt(int index)
You can reference the individual characters in a string by using the method charAt() with the same
index that you would use for an array.
3. compareTo(String str)
You should not use the == operator to test whether two strings are equal. Using the == operator
determines only whether the references to the strings are the same; it does not compare the contents of
the String instances. You can compare strings by using the compareTo()method. Not only can you
determine whether two strings are equal, but you can determine which of two strings comes before the
other according to the Unicode table.
4. concat(String str)
You can use the concat() method to concatenate two strings. You can also concatenate two strings to
form another string by using the + operator.
"Hello".concat(" Wolrd");
8. trim()
Returns a string that has all leading and trailing spaces in the original string removed.
IV. Exercises
1. Write a Java program:
• Split the full name into first name and last name.