home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lab01b
Strings
and setup: H: drive; Object120.java
- Mount your H: drive:
-
Mapping your H: drive (windows)
-
Mapping your H: drive (mac)
- Alternately: instead of having your H: drive appear
on the desktop, another option is
manually transferring files to/from H: drive via myRU.
- If you want to map your H: drive from off-campus
(or, use any campus-computer resource from off-campus),
you can connect your computer to a campus
"virtual private network":
Installing VPN (windows)
- We will take a few minutes to finish and
discuss lab01a—lab intro: Compiling a program with BlueJ.
You'll presumably want to Save As… your project
onto your H: drive.
If you can't locate your file from last time, you can copy/paste
this file into BlueJ.
Using Object120
-
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:
- Right-click, and save Object120.java
somewhere on your H: drive.
(Note that we'll be using this file for each lab and
homework project,
for about ten weeks.)
-
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.
-
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).
-
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.
Strings
-
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.
-
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.)
-
"hello" + "there"
-
"hello " + "there"
-
Object120.length("hello")
-
Object120.length("Z")
- Can you make a String whose length is 0? Try it!
-
Object120.charAt("howdy", 2)
-
Object120.charAt("howdy", 1)
Tip:In the code pad, pressing up-arrow key will
scroll through previous expressions — very handy when
you want to make slight changes.
-
Object120.charAt("howdy", 0)
We say that the first character of
a string is “at index 0”.
(Sometimes beginning programmers want to
say “the first character” to mean what most humans
would call the second character; this is too confusing.
So we'll stick with saying that the “second character”
is “at index 1”.)
-
Object120.charAt("howdy", 17)
-
Object120.toUpperCase("howdy")
-
Object120.toUpperCase("hOwDy")
-
Object120.substring( "supercalifragilisticexpialidocious", 5, 10 )
Calling these functions from a program
-
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().)
-
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.
-
Make at least two more test cases, trying to predict the answers:
-
charAt( substring( "supercalifragilisticexpialidocious", 5, 10 ), 2 )
-
charAt( toUpperCase(substring( "supercalifragilisticexpialidocious", 5, 10 )), 2 )
-
Note:
When calling a function like charAt:
In the code-pad, we need to type its full
name, “Object120.charAt”.
But inside your class, you may omit it
(though certainly may still include it,
if that consistency makes more sense to you).
If you're curious about the Java's reasoning behind this:
We're allowed to omit the full-name inside the class, because
(a) code inside class Pizzeria is already on a
first-name basis with all functions in the same class;
you don't need to specify the full name,
and
(b) because “class Pizzeria extends Object120”,
everything that is Object120 knows
is also known by Pizzeria1.
-
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) ).
1
In object-oriented lingo,
“Pizzeria extends Object120” means
“Every Pizzeria is an Object120,
although not &vv;.”.
↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs