Practice with Functions and Strings
> Click or hit Control-Enter to run the code above
Algorithmic Thinking and Problem Solving
But First: More About Functions
But First: How Are You?
Java Method Overloading
I told you that functions couldn’t have the same name.
I lied.
> Click or hit Control-Enter to run the code above
Java Method Overloading
Java uses both the function name and the the list of arguments and types to determine which function to call.
Together they are called the function signature.
-
Does the function have the right name?
-
Does the function take the right arguments in the right order?
-
Java will also try to convert types to find a match as long as no loss of precision occurs
> Click or hit Control-Enter to run the code above
Function Input Validation
A common function design pattern is to check your inputs at the top.
Think about all the bad inputs you could possibly get from the caller.
int sum(int[] numbers) {
// check numbers to make sure it's sane
}
> Click or hit Control-Enter to run the code above
null
Arrays
What is the value of array declared below?
int[] output = null;
System.out.println(output);
-
Java has a special value that can be used to indicate that an object has not been initialized:
null
. -
null
is not a valid object: it has no properties or methods that you can use. -
Attempts to use
null
will result in an error
> Click or hit Control-Enter to run the code above
Defensive Programming 101
Always check objects for null
.
Java and other Java-like languages now also have better primitives for dealing
with null
and avoiding
the Billion Dollar
Mistake.
Casting
If you want to force Java to convert a variable from one type to another you can try applying a cast.
int i = 10;
double d = i; // This works since no information is lost
i = d; // This does not work since we'd have to throw out the fraction
i = (int) d; // But we can force Java to do it
> Click or hit Control-Enter to run the code above
Let’s Solve Some Problems!
String Reverse
Given a String
, return it in reverse order.
First, what is our algorithm? I can think of at least three ways to do this…
-
Examine each character in the input
String
-
Put it in the right place in the output
String
> Click or hit Control-Enter to run the code above
Testing
Test cases are an indispensable part of modern software development.
-
To perform a simple test, pick a input that you know the answer to and check that your function returns the expected value.
-
Try to pick cases that are hard and where you might not get the right answer—we call these corner cases.
String Search
Given a String
containing multiple words, determine if it contains a
particular word
First, what is our algorithm?
-
Split the input
String
into multiple words, which requires picking a delimiter. -
Examine each word in the resulting array and see if it is equal to the word that we are searching for
String Equality
How do we determine if two Strings are equal?
> Click or hit Control-Enter to run the code above
Object Equality
Strings in Java are objects.
Never test object equality using ==
!
Instead, using the .equals
method.
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
Announcements
-
Weekend office hours will be at similar times to last week and posted today.
-
A reminder: you can make up Q0, Q1 and Q2 this week through Saturday in the CBTF. But this is it for these late quizzes.
-
We’re working on getting the lecture videos captioned—once that’s done, they’ll be posted online.