Week 12 ExerciseDo the Week 10 Exercise
Complete
import java.util.Iterator;
class E10 {
/**
Return whether the Integers from it are in increasing order.
Precondition: it != null and none of its elements are null.
*/
public static boolean isIncreasing(Iterator<Integer> it) {
if (!it.hasNext()) {
return true;
} else {
// Fill this in, using m as a helper.
}
}
private static boolean m(Integer i, Iterator<Integer> it) {
// Fill this in recursively.
}
}
Week 9 ExercisePurposeImplement a simple Some Highlights from the Bulletin BoardThere's been a lively discussion. Here are some significant posts: The concepts involved. The following is the core of what you want to accomplish: Iterator<Integer> it = new RangeIterator(3, 7); System.out.println(it.next()); // prints 3 System.out.println(it.next()); // prints 4 The A48/148 bit: you are using the generic interface
Now write the "..." bit: But be careful not to make too many assumptions about the order of users calling Errors students have encountered"AmbiguousMethodException": the Interactions Pane isn't real Java. "Incompatible types":
don't define your own "Type parameter":
don't make The ExerciseLook up the Write a class Include the following constructor:
/** An Iterator producing Integers with values in [start, end) in order: start, start+1, start+2, ..., end-1.
Precondition: start <= end.
@param start value of the first Integer to produce
@param end one more than the value of the last Integer to produce */
public RangeIterator(int start, int end)
For the method throw new UnsupportedOperationException(); For the method throw new NoSuchElementException(); Also, make a class Note: the MarkingMarking will be by automark of correctness, so be sure to test your code. Commenting is not required, nor will style be marked. Week 6 ExerciseSubmission InstructionsSubmit classes
Question 1Let s(n) be the sum of 1^2, 2^2, ... up to and including n^2.
class E1 {
/** Return the sum of the squares from 1 to n.
Requires: n >= 1. */
public static int s(int n) {
}
}
Question 2Let s(k,n) be the sum of the squares k^2, (k+1)^2, ... up to and including n^2.
class E2 {
/** Return the sum of the squares from k to n.
Requires: k <= n. */
public static int sk(int k, int n) {
// The recursive call must be of the form sk(__, n)
}
/** Return the sum of the squares from k to n.
Requires: k <= n. */
public static int sn(int k, int n) {
// The recursive call must be of the form sn(k, __)
}
}
Question 3[2 marks] Complete
class E3 {
/** Return the sum of the squares from 1 to n.
Requires: n >= 1. */
public static int s(int n) {
return sHelper(__, __, __);
}
/** Return sumSoFar + next^2 + (next+1)^2 + ... + n^2.
Requires: 1 <= next <= n. */
public static int sHelper(int sumSoFar, int next, int n) {
// The recursive call must be of the form sHelper(__, __, n)
}
}
Question 4[2 marks] Complete
class E4 {
/** Return the sum of the squares from 1 to n.
Requires: n >= 1. */
public static int s(int n) {
return sHelper(__, __);
}
/** Return 1^2 + 2^2 + ... + next^2 + sumSoFar.
Requires: 1 <= next. */
public static int sHelper(int sumSoFar, int next) {
}
}
Week 5 ExerciseA tester. Submission InstructionsSubmit only class
The Exercise
This exercise is about simple first-rest recursion on a sequence of items:
import java.util.*;
public class E {
/** Return the number of characters in s. */
public static int length(String s) {
if (s.equals("")) {
// FILL THIS IN.
} else {
char first = s.charAt(0);
int lengthOfRest = length(s.substring(1));
// FILL THIS IN.
// The only variables you may mention are first and lengthOfRest.
}
}
/** Return the number of elements in l. */
public static <T> int length(List<T> l) {
if (l.isEmpty()) {
// FILL THIS IN.
} else {
T first = l.get(0);
int lengthOfRest = length(l.subList(1, l.size()));
// FILL THIS IN.
// The only variables you may mention are first and lengthOfRest.
}
}
/** Return whether s contains c. */
public static boolean contains(String s, char c) {
if (s.equals("")) {
// FILL THIS IN.
} else {
char first = s.charAt(0);
boolean containsOfRest = contains(s.substring(1), c);
// FILL THIS IN.
// The only variables you may mention are first, containsOfRest and c.
}
}
/** Return whether l contains t. */
public static <T> boolean contains(List<T> l, T t) {
if (l.isEmpty()) {
// FILL THIS IN.
} else {
T first = l.get(0);
boolean containsOfRest = contains(l.subList(1, l.size()), t);
// FILL THIS IN.
// The only variables you may mention are first, containsOfRest and t.
}
}
/** Return a copy of s. */
public static String copy(String s) {
if (s.length() == 0) {
// FILL THIS IN.
} else {
char first = s.charAt(0);
String copyOfRest = copy(s.substring(1));
// FILL THIS IN.
// The only variables you may mention are first and copyOfRest.
}
}
/** Return a LinkedList with all the elements of l, in the same order. */
public static <T> LinkedList<T> copy(List<T> l) {
if (l.isEmpty()) {
// FILL THIS IN.
} else {
T first = l.get(0);
LinkedList<T> copyOfRest = copy(l.subList(1, l.size()));
// FILL THIS IN.
// The only variables you may mention are first and copyOfRest.
}
}
/** Return a copy of s but with the characters in reverse order. */
public static String reverseCopy(String s) {
if (s.length() == 0) {
// FILL THIS IN.
} else {
char first = s.charAt(0);
String reverseCopyOfRest = reverseCopy(s.substring(1));
// FILL THIS IN.
// The only variables you may mention are first and reverseCopyOfRest.
}
}
/** Return a LinkedList with all the elements of l, but in reverse order. */
public static <T> LinkedList<T> reverseCopy(List<T> l) {
if (l.isEmpty()) {
// FILL THIS IN.
} else {
T first = l.get(0);
LinkedList<T> reverseCopyOfRest = reverseCopy(l.subList(1, l.size()));
// FILL THIS IN.
// The only variables you may mention are first and reverseCopyOfRest.
}
}
}
Week 4 ExerciseA tester. Submission InstructionsSubmit only class
The ExerciseConsider the following interfaces and classes implementing them:
interface Stack<T> {
void push(T o);
T pop();
int size();
}
interface Queue<T> {
void enqueue(T o);
T dequeue();
int size();
}
class StackImp<T> implements Stack<T> {
public StackImp(int capacity) { /* Implementation not shown */ }
/* Rest of class not shown. */
}
class QueueImp<T> implements Queue<T> {
public QueueImp(int capacity) { /* Implementation not shown */ }
/* Rest of class not shown. */
}
Complete the two methods in class
public class E {
/** Reverse the order of the ith through jth elements of s,
where the numbering starts from 0 at the top.
Requires: 0 <= i <= j < s.size().
@param i first index of the elements to reverse
@param j last index of the elements to reverse */
public static <T> void reverse(Stack<T> s, int i, int j) {
}
/** Reverse the order of the ith through jth elements of q,
where the numbering starts from 0 at the front.
Requires: 0 <= i <= j < q.size().
@param i first index of the elements to reverse
@param j last index of the elements to reverse */
public static <T> void reverse(Queue<T> q, int i, int j) {
}
}
Week 3 ExerciseWas delayed. We're now considering it part of Assignment 0, due this Thursday. |