Introduction to Java
Theoretical Lab 1
CS 61BL Summer 2024
Announcements
CS 61BL Summer 2024
Content Review (Java Basics)
CS 61BL Summer 2024
Quick Java Basics
public class Hello {
public static void main(String[] args) {
System.out.println(“Hello world!”);
}
}
CS 61BL Summer 2024
Worksheet (Q1)
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
CS 61BL Summer 2024
1 Our First Java Program
class Dog {
public static int cuteness = 100;
public Dog(String name, int size) { … }
public void bark(int loudness) { … }
public static void evolve(int newCuteness) {
System.out.println("Becoming cuter...");
cuteness = newCuteness;
}
}
CS 61BL Summer 2024
1 Our First Java Program
class Dog {
public static int cuteness = 100;
public Dog(String name, int size) { … }
public void bark(int loudness) { … }
public static void evolve(int newCuteness) {
System.out.println("Becoming cuter...");
cuteness = newCuteness;
}
}
CS 61BL Summer 2024
1 Our First Java Program
class Dog {
public static int cuteness = 100;
public Dog(String name, int size) { … }
public void bark(int loudness) { … }
public static void evolve(int newCuteness) {
System.out.println("Becoming cuter...");
cuteness = newCuteness;
}
}
CS 61BL Summer 2024
Content Review (Conditionals, Loops, and Arrays)
CS 61BL Summer 2024
While Loops
(Taken from lab spec) Pay close attention to when the while loop terminates.
CS 61BL Summer 2024
For Loops
CS 61BL Summer 2024
break and continue keywords
CS 61BL Summer 2024
Arrays in Java
CS 61BL Summer 2024
Arrays
Structure for declaration: <type of object in the array>[] <variable name>;
One way to instantiate:
Declare and instantiate at once:
CS 61BL Summer 2024
Worksheet (Q2 & Q3)
CS 61BL Summer 2024
2 Mystery
29
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
30
3
0
4
6
3
2
inputArray
k
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
31
3
0
4
6
3
2
inputArray
k
4
x
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
32
3
0
4
6
3
2
inputArray
k
4
x
2
answer
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
33
3
0
4
6
3
2
inputArray
k
4
x
2
answer
3
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
34
3
0
4
6
3
2
inputArray
k
4
x
2
answer
3
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
35
3
0
4
6
3
2
inputArray
k
4
x
2
answer
3
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
36
3
0
4
6
3
2
inputArray
k
4
x
2
answer
3
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
37
3
0
4
6
3
2
inputArray
k
4
x
2
answer
4
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
38
3
0
4
6
3
2
inputArray
k
4
x
2
answer
4
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
39
3
0
4
6
3
2
inputArray
k
4
x
2
answer
4
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
40
3
0
4
6
3
2
inputArray
k
3
x
2
answer
4
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
41
3
0
4
6
3
2
inputArray
k
3
x
4
answer
4
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
42
3
0
4
6
3
2
inputArray
k
3
x
4
answer
5
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
43
3
0
4
6
3
2
inputArray
k
3
x
4
answer
5
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
44
3
0
4
6
3
2
inputArray
k
3
x
4
answer
5
index
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
45
Return: 4
The returned value is the index of the smallest value in the array that occurs at or after index k.
What does this return when the input array is [3, 0, 4, 6, 3] and k is 2?
What does mystery do, in plain English?
CS 61B Summer 2023
2 Mystery
Use mystery to sort a given array of integers in ascending order.
Hint: Can you use what mystery returns to swap integers within the array?
CS 61BL Summer 2024
2 Mystery
Use mystery to sort a given array of integers in ascending order.
Hint: Can you use what mystery returns to swap integers within the array?
CS 61BL Summer 2024
2 Mystery
Use mystery to sort a given array of integers in ascending order.
Hint: Can you use what mystery returns to swap integers within the array?
CS 61BL Summer 2024
2 Mystery
Use mystery to sort a given array of integers in ascending order.
Hint: Can you use what mystery returns to swap integers within the array?
CS 61BL Summer 2024
2 Mystery
Use mystery to sort a given array of integers in ascending order.
Hint: Can you use what mystery returns to swap integers within the array?
CS 61BL Summer 2024
2 Mystery (Extra)
Implement mystery recursively.
CS 61BL Summer 2024
2 Mystery (Extra)
Implement mystery recursively.
CS 61BL Summer 2024
2 Mystery (Extra)
Implement mystery recursively.
the index of min. element seen so far.
the index of the next element to examine.
If examined element is smaller, change minIndex
Otherwise, don’t change minIndex
CS 61BL Summer 2024
3 Two Sum
int[] twoSum(int[] nums, int target) {
for (int i = ____; ____________; ____________) {
for (int j = ____; ____________; ____________) {
if (____________________________) {
return ______________________________;
}
}
}
}
Given an array of numbers nums, find two numbers in nums such that adding them would
result in the value target. Assume that only one such pair exists, and there is
always a pair that fulfills the condition. Return the numbers as an array with two
terms.
CS 61BL Summer 2024
3 Two Sum
int[] twoSum(int[] nums, int target) {
for (int i = ____; ____________; ____________) {
for (int j = ____; ____________; ____________) {
if (____________________________) {
return ______________________________;
}
}
}
}
Algorithm idea: iterate through the array, and for every element in the array, sum that element with all combination of elements. If the result equals target, then we can return those two elements.
Better Algorithm Idea: iterate through the array, and for every element in the array, sum that element with all combination of elements to the right. If the result equals target, then we can return those two elements.
CS 61BL Summer 2024
3 Two Sum
int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (____________________________) {
return ______________________________;
}
}
}
}
Algorithm idea: iterate through the array, and for every element in the array, sum that element with all combination of elements to the right. If the result equals target, then we can return those two elements.
CS 61BL Summer 2024
3 Two Sum
int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{nums[i], nums[j]};
}
}
}
return new int[]{0, 0};
}
Algorithm idea: iterate through the array, and for every element in the array, sum that element with all combination of elements to the right. If the result equals target, then we can return those two elements.
CS 61BL Summer 2024
3 Two Sum
int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{nums[i], nums[j]};
}
}
}
}
Algorithm idea: iterate through the array, and for every element in the array, sum that element with all combination of elements to the right. If the result equals target, then we can return those two elements.
CS 61BL Summer 2024
3 Two Sum
int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{nums[i], nums[j]};
}
}
}
}
Note: This is a common problem used in interviews, but this approach isn’t optimized. Later in the course, you’ll learn how to use data structures to make your code even faster!
CS 61BL Summer 2024
Worksheet (Q4)
CS 61BL Summer 2024
4 Reversals
In this problem, we are trying to implement a reverseString method. This method
takes in a string and reverses it.
1 public static String reverseString(String s) {
2 // Implementation to be filled in
3 }
(a) For each of the test cases we have written below, write down whether it’s a
correct and useful test case, and briefly explain why.
CS 61BL Summer 2024
4 Reversals
This wouldn’t work because we didn’t actually call the method reverseString. Key takeaway: always call the method you are trying to test!
This works well and tests the reversal of a given word.
This would give a compiler error: the method reverseString is supposed to take in a String, while an int is given.
This works well and tests whether reversals work for special characters.
This works well and tests whether reversals work for empty strings - a common edge case.
This would also work as a test case.
CS 61BL Summer 2024
4 Reversals
We have the current implementation for reverseString.
1 public static String reverseString(String s) {
2 return s.substring(1) + s.charAt(0);
3 }
This test would fail because our implementation would return "elloH".
This test would fail because our implementation would be trying to access index 1 for a string of length 0, resulting in an IndexOutOfBoundsException.
This test would pass because our implementation would return "aaaab", which is the same as the expected answer. The key takeaway from this is that buggy implementations sometimes won’t be caught if your test cases are not comprehensive.
CS 61BL Summer 2024
4 Reversals
We will now write a correct implementation for reverseString.
public static String reverseString(String str) {
________________________________
for (_________________; _________________; _________________) {
_________________
}
________________________________
}
CS 61BL Summer 2024
4 Reversals
We will now write a correct implementation for reverseString.
1 public static String reverseString(String str) {
2 String reversed = "";
3 for (int i = str.length() - 1; i >= 0; i--) {
4 reversed += str.charAt(i);
5 }
6 return reversed;
7 }
CS 61BL Summer 2024
Worksheet (Q5, Extra)
CS 61BL Summer 2024
5 Rotate
We’d like to implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, wrapping to the other side as necessary.
Negative k means shifting towards the left.
public static int[] rotate(int[] A, int k) {
int rightShift = _______________________________;
if (_________________________) {
_____________________________________________;
}
int[] newArr = ____________________________________;
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61BL Summer 2024
5 Rotate
We’d like to implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, wrapping to the other side as necessary.
Negative k means shifting towards the left.
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (_________________________) {
_____________________________________________;
}
int[] newArr = ____________________________________;
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61BL Summer 2024
5 Rotate
We’d like to implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, wrapping to the other side as necessary.
Negative k means shifting towards the left.
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = ____________________________________;
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61BL Summer 2024
5 Rotate
We’d like to implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, wrapping to the other side as necessary.
Negative k means shifting towards the left.
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61BL Summer 2024
5 Rotate
We’d like to implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, wrapping to the other side as necessary.
Negative k means shifting towards the left.
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (int i = 0; i < A.length; i++) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61BL Summer 2024
5 Rotate
We’d like to implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, wrapping to the other side as necessary.
Negative k means shifting towards the left.
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (int i = 0; i < A.length; i++) {
int newIndex = (i + rightShift) % A.length;
_____________________________________________;
}
return newArr;
}
CS 61BL Summer 2024
5 Rotate
We’d like to implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, wrapping to the other side as necessary.
Negative k means shifting towards the left.
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (int i = 0; i < A.length; i++) {
int newIndex = (i + rightShift) % A.length;
newArr[newIndex] = A[i];
}
return newArr;
}
CS 61BL Summer 2024
bit.ly/su24-tlab-1
CS 61BL Summer 2024