So I have this code( StackInAllSocks ) and I implemented the method but I can't seem to figure
out why there isn't anything showing up on the console. It should pop,peek and push b using the
methods from the class called ArrayListTen . The ArrayListTen works fine and compiles the
tested code of Rigth# but for StackInAllSocks it doesn't complete at all. note that file
VodeDodeis not to be changed is just a Node storage area of the array list.
Did I implement the method on StackInAllSocks correctly? if so, should I not use the method
from the ArrayListTen .?
__________________________________________________________________________
: the code is : VodeDodeis
class VodeDode<T> {
private T data;
private VodeDode<T> next;
private VodeDode<T> prev;
public VodeDode(T data) {
this.data = data;
this.next = null;
this.prev = null;}
public T getData() {
return data;}
public void setData(T data) {
this.data = data;}
public VodeDode<T> getNext() {
return this.next;}
public void setNext(VodeDode<T> next) {
this.next = next;}
public VodeDode<T> getPrev() {
return this.prev;}
public void setPrev(VodeDode<T> prev) {
this.prev = prev;}
@Override
public String toString() {
return data.toString();}}
_________________________________________________________________________
CODE that works fine called ArrayListTen:
import java.util.Iterator;
public class ArrayListTen<T> implements Iterable<T> {
private VodeDode<T> head; //beginning of list
private VodeDode<T> tail; //end of list
private int size;
private VodeDode<T> new_item;
public ArrayListTen( ){
this.head = null;
this.tail = null;
this.size = 0;}
public int lenght() {
return size ;}
public T getBegin() {
if (this.head != null) {
return head.getData();}
else { return null;}}
public void addBegin(T value) {
VodeDode<T> newVodeDode =new VodeDode<T>(value);
if (this.head== null) {
head = newVodeDode;
tail =newVodeDode;}
else {VodeDode<T> temp = head;
head = newVodeDode;
head.setNext(temp);}
size++;}
public T removeBegin() {
if(this.head == null) {
return null;}
else {T current = head.getData();
if (tail == head) {
tail = null;
head = null;
} else {
head = head.getNext();
head.setPrev(null);}size--;
return current;}}
public T getEnd() {
if (tail != null) {
return tail.getData();
} else {
return null;}}
public void addEnd(T value) {
VodeDode<T> newVodeDode = new VodeDode<T>(value);
if (this.tail == null) {
head = newVodeDode;
tail = newVodeDode;
} else {
newVodeDode.setPrev(tail);
tail.setNext(newVodeDode);
tail = newVodeDode;}
size++;}
public T removeEnd() {
if(this.tail == null) {
return null;}
else {
T current = tail.getData();//was head.
if (head == tail) {
head = null;
tail = null;
} else {
tail = tail.getPrev();
tail.setNext(null);}
size--;
return current;}}
public T removeBN(T value) {
VodeDode<T> currVodeDode = head;
VodeDode<T> prevVodeDode = null;
while(currVodeDode != null){
if(currVodeDode.getData().equals(value)){
if(prevVodeDode != null){
prevVodeDode.setNext(currVodeDode.getNext());}
else{
head = curr.
So I have this code(StackInAllSocks) and I implemented the method but.pdf
1. So I have this code( StackInAllSocks ) and I implemented the method but I can't seem to figure
out why there isn't anything showing up on the console. It should pop,peek and push b using the
methods from the class called ArrayListTen . The ArrayListTen works fine and compiles the
tested code of Rigth# but for StackInAllSocks it doesn't complete at all. note that file
VodeDodeis not to be changed is just a Node storage area of the array list.
Did I implement the method on StackInAllSocks correctly? if so, should I not use the method
from the ArrayListTen .?
__________________________________________________________________________
: the code is : VodeDodeis
class VodeDode<T> {
private T data;
private VodeDode<T> next;
private VodeDode<T> prev;
public VodeDode(T data) {
this.data = data;
this.next = null;
this.prev = null;}
public T getData() {
return data;}
public void setData(T data) {
this.data = data;}
public VodeDode<T> getNext() {
return this.next;}
public void setNext(VodeDode<T> next) {
this.next = next;}
public VodeDode<T> getPrev() {
2. return this.prev;}
public void setPrev(VodeDode<T> prev) {
this.prev = prev;}
@Override
public String toString() {
return data.toString();}}
_________________________________________________________________________
CODE that works fine called ArrayListTen:
import java.util.Iterator;
public class ArrayListTen<T> implements Iterable<T> {
private VodeDode<T> head; //beginning of list
private VodeDode<T> tail; //end of list
private int size;
private VodeDode<T> new_item;
public ArrayListTen( ){
this.head = null;
this.tail = null;
this.size = 0;}
public int lenght() {
return size ;}
public T getBegin() {
if (this.head != null) {
return head.getData();}
3. else { return null;}}
public void addBegin(T value) {
VodeDode<T> newVodeDode =new VodeDode<T>(value);
if (this.head== null) {
head = newVodeDode;
tail =newVodeDode;}
else {VodeDode<T> temp = head;
head = newVodeDode;
head.setNext(temp);}
size++;}
public T removeBegin() {
if(this.head == null) {
return null;}
else {T current = head.getData();
if (tail == head) {
tail = null;
head = null;
} else {
head = head.getNext();
head.setPrev(null);}size--;
return current;}}
public T getEnd() {
if (tail != null) {
4. return tail.getData();
} else {
return null;}}
public void addEnd(T value) {
VodeDode<T> newVodeDode = new VodeDode<T>(value);
if (this.tail == null) {
head = newVodeDode;
tail = newVodeDode;
} else {
newVodeDode.setPrev(tail);
tail.setNext(newVodeDode);
tail = newVodeDode;}
size++;}
public T removeEnd() {
if(this.tail == null) {
return null;}
else {
T current = tail.getData();//was head.
if (head == tail) {
head = null;
tail = null;
} else {
tail = tail.getPrev();
5. tail.setNext(null);}
size--;
return current;}}
public T removeBN(T value) {
VodeDode<T> currVodeDode = head;
VodeDode<T> prevVodeDode = null;
while(currVodeDode != null){
if(currVodeDode.getData().equals(value)){
if(prevVodeDode != null){
prevVodeDode.setNext(currVodeDode.getNext());}
else{
head = currVodeDode.getNext();}
return currVodeDode.getData();}
prevVodeDode = currVodeDode;
currVodeDode = currVodeDode.getNext();
}return null ;}
public String listToString(int start) {
if (start < 0 || start >= size()) {
return "";}
StringBuilder s = new StringBuilder(size());
Node<T> current = head;
for (int i = 0; i < start; i++) {
current = current.getNext();}
6. while (current != null) {
s.append(current.getData()).append(" ");
current = current.getNext();}
return s.toString();
}public String listToStringBackward() {
String result = "";
if (head == null) {
return result;
}VodeDode<T> current = head;
while (current != null) {
result = current.getData() + " " + result;
current = current.getNext();
}return result;
} public Iterator<T> iterator() {
return new Iterator<T>() {
VodeDode<T> current = head;
public boolean hasNext() {
return current != null;
}public T next() {
if (current == null) {
throw new NullPointerException();
}T data = current.getData();
current = current.getNext();
7. return data;}};}
public Iterator<T> backwardIterator() {
return new Iterator<T>() {
VodeDode<T> current = tail;
public boolean hasNext() {
return current != null;
}
public T next() {
if (current == null) {
throw new NullPointerException();
}
T data = current.getData();
current = current.getPrev();
return data;
}
};
}
//******************************************************
//******* BELOW THIS LINE IS PROVIDED code *******
//******* Do NOT edit code! *******
//******* Remember to add JavaDoc *******
//******************************************************
// return a string representing all values in the list, from beginning to end,
8. // seperated by a single space
// return empty string for an empty list
// O(n) where n is the number of items
// This would work if your listToString(index) works
public String listToString() {
return listToString(0);
}//******************************************************
//******* BELOW THIS LINE IS TESTING CODE *******
//******* Edit it as much as you'd like! *******
//******* Remember to add JavaDoc *******
//******************************************************
public static void main(String[] args) {
ArrayListTen<Integer> list = new ArrayListTen<>();
list.addBegin(122);
list.addBegin(345);
list.addBegin(389);
list.addBegin(999);
System.out.println(list.listToString());
//System.out.println(list.listToStringBackward());
//System.out.println(list.listToString());
if (list.getBegin()==999 && list.getEnd()==122 &&
list.listToString().equals("122 345 389 999 ")) {
System.out.println("Rigth1");}
9. //addEnd
list.addEnd(333);
if (list.listToString().equals("122 345 389 999 333 ")) {
System.out.println("Rigth2");}
ArrayListTen<String> states = new ArrayListTen<>();
states.addEnd("A");
states.addEnd("B");
states.addEnd("C");
states.addEnd("D");
states.addEnd("E");
//removeBNBegin, removeBNEnd
String name1 = states.removeBegin();
String name2 = states.removeEnd();
if (name1.equals("A") && name2.equals("E") &&
states.listToString().equals("B C D ") &&
states.listToStringBackward().equals("D C B ")){
System.out.println("Rigth3");}
//System.out.println(states.listToString());
//System.out.println(states.listToStringBackward());
//System.out.println(name1);
//System.out.println(name2);
//removeBN
ArrayListTen<Integer> nums = new ArrayListTen<>();
11. //remove special case
class SomeType{
private String value;
public SomeType(String value) { this.value = value; }
public boolean equals(Object o) {
if (!(o instanceof SomeType)) return false;
//both null
if (((SomeType)o).value == null && this.value==null) return true;
//both empty string
if (((SomeType)o).value.length() == 0 && this.value.length()==0) return true;
//compare only the leading chars
return ((SomeType)o).value.charAt(0) == this.value.charAt(0);}
public String toString(){ return value;}}
SomeType item1 = new SomeType("Apple");
SomeType item2 = new SomeType("Alli");
SomeType item3 = new SomeType("Be");
SomeType item4 = new SomeType("der");
ArrayListTen<SomeType> items = new ArrayListTen<>();
items.addEnd(item1);
items.addEnd(item2);
items.addEnd(item3);
SomeType deleted = items.removeBN(item4);
if (deleted.toString().equals("Apple")){
12. System.out.println("Rigth7");
}}}
_______________________________________________________________________
The code below is the one that doesn't let me compile anything at all or pass these tests of
printing Right# CODE: StackInAllSocks
import java.util.Iterator;
public class StackInAllSocks<T> implements Iterable<T> {
// storage - you MUST use this for credit!
// Do NOT change the name or type
// NOTE: you cannot use any arrays or JCF instances in your implementation.
private ArrayListTen<T> EE;
// ADD MORE PRIVATE MEMBERS HERE IF NEEDED!
// private int size;
// initialize the StackInAllSocks to being an empty StackInAllSocks
public StackInAllSocks() {
EE = new ArrayListTen<>();
size = 0;}
public void push(T item) {
// push an item onto the StackInAllSocks
// you may assume the item is not null
EE.addBegin(item);
size++;}
public T pop() {
// pop an item off the StackInAllSocks
13. // if there are no items on the StackInAllSocks, return null
if (isEmpty()) {
return null;}
size--;
return EE.removeEnd();
}
public T peek() {
// return the top of the StackInAllSocks (but don't remove it)
// if there are no items on the StackInAllSocks, return null
if (isEmpty()) {
return null;
}
return EE.getBegin();
}
public String toString() {
// Create a string of the StackInAllSocks where each item
// is separated by a space from bottom to top, that is:
// - the bottom of the StackInAllSocks should be shown to the left; and
// - the top of the StackInAllSocks should be shown to the right
// Hint: Reuse the provided code from another class
// instead of writing this yourself!
// O(n) where n is the number of items in stack
StringBuilder sb = new StringBuilder(); for (T item : EE) {
14. sb.append(item).append(" ");}return sb.toString().trim();}
public boolean isEmpty() {
// return whether or not the StackInAllSocks is empty
// O(1)
return EE != null;
}
public StackInAllSocks<T> reverseStack() {
// return a new stack with all items from this stack
// but in the reverse order
// i.e. current stack top should be at the bottom of the reversed stack
// current stack bottom should be at the top of the reversed stack
// Note: the returned new stack should not be affected by any
// subsequent operations of this stack. See examples below in main().
// O(n) where n is the number of items in stack
StackInAllSocks<T> reversedStack = new StackInAllSocks<>();
for (T item : EE) {
reversedStack.push(item);}
return reversedStack; }
public Iterator<T> iterator() {
return EE.iterator();}
//******************************************************
//******* BELOW THIS LINE IS TESTING CODE *******
//******* Edit it as much as you'd like! *******
15. //******* Remember to add JavaDoc *******
//******************************************************
public static void main(String[] args) {
StackInAllSocks<String> s = new StackInAllSocks<>();
s.push("student");
//s.push("----------->");
s.push("help");
s.peek();
if (!s.isEmpty() && s.peek().equals("help") && s.pop().equals("help")
&& s.peek().equals("student")) {
System.out.println("Right1");}
System.out.println(s);
System.out.println(s.isEmpty());
System.out.println(s.peek().equals("help"));
//System.out.println(s.pop());
s.push("support");
s.push("and");
s.push("advocacy");
s.push("center");
if (s.toString().equals("student support and advocacy center")
&& !s.isEmpty()) {
System.out.println("Right2");
}
16. //System.out.println(s);
StackInAllSocks<String> back = s.reverseStack();
s.pop();
s.pop();
s.pop();
//System.out.println(s);
if (s.toString().equals("student support") && s.pop().equals("support")
&& s.pop().equals("student") && s.isEmpty() && s.pop() == null
&& back.toString().equals("center advocacy and support student")) {
System.out.println("Right3");}}}
Thank you For looking at my code