Objective
- Gain experience using classes and methods as a precursor to developing our
own classes and methods.
Means
- Conduct a series of experiments using the String class..
Task 1 -- Make use of what is out there
- One of the great resources for the Java program language is the website
java.sun.com. Part of this website is documentation
for the standard Java classes.
- A good starting point for that documentation is
java.sun.com/j2se/1.4.2/search.html.
This page allows you to search for information on particular classes.
- Go to the Java documentation search page and lookup the class String.
Using the resulting String documentation to record the purpose of the
String method charAt().
- Another great resource at java.sun.com is its
tutorials. The starting point for tutorials is
java.sun.com/docs/books/tutorial.
In particular, you might the find its tutorial
Learning the
Java Language helpful.
Task 2 -- String familiarity
- Download the file StringFun.java to your home
directory. If you do not remember the process, you can review the instructions
here. Use
that file to enter the code segments of interest in this lab task. Open the
file using the JCreator Java programming IDE. What values do String variables
a, b, and
c initially represent? Record your answer. Discuss your answer with your
partner.
- Add the following statements to method main().
- System.out.println("a = " + a);
- System.out.println("b = " + b);
- System.out.println("c = " + c);
- Run the revised program to determine the actual values of
the variables. If the program output is different from your
answer, figure out with your partner why.
- The class String provides two instance methods named substring() for
determining whether one string is a substring of a given string. They have the
following descriptions.
- String substring(int m)
- Returns a substring of the given string equal to the characters found at
locations m through n-1, where n is the length of the
string If m is non-sensible then an exception is thrown. In this case,
no string is returned..
- String substring(int m, int n)
- Returns a substring of the given string equal to the characters found at
locations m through n-1. If one or both of m or
n is
non-sensible then an exception is thrown. In this case, no string is
returned.
- Write down what you expect the output of the following code segment
to be. Discuss your answers with your lab partner.
- String s = "perseverance";
- System.out.println( s.substring(4,6) );
- System.out.println( s.substring(0,5) );
- System.out.println( s.substring(4) );
- System.out.println( s.substring(8, 3) );
- Enter the statements into StringFun.java. Run the revised program
to see whether your answers agree with the actual output. If they differ,
figure out why and discuss your results with your partner.
- Java overloads the + operator to perform different tasks depending upon
the types of its operands. It performs concatenation when at least one operand
is a string. Otherwise, it performs arithmetic. For example,
- 5 + 3 produces the number 8
- 5 + "3" produces the string "53"
- "5" + 3 produces the string "53"
- 5 + '3' produces the number corresponding to the character '8'
- "5" + '3' produces the string "53"
- Write down what you expect the output of the following code segment
to be. Discuss your answers with your lab partner.
- System.out.println( 1 + 2 + " buckle my shoe" );
- System.out.println( "buckle my shoe" + 1
+ 2 );
- System.out.println( "one-" + "two"+ " buckle my shoe" );
- System.out.println( "" + 1 + 2 + " buckle my shoe" );
- System.out.println( '1' + 2 + " buckle my shoe" );
- Enter the statements into StringFun.java. Run the revised program
to see whether your answers agree with the actual output. If they differ,
figure out why and discuss your results with your partner.
- Use the Java online documentation to gain familiarity with method indexOf().
After examining the documentation write down what you expect the output of the following code segment
to be. Discuss your answers with your lab partner.
- String t = "deadheaded";
- System.out.println( t.indexOf("ead") );
- System.out.println( t.indexOf("dh", 2) );
- System.out.println( t.indexOf("d", 5) );
- System.out.println( t.indexOf("d", 10) );
- Enter the statements into StringFun.java. Run the revised program
to see whether your answers agree with the actual output. If they differ,
figure out why and discuss your results with your partner or with a lab
assistant. Show the completed program to a lab assistant and
submit
it.
Task 3 -- A simple String program
- Download the file PhoneNumberFun.java.
Examine the comments of the program to see a possible problem-solving
solution. The
purpose of this program is to prompt a user for a telephone number of the form ddd–ddd–dddd, where d is a digit. The program then displays the phone number
in the following format: (ddd) ddd–dddd.
- Complete the program.
- Show the completed program to a lab assistant and
submit
it.