Hi
i am doing a 2nd year Java course, and have to create a tabbed web browser in Java.however i cant use inbuilt classes,like JTabbedPane,DefaultMutableTreeNode etc. i have to make everything myself, i am trying not to freak out.
The first part was to create a circular list which will be used to implement the tabs. This is the given circular list ADT which i had to implement:
Code:
package cs214.lists;
import cs214.common.Position;
/**
* Interface for a circular linked list. The list has a single access point,
* called the cursor, which may be adjusted to access the data in the list.
*
* @param <E>
* the type of data to store at each list position.
*/
public interface CircularListADT<E> extends Iterable<E> {
/**
* Returns the number of elements (alternatively, positions) in the list.
*
* @return the number of elements in the list.
*/
public int size();
/**
* Tests whether the list is empty.
*
* @return <code>true</code> if the list is empty, and <code>false</code> if
* it is not.
*/
public boolean isEmpty();
/**
* Returns the cursor position in the list.
*
* @return the cursor position.
* @throws EmptyListException
* if the list is empty.
*/
public Position<E> cursor() throws EmptyListException;
/**
* Removes the cursor position from the list, and returns its element. The
* cursor is advanced to the next position in the list, if there is one.
*
* @return the element stored in the removed position.
* @throws EmptyListException
* if the list is empty.
*/
public E remove() throws EmptyListException;
/**
* Adds a new position containing the specified element to the list. If the
* list is non-empty, the position is added directly after the current
* cursor; if the list is empty, it becomes the only, first element and
* cursor.
*
* @param element
* the element to store at the new position.
*/
public void add(E element);
/**
* Moves the cursor to the next position in the list.
*
* @throws EmptyListException
* if the list is empty.
*/
public void advance() throws EmptyListException;
/**
* Moves the cursor to the previous position in the list.
*
* @throws EmptyListException
* if the list is empty.
*/
public void retreat() throws EmptyListException;
}
I implemented this in a Circular List file:
Code:
package cs214.lists;
import cs214.common.Position;
import cs214.lists.CNode;
import java.util.Iterator;
import java.lang.UnsupportedOperationException;
//import cs214.lists.CNode.Node;
public class CircularList<E> implements CircularListADT<E> {
protected int size;
protected CNode<E> cursor = null;
protected CLIterator <E>it;
public CircularList(){
size = 0;
}
public boolean isEmpty() {
return cursor == null;
}
public int size() {
return this.size;
}
public Position<E> cursor() throws EmptyListException {
if (cursor == null) {
throw new EmptyListException("The list is empty.");
}
return cursor;
}
public E remove() throws EmptyListException {
CNode<E> n = cursor.getPrevious();
CNode<E>m = cursor.getNext();
if (cursor == null) {
throw new EmptyListException("The list is empty.");
}
else {
n.setPrevious(m);
m.setNext(n);
cursor.setPrevious(null);
cursor.setNext(null);
size--;
/*n = cursor;
n.next = cursor.next;
cursor = n.next;*/
}
return cursor.value;
}
public void add(E element) {
CNode<E> n = new CNode<E>(element,cursor,cursor.getPrevious());
if (cursor == null) {
n.next = n;
cursor = n;
}
else {
n.next = cursor.next;
cursor.next = n;
cursor = n;
}
}
public void advance() throws EmptyListException {
if (cursor == null) {
throw new EmptyListException("The list is empty.");
}
else {
it = new CLIterator<E>(cursor);
cursor = cursor.next;
}
}
public void retreat() throws EmptyListException{
if (cursor == null) {
throw new EmptyListException("The list is empty.");
}
}
/* public Iterator<E> getIterator() {
return it;
}
*/
public Iterator<E> iterator(){
return new CLIterator<E>(cursor);
}
class CLIterator<E> implements Iterator<E> {
CNode<E> cursor;
public CLIterator(CNode<E> cursor) {
this.cursor = cursor;
}
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
cursor = cursor.getNext();
return cursor.element();
}
public void remove() throws UnsupportedOperationException{
if (cursor ==null){
throw new UnsupportedOperationException();
}
}
}
}
This is the Position interface:
Code:
package cs214.common;
/**
* A position in a relatively-defined data structure. Allows a user access to an
* element stored at the position, without breaking encapsulation and exposing
* implementation dependent links to other positions.
*
* @param <E>
* the type of element stored at the position.
*/
public interface Position<E> {
/**
* Returns the element associated with this position.
*
* @return the element associated with this position.
*/
public E element();
}
and the CNode class which i created to define nodes in the circular list:
Code:
package cs214.lists;
import cs214.common.Position;
public class CNode<E> implements Position<E> {
protected E value;
protected CNode<E> next,prev;
public CNode(E element, CNode<E> n, CNode<E> p){
value = element;
next = n;
prev = p;
}
//public CNode(E element2) {
// TODO Auto-generated constructor stub
//}
public E element() {
return value;
}
public CNode<E> getPrevious(){
return prev;
}
public CNode<E> getNext(){
return next;
}
public void setElement(E newElement){
value = newElement;
}
public void setPrevious(CNode<E> newPrevious){
prev = newPrevious;
}
public void setNext(CNode<E> newNext){
next = newNext;
}
public String toString(){
return"This is a node and this is what is inside it " + value;
}
}
My problem is, i dont know how to test if this actually works. Im supposed to make a simple GUI which will create a new tab, close it etc. attached is the actual question. I would appreciate any help at all, because i cant move on to the next part if i dont fix this asap.