|
|
Laboratory 3
Using Objects
Week of Monday, 5 September, 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.
Remember that if you don't finish during the lab time, that's fine -- you
need to
submit the files you have done so far, get a
lab
extension, and then complete (and submit) the lab within the next 24 hours.
Task 1: Make use of what is out there
Task 2: String familiarity
- Download the file StringFun.java to your home
directory (right-click on the file and select "Save As"). Use
that file to enter the code segments of interest in this lab task. Open the
file using the JCreator. What values do String variables
a, b, and
c initially represent? Record your answer as comments in the file.
- Add the following statements to method main().
- System.out.println("a = " + a);
- System.out.println("b = " + b);
- System.out.println("c = " + c);
- Recall that when you want to print out multiple things via a
System.out.println() statement, you separate them by using the plus sign.
- 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.
- The variables declared within the parenthesis are called parameters.
When a method is defined, the types of the parameters are listed (in this
case, they are both ints), as shown above. When calling the method is
called, as shown below, the types are not listed - only the value for the
variables is provided.
- Write down what you expect the output of the following code segment to
be. Record the answer as comments in your StringFun.java file.
- 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 (under the comment that
says 'paste first code segment here'). Run the revised program
to see whether your answers agree with the actual output. If they differ,
figure out why (ask a TA if you are unsure).
- 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"
- Record (as comments in the StringFun.java file) 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 (under the comment that
says 'paste second code segment here'). Run the revised program
to see whether your answers agree with the actual output. If they differ, try
to figure out why (as a TA if you are unsure).
- 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.
- 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 (under the comment that
says 'paste third code segment here'). Run the revised program
to see whether your answers agree with the actual output. If they differ,
figure out why.
- When you are done,
submit
your program.
Task 3: A simple String program
Work on the homework
Use any remaining time to work on
homework J1.