How do I implement abstraction and generics in java? - java

I currently have a project that requires the use of abstraction and generics, but I don't even know where I should start for this. The abstract class is as follows.
public abstract class Links<AType> {
abstract AType getElem(); //returns the head of the list
abstract Links<AType> getNext(); //return the next link
}
This is the class that extends the abstract class
public class Cons<AType> extends Links<AType> {
AType elem;
Links<AType> next;
Cons(AType elem, Links<AType> next) {
this.elem = elem;
this.next = next;
}
#Override
AType getElem() {
return elem;
}
#Override
Links<AType> getNext() {
return next;
}
}
Here's another class that extends the abstract class
public class Nil<AType> extends Links<AType> {
Nil(){}
#Override
AType getElem() {
return null;
}
#Override
Links<AType> getNext() {
return null;
}
}
And here is the class that is supposed to implement everything
public class LList<AType> {
Links<AType> list;
LList() {
list = new Cons<>();
}
Links<AType> getList() {
return list;
}
AType get(int n, AType a) {
Cons<AType> aTypeCons = new Cons<>(a, list);
return null;
}
void add(AType elem) {
//add to head of list
}
void remove(int i) {
//remove ith element
//do nothing if i is invalid
}
void print() {
//prints the list
}
}
I just need some help figuring out where to actually start in making the LList class. I can't figure out the constructor because Links is abstract, so I can't make that an object, and I can't make a new Cons<> because there are no elements that are passed into the constructor. However, the constructor is supposed to instantiate a new list. I also can't figure out how I'm supposed to be able to access an individual element of that list. If I can just have a bit of understanding of what needs to happen in the constructor, I should be able to figure out how to implement the rest of the methods.

Your LList is a singly-linked list, where each element has a value and a link to the list that follows. The final element of the list will always be a Nil object, which represents an empty list. When you first initialize an empty list, you can just assign list = new Nil<>();. When you add an element to the list, you can reassign it as list = new Cons<>(elem, list);.
To access an an element in the list by index, just use a while loop that calls getNext() until it's either reached the desired index or found the end of the list.
AType get(int n) {
Links<AType> current = list;
while (n > 0 && current instanceof Cons) {
current = current.getNext();
n--;
}
return current.getElem();
}

Related

Generic in Java throwing errors when methods are defined

I am learning Generics in Java. For that, I tried out a simple LinkedList like that.
class Node {
private int age;
private String name;
private Node next;
public Node(int age, String name) {
this.age = age;
this.name = name;
this.next = null;
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
}
class LinkedList<T> {
private T head;
private T current;
public LinkedList() {
head = null;
current = null;
}
public void append(T x) {
if (head == null) {
head = x;
current = x;
}
else {
current.setNext(x);
current = x;
}
}
public T getAt(int index) {
T ptr = head;
for(int i = 0; i < index; i++) {
ptr = ptr.getNext();
}
return ptr;
}
}
class Main {
public static void main(String[] args) {
LinkedList<Node> list = new LinkedList<Node>();
list.append(new Node(39, "John"));
list.append(new Node(43, "Josh"));
Node x = list.getAt(1);
System.out.println(String.format("%d, %s", x.getAge(), x.getName()));
}
}
But I get this error, while all the methods do exist in the Node class. What mistake am I doing?
LinkedList.java:16: error: cannot find symbol
current.setNext(x);
^
symbol: method setNext(T)
location: variable current of type T
where T is a type-variable:
T extends Object declared in class LinkedList
LinkedList.java:24: error: cannot find symbol
ptr = ptr.getNext();
^
symbol: method getNext()
location: variable ptr of type T
where T is a type-variable:
T extends Object declared in class LinkedList
2 errors
If current is of type T, you can't call methods of the Node class (such as setNext()) on current, since T can be substituted by any class when you instantiate your LinkedList.
Your Node class shouldn't be the generic type argument of LinkedList. A LinkedList should always be made of Nodes. The type of the data stored in each Node should be a generic type.
class Node<T> {
private T data;
private Node next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
And the LinkedList should contain Node<T> nodes:
class LinkedList<T> {
private Node<T> head;
private Node<T> current;
}
Compiler is unable to understand T type. You have used t.setNext() , however it is isn't present in T definition unless actually used. I might sound a bit confusing here but try this:
Create an Interface Contract having setNext and getNext method.
Implement Node extending above interface. Node implements Contract.
In Linked List change generics to T extends Contract
There is no hasNext for any given generic T, so the code doesn't compile
You'd have to make sure that the LinkedList only holds Node classes or its subtypes
class LinkedList<T extends Node>
But note: That T is not the same as the generic stored within the nodes, so this seems better
class LinkedList<T> {
Node<T> head;
private T current; is a generic type and you are calling setNext and getNext on it. How come T know that it always have these methods? That's the reason it's not working.
Hence, you need to ensure that your generic type T knows that it has setNext and getNext methods.
Hence the fix is:
T extends NodeAbstract in class definition where NodeAbstract is the interface declaring signature of these methods. now this ensures that anything T gets is always going to have these two methods.
You must make Node<T> and LinkedList<Node>.
public void append(T x) { // Here x is of Type T
if (head == null) {
head = x;
current = x; //Assigning x to current, current is also of Type T
}
else {
current.setNext(x); // T.setNext is not defined! Because T can be a String/Integer/whatever. They do not have setNext method
current = x;
}
}

What is the use of the position interface in the Linked list implementations?

I do not see the use of the interface,
why can't we directly implement the getElement() method directly in the Node Class?
public interface Position <T> {
public T getElement();
}
Hereby the SNODE class:
public class SNode<T> implements Position<T> {
private T element;
private SNode<T> next;
public SNode(T e, SNode<T> n) {
element = e;
next = n;
}
public SNode<T> getNext() {
return next;
}
public void setNext(SNode<T> next) {
this.next = next;
}
public void setElement(T element) {
this.element = element;
}
#Override
public T getElement() {
return element;
}
}
The position interface provides a general abstraction for the location of an element within a structure. A position acts as a marker/token within a broader list. A position p, which is associated with some element e in a list L, does not change, even if the index of e changes in L due to insertions or deletions elsewhere in the list. Nor does position p change if we replace the element e stored at p with another element. The only way in which a position becomes invalid is if that position is removed.
The reason for having it's definition of a position type allows position to serve as parameters to some methods and return values from other methods of a list.

linked list iterator inner class-java

I am writing an iterator inner class that iterates through a list. Besides the remove method, I believe I have implemented all the methods of iterator correctly but I get an error saying "Bound mismatch: The type E is not a valid substitute for the bounded parameter > of the type List.Node". I believe this has to with having Node> implements Iterable at the top of my code but I do not want to change that if unneeded. Any possible suggestions on what I should do?
public class List<T extends Comparable<L>> implements Iterable<L> {
private class Node<N extends Comparable<N>> {
private N data;
private Node<N> next;
}
protected Node<L> head;
public Iterator<L> iterator() {
return new ListIterator<L>();
}
public class ListIterator<E extends Comparable<E>> implements Iterator<E> {
private Node<E> N = (Node<E>) head; //"Bound mismatch: The type E is not a valid substitute for the bounded parameter <D extends Comparable<D>> of the type List<T>.Node<D>"
public boolean hasNext() {
return (N.next != null);
}
public E next() {
if (!hasNext())
throw new NoSuchElementException();
else {
N = N.next;
return N.data;
}
}
public void remove() {
}
}
}
You should reduce the number of generic types. Because the inner classes know the generic type of their parent class, you should simplify the Node and ListIterator class:
public class List<L extends Comparable<L>> implements Iterable<L> {
private class Node {
private L data;
private Node next;
}
protected Node head;
public Iterator<L> iterator() {
return new ListIterator();
}
public class ListIterator implements Iterator<L> {
private Node N = head;
public boolean hasNext() {
return (N.next != null);
}
public L next() {
if (!hasNext())
throw new NoSuchElementException();
else {
N = N.next;
return N.data;
}
}
public void remove() {
}
}
}
The type parameter N is declared as
N extends Comparable<N>
ie. it has bounds. It must be Comparable to itself.
The type parameter E is declared as
E
ie. it has no bounds. It can be any type, but not necessarily a type that is Comparable to itself.
Therefore, you can't use E where an N is expected. Consider adding the same bounds as N to E.

Generic Iterator implementation in java

I have the following design:
I have an Abstract class Instance,
I have a class Library that extends Instance and
I have a class File that also extends Instance
I've created my own linked list implementation and it's defined as follows:
public class List<T extends Instance> implements Iterable {
//some other code here
public Iterator iterator(){
return new ListIterator(this);
}
now I've created a class
public class ListIterator<T extends Instance> implements Iterator<T> {
private List thisList;
private Node current;
public ListIterator(List l){
thisList=l;
current=thisList.head.next;
}
#Override
public boolean hasNext() {
if(current==null)
return false;
return false;
}
#Override
public T next() {
Node temp=current;
current=current.next;
return temp.data;
}
}
Where Node is
public class Node<T extends Instance> {
public Node<T> next;
public Node<T> prev;
public T data;
public Node(T data,Node prev, Node next){
this.data=data;
this.prev=prev;
this.next=next;
}
}
so my problem is as follows: the line return temp.data rises an error:
Type mismatch - cannot convert from Instance to T.
What is wrong with this code?
I'd say that Node.data is a reference to an Instance object? If that is the case, the compiler can't automatically change an Instance to a T, because even though T is an Instance object (T extends Instance), any given Instance might not be a T.
The Java Generics tutorial explains it: http://docs.oracle.com/javase/tutorial/extra/generics/subtype.html
Also, in your List<T> class, you should be specifying Iterator and ListIterator as generic using Iterator<T> and ListIterator<T>, or else the compiler won't be able to handle the generics properly. Your Node reference also needs to be generic: Node<T>
Hence you should be using
private Node<T> current;
and
public T next() {
Node<T> temp=current;
current=current.next;
return temp.data;
}
The compiler will usually warn you when you're using a raw type for a generic class.
Did no one notice the bug:
public boolean hasNext() {
if(current==null)
return false;
return false;
}
This is an invariant. Unless I am missing something, the iterator will very quickly return 0 elements!

How to compare generic nodes in a linked list using Comparable?

I am implementing a sorted list using linked lists. My node class looks like this
public class Node<E>{
E elem;
Node<E> next, previous;
}
In the sorted list class I have the add method, where I need to compare generic objects based on their implementation of compareTo() methods, but I get this syntax error
"The method compareTo(E) is undefined for type E". I have tried implemnting the compareTo method in Node, but then I can't call any of object's methods, because E is generic type.
Here is the non-finished body of add(E elem) method.
public void add(E elem)
{
Node<E> temp = new Node<E>();
temp.elem = elem;
if( isEmpty() ) {
temp.next = head;
head.previous = temp;
head = temp;
counter++;
}else{
for(Node<E> cur = head; cur.next != null ; cur= cur.next) {
**if(temp.elem.comparTo(cur.elem)) {**
//do the sort;
}/*else{
cur.previous = temp;
}*/
}
//else insert at the end
}
}
Here is one of the object implemnting compareTo method
public class Patient implements Comparable<Patient>{
public int compareTo(Patient that)
{
return (this.getPriority() <= that.getPriority() ? 1 : 0 );
}
}
Bound E to Comparable:
public class Node<E extends Comparable<E>>{
E elem;
Node<E> next, previous;
}
It will compile now.
If you want the elements stored in your nodes to be comparable, you can state this using generics:
public class Node<E extends Comparable<E>> {
E elem;
Node<E> next, previous;
}
this way it is sure, that every E implements the Comparable interface, so you can safely call the compareTo method.
It seems that your generic E must be E extends Comparable<E>. This way you will get the access to the compareTo(E other) method. However, you will be unable to add elements that are not implementing this interface.
Try
public class Node<E extends Comparable<E>>{
E elem;
Node<E> next, previous;
}

Categories