Each week you will complete problem(s) to turn in at your section. Most weeks there will usually be problems posted for both Tuesday's and Thursday's section. You must complete at least one problem set per week to earn +3 points for that week. You must earn a total of 20 points for the quarter to receive full credit. Additional points beyond these 20 do not affect your grade, but you are welcome to complete every set of problems if you like.
You will not be graded on whether you have a perfect solution, but on whether you have demonstrated effort. Therefore please show some work that demonstrates how you got the answer rather than just writing the answer by itself. We will be somewhat lenient about exactly how the work is shown. If you find that you have been working on these problems for more than 30 minutes, please stop and indicate this on your paper. Incomplete solutions can still receive credit.
Section homework will be made available the day before it is due. Since it is meant as a warm-up for section, we encourage you to do it not too long before section.
Problems: Solve the following three (3) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
Problems: Solve the following two (2) problem on paper (hand-written or printed) and bring your sheet of paper to your section:
LinkedIntList
as a parameter and that moves values from the second list to this list. You are to attach the second list's elements to the end of this list. You are also to empty the second list. For example, suppose two lists store these sequeces of values:
list1: [8, 17, 2, 4] list2: [1, 2, 3]The call of
list1.transferFrom(list2);
should leave the lists as follows:
list1: [8, 17, 2, 4, 1, 2, 3] list2: []The order of the arguments matters;
list2.transferFrom(lis1);
would have left the lists as follows:
list1: [] list2: [1, 2, 3, 8, 17, 2, 4]Either of the two lists could be empty, but you can assume that neither list is null. You are not to create any new nodes. Your method should simply change the links of the lists to join them together. Assume that you are adding this method to the
LinkedIntList
class from lecture.
Problems: Solve the following problem on paper (hand-written or printed) and bring your sheet of paper to your section:
Define a class called Office that keeps track of information about an office's features. The class should have the following public methods:
Method | Description |
---|---|
public Office(double width, double length, boolean couch, int windows) |
constructs an Office object with the given width and length, couch (true or false) and number of windows |
public boolean isCorner() |
returns true if the office is a corner office. A corner office is square (has the same width and length) and has exactly two windows. |
public String toString() |
returns a string representing the office in the format: width: , length: , windows: . If the office has a couch ", has a couch" should be appended to the end of the string. |
Office o1 = new Office (10.3, 10.3, true, 3); Office o2 = new Office (14.0, 6.7, false, 3);A call on
o1.toString()
and o2.toString()
would return, respectively:
width: 10.3, length: 10.3, windows: 3, has a couch width: 14.0, length: 6.7, windows: 2Also make Office objects comparable to each other using the Comparable interface. Offices that have that have a greater area (width * length) should be considered "less" than other Offices so that they appear at the beginning of a sorted list. Offices that have the same area should be ordered by the number of windows they have, with Offices that have a greater amount of windows considered "less" than Offices that have less. If Offices still appear equal they should be compared by whether or not they have a couch. Offices with couches should be considered "less".
Problems: Solve the following one (1) problem on paper (hand-written or printed) and bring your sheet of paper to your section:
makePerfect
, which is also a Practice-it problem, that could be added to the IntTree
class. The method should add nodes until the binary tree is a "perfect" tree. A perfect binary tree is one where all leaves are at the same level. Another way of thinking of it is that you are adding dummy nodes to the tree until every path from the root to a leaf is the same length. A perfect tree's shape is exactly triangular and every branch node has exactly two children. Each node you add to the tree should store the value 0. The following is an example with a variable tree
IntTree.
IntTree tree = new IntTree();
tree.makePerfect();
before call: +----+ | 67 | +----+ / \ / \ +----+ +----+ | 80 | | 52 | +----+ +----+ / \ / / \ / +----+ +----+ +----+ | 16 | | 21 | | 99 | +----+ +----+ +----+ / \ \ / \ \ +----+ +----+ +----+ | 45 | | 33 | | 67 | +----+ +----+ +----+ after call: +----+ | 67 | +----+ / \ / \ +----+ +----+ | 80 | | 52 | +----+ +----+ / \ / \ / \ / \ +----+ +----+ +----+ +----+ | 16 | | 21 | | 99 | | 0 | +----+ +----+ +----+ +----+ / \ / \ / \ / \ / \ / \ / \ / \ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 0 | | 0 | | 45 | | 33 | | 0 | | 67 | | 0 | | 0 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+For this problem, you may assume the existence of the following helper method, which you are allowed to call at most once during an entire call to your method:
public int height() { ... } // returns height of tree from root to lowest leaf
For example, calling height on the tree above would return 4 because it is 4 levels tall.
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
public class Vehicle {...} public class Car extends Vehicle {...} public class SUV extends Car {...}Which of the following are legal statements?
public class Bay extends Lake { public void method1() { System.out.print("Bay 1 "); super.method2(); } public void method2() { System.out.print("Bay 2 "); } } public class Pond { public void method1() { System.out.print("Pond 1 "); } public void method2() { System.out.print("Pond 2 "); } public void method3() { System.out.print("Pond 3 "); } } public class Ocean extends Bay { public void method2() { System.out.print("Ocean 2 "); } } public class Lake extends Pond { public void method3() { System.out.print("Lake 3 "); method2(); } }
Pond var1 = new Bay(); Object var2 = new Ocean();
((Lake) var1).method1(); ((Bay) var1).method1(); ((Pond) var2).method2(); ((Lake) var2).method2(); ((Ocean) var2).method3();
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
Problems: Solve the following one (1) problem on paper (hand-written or printed) and bring your sheet of paper to either one of the sections this week:
permute("JANE")
outputting JANE, JAEN, JNAE, ..., it should output ENAJ, ENJA, EANJ, ... Reverse the order by modifying the algorithm and the order in which it chooses various paths to explore, not by literally reversing strings as they are about to be printed. Use Permute.java's as a starting point.Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
ArrayIntList
where the size is never incremented when add
is called. Write a minimal method that would expose this bug. In other words, your code should include some printlns that would indicate that something bad happened given that bug.
You don't necessarily have to type up and run the code if you don't want to. Just write out the code for this one method manually. It can be very short; write the minimal amount of code you need so that this bug will be visible.
You don't need to write a complete program; just the relevant lines of code to answer the question.
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
// Prints each character of the string reversed twice. // doubleReverse("hello") prints oolllleehh public static void doubleReverse(String s) { for (int i = s.length() - 1; i >= 0; i--) { System.out.print(s.charAt(i)); System.out.print(s.charAt(i)); } }
front -> [1] -> [2] / temp -> [3] -> [4] -> [5] / Into this sequence of ListNode objects: front -> [3] -> [2] -> [4] -> [1] -> [5] /There may be more than one way to write the code, but you are NOT allowed to change any existing node's data field value. You also should not create new ListNode objects unless necessary to add new values to the chain, but you may create a single ListNode variable to refer to any existing node if you like. If a variable does not appear in the "after" picture, it doesn't matter what value it has after the changes are made.
Problems: Solve the following three (3) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
Integer n1 = 15; Integer n2 = 7; Integer n3 = 15; String s1 = "computer"; String s2 = "soda"; String s3 = "pencil";Indicate whether the result of each of the following comparisons is positive, negative, or 0: (solve all of (a) - (f). You do not need to show your work.)
a. n1.compareTo(n2) b. n3.compareTo(n1) c. n2.compareTo(n1) d. s1.compareTo(s2) e. s3.compareTo(s1) f. s2.compareTo(s2)
TimeSpan
class from Chapter 8 to include a compareTo
method that compares time spans by their length. A time span that represents a shorter amount of time is considered to be "less than" one that represents a longer amount of time. For example, a span of 3 hours and 15 minutes is greater than a span of 1 hour and 40 minutes. (You can use the following file as a template: TimeSpan.java)
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
List
has every method that a Set
has, and more. So why would you use a Set
rather than a List
? (A few sentences will suffice.){key1=value1, key2=value2, ...}
Map<Integer, String> map = new HashMap<Integer, String>(); map.put(8, "Eight"); map.put(41, "Forty-one"); map.put(8, "Ocho"); map.put(18, "Eighteen"); map.put(50, "Fifty"); map.put(132, "OneThreeTwo"); map.put(28, "Twenty-eight"); map.put(79, "Seventy-nine"); map.remove(41); map.remove(28); map.remove("Eight"); map.put(50, "Forty-one"); map.put(28, "18"); map.remove(18);
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
// Adds the digits of the given number. // Example: digitSum(3456) returns 3+4+5+6 = 18 public static int digitSum(int n) { if (n > 10) { // base case (small number) return n; } else { // recursive case (large number) return n % 10 + digitSum(n / 10); } }(Indicate the bug and explain why it causes the method to fail. Why does your change solve the problem?)
public static int mystery5(int x, int y) { if (x < 0) { return -mystery5(-x, y); } else if (y < 0) { return -mystery5(x, -y); } else if (x == 0 && y == 0) { return 0; } else { return 100 * mystery5(x / 10, y / 10) + 10 * (x % 10) + y % 10; } }For each of the following calls, indicate the value that is returned: (Solve only parts (a), (c), and (e). Show your work by writing out the series of calls that are made before writing the final output. For example, if mystery1(5) calls mystery1(4), write the sequence of such calls before your answer.
a. mystery5(5, 7); c. mystery5(-7, 4); e. mystery5(128, 343);
Problems: Solve the following one (1) problem on paper (hand-written or printed) and bring your sheet of paper to your section:
public static void mystery1(int n) { if (n <= 1) { System.out.print(n); } else { mystery1(n / 2); System.out.print(", " + n); } }For each of the following calls, indicate the output that is produced by the
mystery1
method: (Solve only parts (a), (b), (e) and (f)). Show your work by writing out the series of calls that are made before writing the final output. For example, if mystery1(5) calls mystery1(4), write the sequence of such calls before your answer.
a. mystery1(1); b. mystery1(2); e. mystery1(16); f. mystery1(30);
Problems: Solve the following two (2) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
LinkedIntList
class from this chapter. Write a method called set
that accepts an index and a value and sets the list's element at that index to have the given value. You may assume that the index is between 0 (inclusive) and the size of the list (exclusive). (You can base your solution on the get
method we wrote in lecture on Wednesday. If you're having trouble, do the best that you can in < 30 minutes and turn in your best effort.)Problems: Solve the following one (1) problem on paper (hand-written or printed) and bring your sheet of paper to your section:
list.next = new LinkNode(3, list.next);
Problems: Solve the following three (3) problems on paper (hand-written or printed) and bring your sheet of paper to your section:
ArrayIntList
(Tue Oct 6)Problems: Solve the following two (2) Self-Check problems on paper (hand-written or printed) and bring your sheet of paper to your section:
You don't need to write a complete program; just the relevant lines of code to answer the question.
No problems are due for the first section.
Here are some optional Practice-It problems that would be useful review: arrayMystery, indexOf, findMin, numUnique, stretch, rotateRight, add.