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
Preparing for CBTF Quizzes
Our quizzes get more challenging from this point forward. Here’s how to prepare.
-
Review the lecture slides
-
Go over the HW125 practice problems
-
Go over the HW125 practice problems
-
Go over the HW125 practice problems
-
Go over the HW125 practice problems
Preparing for CBTF Quizzes
int earnGradeOnQuiz() {
int currentGrade = 0;
currentGrade += reviewSlides();
while (workOnHomeworkProblems()) {
currentGrade++;
}
return currentGrade;
}
Dropped Grade Policy
CS 125 has a generous dropped grade policy. However, these are not "get out of class free" cards.
-
Dropped lectures and labs are pre-excused absences intended to accommodate all the reasons you might miss class or not complete a homework problem: sickness, family emergencies, travel, religious obligations, extracurricular activities, etc.
-
As a result we don’t excuse absences. Please don’t email us about this.
-
We expect you to try to attend and complete everything. But the dropped grades are for times when circumstances intervene.
-
If you use them up skipping class just because you don’t feel like coming, and then your fish dies and you are bereft, you don’t get more.
Announcements
-
MP0 is out and due next weekend. The early deadline is this weekend on your deadline day. Please get started! Office hours start immediately after class in Siebel 0403.
-
My office hours today will be in Siebel 0403 from 1–3PM.