The main goals for this lab are for you to get more comfortable implementing interfaces and working with classes.
This lab is a paired programming assignment. What exactly does that mean? You will be working in pairs on the CS lab computers. Each pair will be working on one computer. One person will be the driver and the other person will be the navigator. Here is the rule: the driver controlls the lab computer, but the driver can only type what the navigator tells them to type. For this to work well, each pair should be constantly talking among themselves. After each problem, you will switch roles, the navigator will become the driver and the driver will become the navigator.
Partners You must work with your partner from last week. If they are not here, please see your instructor.
Before leaving the lab, make sure you fill out the attendance sheet and go through your answers with a TA or instructor. You will not get full credit otherwise.
1 class Bounds {
2 private int min;
3 private int max;
4
5 public Bounds(int min, int max) {
6 this.min = min;
7 this.max = max;
8 // A) Draw the stack diagram here
9 }
10
11 public void extend(Bounds other) {
12 if (other.min < this.min) {
13 this.min = other.min;
14 }
15 if (other.max > this.max) {
16 this.max = other.max;
17 }
18 // C) Draw the stack diagram here
19 }
20
21 public String toString() {
22 return "[" + this.min + ", " + this.max + "]";
23 // B) Draw the stack diagram here
24 }
25
26 public static Bounds Add(Bounds a, Bounds b) {
27 int min = a.min + b.min;
28 int max = a.max + b.max;
29 // D) Draw the stack diagram here
30 return new Bounds(min, max);
31 }
32
33 public static void main(String[] args) {
34 Bounds b1 = new Bounds(-4, 7);
35 Bounds b2 = new Bounds(2, 9);
36 System.out.println("Bounds 1: " + b1);
37 System.out.println("Bounds 2: " + b2);
38
39 b1.extend(b2);
40 System.out.println("Extended: " + b1);
41
42 Bounds c = Bounds.Add(b1, b2);
43 System.out.println("Add: " + c);
44 // E) Draw the stack diagram here
45 }
46 }
Imagine we ran this program called Bounds
. Below, write down the lines numbers in the order that they are executed.
What is the output of the above program?
For each iteration of the loop in binarySearch(int x, int[] lst)
, show the index values for low, high, and mid, and the value stored at location mid. What will be returned by this function call?
int x = 67;
int[] lst = {10, 12, 14, 21, 37, 44, 45, 46, 58, 67, 99};
0 1 2 3 4 5 6 7 8 9 10
low | high | mid | L[mid]
- - - - - - - - - - - - -
| | |
| | |
| | |
| | |
For each iteration of the loop in binarySearch(int y, int[] lst)
, show the index values for low, high, and mid, and the value stored at location mid. What will be returned by this function call?
int y = 4;
int[] lst = [10, 12, 14, 21, 37, 44, 45, 46, 58, 67, 99];
0 1 2 3 4 5 6 7 8 9 10
low | high | mid | L[mid]
- - - - - - - - - - - - -
| | |
| | |
| | |
| | |
Write a function to return the index of the largest item in a given list.
// Input: An array of integers
// Returns: The index of the largest item in the list,
// -1 if the list is empty
int indexOfLargest(int[] ls) {
// complete this function
}
Write a function to return the first index of the item that appears the most in a given list. For this problem you can assume that there is just one mode in a list. To keep things easier, you can also assume that there are only positive values in our list.
// Input: An array of integers
// Returns: The first index of the item in the list that appears the most frequently,
// -1 if the list is empty
int indexOfMode(int[] ls) {
// complete this function
}
Now, imagine we asked you to find the most common character in a String.
Below, write out an algorithm to solve this.
In today’s lab we covered searching and reading code. This should help prepare you for midterm 2.
Before leaving, make sure your TA/instructor have signed you out of the lab. If you finish the lab early, you are free to go.