Midterm Review
> Click or hit Control-Enter to run the code above
Checkpoint 0
You’ve come a long way in four weeks.
Midterm Goal
I want to ensure that you are ready to go on and succeed in CS 125.
Midterm Format
-
12 multiple-choice questions, mostly on code reading
-
1 "what does this code do?" free response question, graded after the exam
-
4 programming questions:
-
one using arrays,
-
one using multi-dimensional arrays,
-
one using strings,
-
and the last implementing a algorithm according to a description
-
Q3 Review: Understanding Functions
boolean g(char[] x, char[] y) {
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; i++) {
if (x[i] != y[i]) {
return false;
}
}
return true;
}
What happens if the function above is called with two empty arrays?
Q3 Review: Understanding Functions
What do we call the combination of the function name and list of argument types that is used by Java to determine which function to call?
Q3 Review: Understanding Functions
int multiply(int first, int second, int third) {
return first * second * third;
}
double multiply(double first, double second) {
return first * second;
}
System.out.println(multiply(10, 20));
What will be printed when the code above runs?
Understanding Overloading
int multiply(int first, int second, int third) {
return first * second * third;
}
double multiply(double first, double second) {
return first * second;
}
System.out.println(multiply(10, 20));
Why isn’t the method return type part of the function signature?
Warm Up: Array Sum
Sum all values in a one-dimensional array of int
values.
> Click or hit Control-Enter to run the code above
String Reverse
Write a function called reverseString
that takes a single String
argument
and returns that string reversed.
If the passed String
is null you should return null.
What’s an algorithm?
-
Go through the passed string backwards character by character
-
Append each character to the front of a new
String
-
Return the new reversed
String
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
String Equality
Write a function called areEquals
that takes two String
arguments and returns
true
if the two strings are equal (that is, they contain the same characters)
and false
otherwise.
Note that either first or second may be null
.
If either are null
you should return false
, including if both are null
.
What’s an algorithm?
-
Eliminate the
null
cases -
Go through each
String
character by character… -
…or just read the docs.
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
String Rotate Left
Write a function called rotateLeft
that takes a String
as its first argument and
a positive int
as its second argument and rotates the String
left by the given
number of characters.
What’s an algorithm?
-
Create a new character array of the same size as the input
-
Go through the passed string character by character
-
Compute the new position for that character
-
Copy the character into position in the new character array
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
Announcements
-
Midterm 0 starts tomorrow. As a reminder, you cannot drop midterm scores, and we expect you to work with the CBTF to take it this week.
-
MP2 is due today at 5PM. Good luck wrapping up! (And it’s good preparation for the midterm.)
-
Office hours all day today.
-
Daily homework introducing objects continues this week. But MP3 won’t be released until Friday.