|
|
Java Student Project |
||
|
|||
Vowels |
|||
|
|
|
||
| Ability Level: | Beginning | ||
|
|
|
||
| Estimated Time: | 45 minutes | ||
|
|
|
||
| Objectives: |
· Learn
how to declare a String object
· Be able to compare Strings · Understand how to use String methods · Learn
how to convert Strings to numbers |
||
|
|
|
||
| Materials & Resources: |
|
||
|
|
|
||
| Overview: |
The String class is
used when you are creating strings that will not change, or are
immutable. In Java, Strings are first-class objects, unlike C
and C++ strings, which are simply null-terminated arrays of
8-bit characters.
There are two
classes that store and manipulate character data: String, for
constant strings, and StringBuffer, for strings that can change.
Both String and
StringBuffer provide a number of methods, including some for
inspecting substrings and getting the positions of a specific
character. In addition StringBuffer provides methods to insert
characters into
the buffer, modify a character at a specific location within the
buffer, as well as other methods. String
and StringBuffer provide several other useful ways to manipulate
string data, including concatenation, comparison, substitution,
and conversion to upper and lower case. All Java
objects have a toString( ) method to convert them to a String,
as do the wrapper classes for the primitive types. |
||
|
|
|
||
| Instructions: |
Create an array of five Strings containing the first names
of people. Write a program that counts and displays the
number of vowels in the Strings that you entered, without
regard to case. The class name is vowels. |
||
|
|
|
||
| Hints: | Some important
points to keep in mind in doing this project: I. Declaring strings A. The String object is used to hold a series of characters. The String class is defined in java.lang.String, and is automatically imported into every Java program.B. You create a string object with the keyword new, as with any object, and call the String constructor method.C. String objects can be used as an argument in the print( ) and println( ) methods. You may create arrays of String objects.II. Comparing Strings A. String variable names are references. You can't compare equality of two String objects by using the object names alone. B. The equals( ) method evaluates the contents of two String objects to determine if they are equivalent. The method returns true if the objects have identical contents. This comparison is case sensitive.C. The equalsIgnoreCase( ) method evaluates the contents of two String objects to determine if they are equivalent. The method returns true if the objects have identical contents. This comparison is not case sensitive.D. The compareTo( ) method evaluates the contents of two String objects to determine if they are equivalent. The method returns zero if the objects have identical contents, a negative number if the calling object is "less", or a positive number if the calling object is "more than" the argument. This comparison is case sensitive.III. Other String methods A. The methods toUpperCase( ) and toLowerCase( ) convert any String object to its uppercase or lowercase equivalent.B. The method indexOf( ) determines whether a specific character occurs within a string, returning the position number of the character if it does, or -1 if it does not.C. The method charAt( ) takes an integer argument and returns the character at that position number in the String object.D. The methods endsWith( ) and startsWith( ) take String arguments and return true or false if the String object ends or starts with the argument.E. The toString( ) method converts any primitive type to a String. It may also be used with any Java object. F. Concatenation is the joining of two String objects together. IV. Converting String objects to numbers A. You can use the Integer.parseInt( ) method of the Integer class to convert a string literal that contains digits to an integer value.B. To convert a String object to a double you must use first the Double.valueOf( ) to convert the String to a Double object, then the doubleValue( ) method to convert it to a double variable.
|
||
|
|
|
||
| Extra: | |||