In java I need help modifying the following sections: size, isEmpy, contains. I
ID: 3780816 • Letter: I
Question
In java I need help modifying the following sections: size, isEmpy, contains. I am trying to do this without changing any declaration of any method. Thank you
public class BtST<Key, Value> {
private Node first;
private class Node {
private Key key;
private Value val;
private Node next;
public Node(Key key, Value val, Node next) {
this.key = key;
this.val = val;
this.next = next;
}
}
public BtST() {
}
public int size() {
return 0;\ ****
}
public boolean isEmpty() {
return true; \***
}
public boolean contains(Key key) {
if (key == null) throw new IllegalArgumentException("argument to contains() is null");
return false;\\ ***
}
public Value get(Key key) {
if (key == null) throw new IllegalArgumentException("argument to get() is null");
for (Node x = first; x != null; x = x.next) {
if (key.equals(x.key))
return x.val;
}
return null;
}
public void put(Key key, Value val) {
if (key == null) throw new IllegalArgumentException("first argument to put() is null");
if (val == null) {
delete(key);
return;
}
for (Node x = first; x != null; x = x.next) {
if (key.equals(x.key)) {
x.val = val;
return;
}
}
first = new Node(key, val, first);
}
public void delete(Key key) {
if (key == null) throw new IllegalArgumentException("argument to delete() is null");
first = delete(first, key);
}
private Node delete(Node x, Key key) {
if (x == null) return null;
if (key.equals(x.key)) {
return x.next;
}
x.next = delete(x.next, key);
return x;
}
public Iterable<Key> keys() {
Queue<Key> queue = new Queue<Key>();
for (Node x = first; x != null; x = x.next)
queue.enqueue(x.key);
return queue;
}
}
Current output, L 11 P 10 M 9 X 7 H S R 3 A 8 12 size mpty at does not contain A st doe not contain B St does not conta1n C gt does nat contain D at does not contain E st doe not contain F at does not cant G st does not contain H at does not contain I st doe not contain J su does not contain K st does not contain L at does not contain M at does not contain N s does not contain st does not contain P at does not contain at does not contain R St does not contain S st does not contain T gt does nat contain U st does not contain V St does not contain St does not conta1n X gt does nat contain Y et does not contain 2 Correct output, L 11 P 10 M 9 X 7 H 5. C 4 R 3 A 8 E 12 5 0 st is not empty st does contain A st does not contain B st does contai st does not contain D st does contain E st does not contain F st does not contain G st does contain H st does not contain I st does not contain J st does not contain K st does contain L st does contai n M st does not contain N st does not contain 0 st does contain P st does not contain Q st does contain R st does contain 5 st does not contain T st does not contain U st does not contain V st does not contai n W st does contai st does not contain Y st does not contain ZExplanation / Answer
public int size() {
int s=0;
Node temp=first;
if(first==null)
{
return 0;
}
else
{
s++;
while(temp.next!=null)
{
s++;
temp=temp.next;
}
return s;
}
}
public boolean isEmpty() {
if(first==null) return true;
else return false;
}
public boolean contains(Key key) {
if (key == null) throw new IllegalArgumentException("argument to contains() is null");
Node temp=first;
if(temp==null)
return false;
else
{
if(temp.key.equals(key)) ////////////////this statement is dependent on Key classs. You may have to override .equals() method to make this work
return true;
else
{
while(temp.next!=null)
{
temp=temp.next;
if(temp.key.equals(key)) return true;
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.