|
|
Laboratory 3
Using Objects
Week of 7 February, 2005
|
Purpose
- Gain experience using classes and methods as a precursor to developing our
own classes and methods
- Conduct a series of experiments using the String class.
There are two files that must be submitted, StringFun.java
and PhoneNumberFun.java.
Task 1: Make use of what is out there
- One of the great resources for the Java programming 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.5.0/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.
Use 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.
- 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 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) );
- 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'.
Recall that Java thinks of all characters as numbers. So whatever
number '3' is (it happens to be 51, but that doesn't make much difference),
5 more than that is the character '8' (or 56).
- "5" + '3' produces the string "53"
- Write down what you expect the output of the following code segment to be.
- 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. Then
submit
your program.
Task 3: A simple String program
Work on the homework
Use any remaining time to work on
homework J1.