5 Dening class BList A bounded list, BList, is a List that places a restriction
ID: 3798512 • Letter: 5
Question
5
Dening class BList
A bounded list, BList, is a List that places a restriction on the maximum allowable number of elements that can be held in the list.
• The invariant adds a restriction that the size must never exceed the specied capacity.
• The constructor methods add a precondition that the capacity cannot be zero.
• The two operations that implement addition now include a precondition that the collection must not have reached its maximum capacity.
6
Your assignment
Implement classes Node, List, BList, and Demo. Class BList retains the interface of its superclass, but places restrictions to its add(..) methods in order to accommodate a bounded collection. Class Demo instantiates a BList and demonstrates its usage by simulating a scenario that deploys all operations.
Dene and implement any and all appropriate contracts using the adbc tool.
Explanation / Answer
Hi, Please find my implementation of first two questions.
Please repost other questions.
Please let me know in case of any issue.
########### Node.java ##############
public class Node<E> {
// instance variables
private E data;
private Node<E> next;
// constructor
public Node(E data){
this.data = data;
next = null;
}
// setters and getters
public E getData() {
return data;
}
public Node<E> getNext() {
return next;
}
public void setData(E data) {
this.data = data;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
############# List.java ################
public class List<E> {
private Node<E> head;
private int listCount;
public List() {
head = null;
listCount = 0;
}
public List(Node<E> n) {
if(n == null){
head = null;
return;
}
head = new Node<E>(n.getData());
listCount = 1;
}
public boolean add(Node<E> n)
{
if(n == null)
return false;
Node<E> temp = new Node<E>(n.getData());
Node<E> current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(temp);
listCount++;
return true;
}
public void add(int index, Node<E> n )
{
// base case
if(n == null || (index < 0 || index > listCount))
return;
Node<E> temp = new Node<E>(n.getData());
Node<E> current = head;
for (int i = 1; i < index && current.getNext() != null; i++) {
current = current.getNext();
}
temp.setNext(current.getNext());
current.setNext(temp);
listCount++;
}
public Node<E> get(int index){
if (index < 0 || index > listCount)
return null;
Node<E> current = head.getNext();
for (int i = 1; i < index; i++) {
current = current.getNext();
}
return current;
}
public void remove(int index){
if (index < 1 || index > size())
return;
Node<E> current = head;
for (int i = 1; i < index; i++) {
current = current.getNext();
}
current.setNext(current.getNext().getNext());
listCount--;
}
public int size()
{
return listCount;
}
public void clear(){
head = null;
listCount = 0;
}
public boolean contains(Node<E> n){
// Base case
if(n == null || head == null)
return false;
Node<E> current = head;
while (current != null) {
if(current.getData() == n.getData())
return true;
current = current.getNext();
}
return false;
}
public int indexOf(Node<E> n){
if(n == null)
return -1;
Node<E> current = head;
int i = 0;
while(current != null) {
if (current.getData() == n.getData())
break;
i++;
current = current.getNext();
}
if(current == null)
return -1;
return i;
}
public boolean isEmpty(){
return head == null;
}
public void set(int index, E element )
{
// base case
if(element == null || (index < 0 || index > listCount))
return;
Node<E> temp = new Node<E>(element);
Node<E> current = head;
for (int i = 1; i < index && current.getNext() != null; i++)
current = current.getNext();
temp.setNext(current.getNext());
current.setNext(temp);
listCount++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.