Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This quiz asks you to complete the one interface and two classes described below

ID: 3770762 • Letter: T

Question

This quiz asks you to complete the one interface and two classes described below. Each item to be filled in is marked by ?n?. Question n asks you to fill in ?n?.

The generic Sequence interface declares two methods to implement. The next method should return the next element in the sequence. The first call to the next method should return the first element of the sequnce. The reset method should ensure that the next call to the next method returns the first element of the sequence.

/**
* A Sequence provides a series of values by repeatedly calling next.
*/
public interface Sequence<T> {
/**
* @return the next value in the sequence
*/
public ?1? next();

/**
* Reset the sequence to start from the beginning.
*/
public void reset();
}

The generic AbstractSequence class implements the next and reset methods of the Sequence interface by two abstract methods. The firstValue method should return the first value of the seqeuence. The computeValue method should return the next value of the sequence given the current value of the sequence.

/**
* An AbstractSequence uses getFirst and computeNext to implement
* next and reset.
*/
public abstract class AbstractSequence?2? implements Sequence<T> {
/**
* The next value to be returned by the next method.
*/
private ?3? nextValue;

public ?4? next() {
?5? current = nextValue;
?6? = computeNext(?7?);
return current;
}

public void reset() { ?8? = getFirst(); }

/**
* @return the first value of the sequence
*/
public abstract ?9? getFirst();

/**
* @param current the current value of the sequence.
* @return the next value of the sequence.
*/
public abstract ?10? computeNext(?10? current);
}

The following class has nothing to do with the above Sequence interface and AbstractSequence class.

The generic Link class allows for the creation of linked lists for any type. The linked list is made up of a series of Link objects. The first Link object is a dummy node to stand for the head of the list. The elements are stored in the following Links.

/**
* A simple class for linked lists. The first Link object
* is a dummy node to represent the head of the list. The
* elements of the list are kept in the following nodes.
*/
public class Link<T> {
private ?11? value;
private ?12? next;

/**
* Add val to the end of the list using recursion.
* @param val the value to be added to the list
*/
public void add(?13? val) {
if (next == null) {
next = ?14? ?15?();
next.value = ?16?;
} else {
next.?17?(val);
}
}

/**
* Check if val is in the the list using recursion.
* Assumes the value in this link has already been checked,
* so it checks the value in the next link.
* @param val the value to test for
* @return true if value is in the list
*/
public boolean contains(?13? val) {
if (next == null) {
return false;
} else if (?18?.equals(val)) {
return ?19?;
} else {
return next.?20?(val);
}
}
}

Explanation / Answer

?n? are replaced with code and are in bold

/**
* A Sequence provides a series of values by repeatedly calling next.
*/
public interface Sequence<T> {
/**
* @return the next value in the sequence
*/
public T next();

/**
* Reset the sequence to start from the beginning.
*/
public void reset();
}

/**
* An AbstractSequence uses getFirst and computeNext to implement
* next and reset.
*/
public abstract class AbstractSequence<T> implements Sequence<T> {
/**
* The next value to be returned by the next method.
*/
private T nextValue;

public T next() {
T current = nextValue;
nextValue = computeNext(current);
return current;
}

public void reset() { nextValue = getFirst(); }

/**
* @return the first value of the sequence
*/
public abstract T getFirst();

/**
* @param current the current value of the sequence.
* @return the next value of the sequence.
*/
public abstract T computeNext(T current);
}

/**
* A simple class for linked lists. The first Link object
* is a dummy node to represent the head of the list. The
* elements of the list are kept in the following nodes.
*/
public class Link<T> {
private T value;
private Link<T> next;

/**
* Add val to the end of the list using recursion.
* @param val the value to be added to the list
*/
public void add(T val) {
if (next == null) {
next = new Link<T>();
next.value = val;
} else {
next.add(val);
}
}

/**
* Check if val is in the the list using recursion.
* Assumes the value in this link has already been checked,
* so it checks the value in the next link.
* @param val the value to test for
* @return true if value is in the list
*/
public boolean contains(T val) {
if (next == null) {
return false;
} else if (next.value.equals(val)) {
return true;
} else {
return next.contains(val);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote