RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs

lab01b
Strings

    Using Object120

  1. Open your project/file from the lab01a—lab intro: Compiling a program with BlueJ.
    (If you don't have that file, you can make a new project, and then copy/paste in these contents.)
  2. Add Object120: For much of this class, we will use an extra file to help us, “Object120.java”. To add it to your BlueJ project:
    1. Right-click, and save Object120.java somewhere on your H: drive. (Note that we'll be using this file for each project for a while.)
    2. In your already-created BlueJ project, select Edit > Add Class from File…, and then select the Object120.java you just saved. Compile, to make sure it works.
    3. Back in Pizzeria.java, we'll add two words: change “class Pizzeria {” to “class Pizzeria extends Object120 {”. This will allow us to use all the helper functions from Object120 inside Pizzeria (without needing to spell out its full name).
  3. Double-click on Object120, and in the upper-right change Source Code to Documentation. These are the signatures of all the methods defined inside Object120. We'll be calling several of those methods today.
  4. Change one character in class Pizzeria. Does your program still compile? Does it run the same? We will go around the room and play stump-the-professor with the compiler error messages.
  5. Strings

  6. In the code pad, type "hello". This is a piece of data shoe type is String (a bunch of individual characters, strung together). We've seen some Strings already, in our System.out.println statements.
  7. There are several functions that can work on Strings. Try typing the following, in Code Pad. (Remember: up-arrow in Code Pad will bring back previously-typed expressions, saving you a lot of time.)
  8. Calling these functions from a program

  9. Now let's call some of these same functions from inside a program. Go back to class Pizzeria, and let's make a new function void testStringFuncs().
    (See if you can declare an empty function and compile it. Look to see exactly what was needed for void testPizzeria().)
  10. Let's write some test cases to see if the String function are behaving as expected in some more involved situations:
          System.out.println( "Computing: " + charAt(toUpperCase("howdy"), 2) );
          System.out.println( "Expected: ???" );
        
    where you replace "???" with what you and your partner think the answer should honest-to-gosh be.
  11. Make at least two more test cases, trying to predict the answers:
  12. The following doesn't make sense. First try adding it as a test case and compiling -- this will give you a syntax error. With your partner, read the error, and see if you can figure out why it doesn't quite make sense: toUpperCase( charAt("howdy", 2) ).

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Sep.07 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme