Looking for some help to check my homework against. We\'re programming with Java
ID: 3681969 • Letter: L
Question
Looking for some help to check my homework against. We're programming with Java and dealing with methods for recursive lists.
You will implement some of the methods for the recursive list implementation. These methods should be implemented in class RecList as instance methods and not reference the Cons class or the Empty class directly. All the methods you are coding will go inside class RecList.
1. RecList drop(int n)
a) Return a copy of this list with the first n elements removed (dropped)
b) If the list has fewer then n elements, return the empty list n should be non-negative
How:
-- If n is 0, return this list
--Otherwise, return the result of dropping the first n-1 elements if the rest of this list
2. RecList cons(Object f)
a) Return a copy of this list with the object f attached to the front
b) This is just an instance method version of the basic static cons function
3. RecList append(RecList rl)
a) Return a list created by appending a copy of this list to the front of a copy of rl.
b) This is similar to the other append but using a list rather than and object
4. RecList withoutLast()
a) Return a copy of this list with the last element removed.
b) This list should be non-empty
How:
----If the rest of this list is empty, return the empty list
-----Otherwise, remove the last element from the rest of this list and cons the first element of this list to the result.
5. Object last()
a) Return the last object in this list.
b) This list should be non-empty
How:
----If the rest of this list is empty, return the first element of this list
----Otherwise, return the last element of the rest of this lsit
6. boolean isSizeOne()
a) Return true if this list has exactly one element Not recursive! (But don’t use length)
7. RecList reverse()
a) Returns the reverse of this list
How:
------ If this list is empty, return the empty list
-----Otherwise, reverse the rest of this list and append the first element of this list to the result
Implement those seven method in of the program below. all method should be in RecList class
Programe
package recursiveListCompact;
import recursiveLists.Cons;
import recursiveLists.Empty;
import java.util.Objects;
public abstract class RecList {
public abstract Object getFirst();
public abstract RecList getRest();
public abstract boolean isEmpty();
public abstract boolean equals(Object obj);
public static RecList cons(Object f, RecList r) {
return new Cons(f,r);
}
public static final RecList EMPTY = new Empty();
// methods to do in class
/**
* Return the length of the list
*/
int length() {
if(isEmpty())
return 0;
else
return 1 + getRest().length();
}
/**
* Return the object at index n in the list
* This will cause an error if n is not a valid index
*/
Object get(int n) {
if(n == 0)
return getFirst();
else
return getRest().get(n-1);
}
/**
* Create a new list with e appended to the end of a copy of this list.
*
*/
public RecList append(Object e) {
if(isEmpty())
return cons(e,this);
else {
Object f = getFirst();
RecList rst = getRest().append(e);
return cons(f,rst);
}
}
private static class Empty extends RecList{
@Override
public Object getFirst() {
throw new IllegalStateException("head of empty list");
}
@Override
public RecList getRest() {
throw new IllegalStateException("tail of an empty list");
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof Empty;
}
@Override
public int hashCode() {
return -123456789;
}
}
private static class Cons extends RecList {
private Object first;
private RecList rest;
public Cons(Object first, RecList rest) {
this.first = first;
this.rest = rest;
}
@Override
public Object getFirst() {
return first;
}
@Override
public RecList getRest() {
return rest;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
else if(obj instanceof Cons ) {
Cons c = (Cons)obj;
return c.getFirst().equals(getFirst()) && c.getRest().equals(getRest());
} else
return false;
}
@Override
public int hashCode() {
return -12345*first.hashCode() + rest.hashCode();
}
}
}
Explanation / Answer
Please try using the below piece of code :
package recursiveLists;
import java.util.Objects;
import java.util.function.Function;
public abstract class RecList {
public abstract Object getFirst();
public abstract RecList getRest();
public abstract boolean isEmpty();
public abstract boolean equals(Object obj);
public static RecList cons(Object f, RecList r) {
return new Cons(f,r);
}
public static final RecList EMPTY = new Empty();
public class Empty extends RecList{
@Override
public Object getFirst() {
throw new IllegalStateException("head of empty list");
}
@Override
public RecList getRest() {
throw new IllegalStateException("tail of an empty list");
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof Empty;
}
@Override
public int hashCode() {
return -123456789;
}
}
public class Cons extends RecList {
private Object first;
private RecList rest;
public Cons(Object first, RecList rest) {
this.first = first;
this.rest = rest;
}
@Override
public Object getFirst() {
return first;
}
@Override
public RecList getRest() {
return rest;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
else if(obj instanceof Cons ) {
Cons c = (Cons)obj;
return c.getFirst().equals(getFirst()) && c.getRest().equals(getRest());
} else
return false;
}
@Override
public int hashCode() {
return -12345*first.hashCode() + rest.hashCode();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.