ulrail.gif

Lab 3: Using Objects

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

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

  • One of the great resources for the Java programming language is the website http://java.sun.com. Part of this website is documentation for the standard Java classes.
     
  • A good starting point for that documentation is http://java.sun.com/j2se/1.5.0/docs/api/index.html.  In the lower-left box, you can look up any class (there are a lot of them!).  Look up the String class.
     
  • Use the resulting String documentation to record the purpose of the String method charAt() in the right-hand window frame.  Scroll down to the "method summary" section, and click on the charAt() method (it's the first one).  This will bring up more information about the charAt() method.  In particular, it takes in a single value (an int), and returns a char.  Don't worry if the full description for charAt() is a bit confusing at this point -- it will become more clear as the semester progresses.  But you should understand the basic details (i.e. what the method does) of charAt().
     
  • Another great resource at http://java.sun.com is its tutorials. The starting point for tutorials is http://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 (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 are the initial values of String variables a, b, and c? Record your answer as comments in the file.
     
  • Add the following statements to method main() (under the comment that says 'paste first code segment here').
    • 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 the end of the String.  If m is non-sensible then an error occurs. 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 error occurs. 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 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 second 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 third 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 fourth 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

  • 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.
     
  • Hint: the following code will read in a phone number from the keyboard, and extract and print out the area code
    Scanner stdin = new Scanner(System.in);
    System.out.print ("Enter the phone number in ddd-ddd-dddd format:");
    String phoneNum = stdin.nextLine();
    String areaCode = phoneNum.substring (0,3);
    System.out.println ("The area code is " + areaCode);
  • Note that the method used to read in an entire String from the keyboard is nextLine().
     
  • A sample execution of PhoneNumberFun follows (the red text is what was entered by the user).
    Phone number conversion
    	
    Enter the phone number in ddd-ddd-dddd format:
    434-867-5309
    The area code is 434
    The local number is 867-5309
    The reformatted phone number is (434) 867-5309
    
  • Complete the program.  The comments in the PhoneNumberFun.java file show the steps to be done.
     
  • Recall that a program legend is a one line print statement that says what this program does (such as "Phone number conversion").
     
  • When you are finished, submit the program.

Work on the homework

Use any remaining time to work on homework 1.

spacer.gif
spacer.gif footer-middle.gif spacer.gif