Question: 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
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
private T data;
private VodeDode
private VodeDode
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
return this.next;}
public void setNext(VodeDode
this.next = next;}
public VodeDode
return this.prev;}
public void setPrev(VodeDode
this.prev = prev;}
@Override
public String toString() {
return data.toString();}}
_________________________________________________________________________
CODE that works fine called ArrayListTen:
import java.util.Iterator;
public class ArrayListTen
private VodeDode
private VodeDode
private int size;
private VodeDode
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
if (this.head== null) {
head = newVodeDode;
tail =newVodeDode;}
else {VodeDode
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
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
VodeDode
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
for (int i = 0; i < start; i++) {
current = current.getNext();}
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
while (current != null) {
result = current.getData() + " " + result;
current = current.getNext();
}return result;
}public Iterator
return new Iterator
VodeDode
public boolean hasNext() {
return current != null;
}public T next() {
if (current == null) {
throw new NullPointerException();
}T data = current.getData();
current = current.getNext();
return data;}};}
public Iterator
return new Iterator
VodeDode
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,
// 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
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");}
//addEnd
list.addEnd(333);
if (list.listToString().equals("122 345 389 999 333 ")) {
System.out.println("Rigth2");}
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
nums.addEnd(10);
nums.addEnd(20);
nums.addEnd(10);
nums.addEnd(30);
nums.addEnd(10);
nums.addEnd(40);
if (nums.removeBN(10)==10 && nums.listToString().equals("20 10 30 10 40 ")
&& nums.removeBN(10)==10 && nums.listToString().equals("20 30 10 40 ")
&& nums.removeBN(50)==null && nums.removeBN(40)==40
&& nums.listToString().equals("20 30 10 ")
&& nums.listToStringBackward().equals("10 30 20 ")){
System.out.println("Rigth4");}
int total = 0;
for (Integer num: nums){
total += num;
}if (total == 60){
System.out.println("Rigth5");
}Iterator
if (iter.hasNext() && iter.next().equals("MD") &&
iter.next().equals("NJ") && iter.next().equals("WV")
&& !iter.hasNext()){
System.out.println("Rigth6");
}
//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
items.addEnd(item1);
items.addEnd(item2);
items.addEnd(item3);
SomeType deleted = items.removeBN(item4);
if (deleted.toString().equals("Apple")){
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
// 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
// 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
// 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) {
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
// 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
for (T item : EE) {
reversedStack.push(item);}
return reversedStack;}
public Iterator
return EE.iterator();}
//******************************************************
//******* BELOW THIS LINE IS TESTING CODE *******
//******* Edit it as much as you'd like! *******
//******* Remember to add JavaDoc *******
//******************************************************
public static void main(String[] args) {
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");
}
//System.out.println(s);
StackInAllSocks
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
