We have a Liststack and an ADTStackJqwikTest.
Here's my code in ListStack for the push and the pushAll() method.
#Override
public Stack<A> push(A e) {
return new ListStack(list.cons(e));
}
#Override
public Stack<A> pushAll(List<A> xs) {
return xs.isEmpty() ? this : new ListStack<A>(List.append(xs, list));
}
In my ListStack I've a test:
public static void main(String[] args) {
Stack<Integer> s1 = empty();
s1 = s1.pushAll(list(1,2,3));
s1 = s1.pushAll(list(1,2,3));
System.out.println("PushAll: " + s1.toList());
Stack<Integer> s2 = empty();
s2 = s2.push(1);
s2 = s2.push(2);
s2 = s2.push(3);
s2 = s2.push(1);
s2 = s2.push(2);
s2 = s2.push(3);
System.out.println("Push: " + s2.toList());
}
}
the result I get is:
PushAll: 1, 2, 3, 1, 2, 3
Push: 3, 2, 1, 3, 2, 1
But the result should be the same. What do I wrong?
In my ADTStackJqwikTest I've to pushAll() methods.
// ∀s:Stack<A> : pushAll([],s) = s
#Property
<A> boolean pushAll(#ForAll("stacks") Stack<A> s) {
return s.pushAll(List.list()).equals(s);
}
// ∀s:Stack<A>, ∀xs:List<A> : pushAll(x:xs,s)= push(x,pushAll(xs,s)), falls s nicht leer
#Property
<A> boolean pushAll(#ForAll("stacks") Stack<A> s, #ForAll("lists") List<A> xs, #ForAll("as") A x) {
return s.pushAll(xs.cons(x)).equals(s.push(x).pushAll(xs));
}
package stack;
import list.List;
import tuple.Tuple;
import static list.List.list;
public class ListStack implements Stack {
private final List<A> list;
private ListStack(List<A> list) {
this.list = list;
}
private ListStack() {
this.list = list();
}
#Override
public boolean isEmpty() {
return list.isEmpty();
}
#Override
public Stack<A> push(A e) {
return new ListStack<A>(list.cons(e));
}
#Override
public Stack<A> pop() {
if(isEmpty())
throw new IllegalStateException("pop from an empty stack");
else return new ListStack<A>(list.tail());
}
#Override
public A top() {
if(isEmpty())
throw new IllegalStateException("top from an empty stack");
else return list.head();
}
#Override
public Tuple<A, Stack<A>> popTop() {
return Tuple.tuple(top(), pop());
}
#Override
public Tuple<List<A>, Stack<A>> popTopAll() {
return null;
}
#Override
public Stack<A> pushAll(List<A> xs) {
return xs.isEmpty() ? this : new ListStack<A>(List.append(xs, list));
}
#Override
public List<A> toList() {
return list;
}
#Override
public boolean isEqualTo(Stack<A> s) {
return this.toList().isEqualTo(s.toList());
}
#Override
public boolean equals(Object o) {
return o instanceof Stack && isEqualTo((Stack) o);
}
public String toString() {
return list.toString();
}
public static <A> Stack<A> empty() {
return new ListStack(list());
}
This is my whole ListStack
change
#Override
public Stack<A> pushAll(List<A> xs) {
return xs.isEmpty() ? this : new ListStack<A>(List.append(xs, list));
}
into
#Override
public Stack<A> pushAll(List<A> xs) {
return xs.isEmpty() ? this : new ListStack<A>(List.append(list, xs));
}
Okay I got it. Thank you guys.
The mistake was the append in the pushAll method.
Related
I have the following problem. I have the following classes
public class Main {
public static void main(String[] args) {
// Expected: true
// Results in: true
List<String> strLst = new ArrayList<>();
strLst.add(new String("A"));
System.out.println(strLst.contains(new String("A")));
// Expected: true
// Results in: true
List<Number> numLst = new ArrayList<>();
numLst.add(new Number(1));
System.out.println(numLst.contains(new Number(1)));
// Expected: true
// Results in: false
Container<String> integers = new Container<>();
integers.add(new String("A"));
System.out.println(integers.contains(new String("A")));
// Expected: true
// Results in: false
Container<Number> numbers = new Container<>();
numbers.add(new Number(1));
System.out.println(numbers.contains(new Number(1)));
}
}
class Container<T> {
ArrayList<Node<T>> internal = new ArrayList<>();
public void add(T elem) {
internal.add(new Node<>(elem));
}
public boolean contains(T label) {
for (Node<T> e : internal) {
if (e.getLabel().equals(e)) {
return true;
}
}
return false;
}
}
class Node<T> {
T label;
public Node(T label) {
this.label = label;
}
public T getLabel() {
return label;
}
}
class Number {
private int val;
public Number(int val) {
this.val = val;
}
public boolean equals(Object o) {
if (o instanceof Number) {
return this.val == ((Number) o).val;
}
return false;
}
}
I am not sure what went wrong here. I would expect similar behavior with my Container as with ArrayList. But it does not, because in my opinion ArrayList (see https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/ArrayList.java#L251) implements contains in a similar way.
What does ArrayList do different than I do? And if it is not possible to implement such behavior the way I hope to, how would I do this alternatively?
The line
if (e.getLabel().equals(e)) {
in contains should be
if (e.getLabel().equals(label)) {
Solution:
public boolean contains(T label) {
for (Node<T> e : internal) {
if (e.getLabel().equals(label)) {
return true;
}
}
return false;
}
i implemented a Stack Class.
Now, i want to create a Queue based on that stack. Like this :
public class Queue<E> {
private Stack<E> next;
private Stack<E> next2;
public E first() {
}
public Queue<E> enqueue(E e) {
}
public Queue<E> dequeue() {
}
public boolean isEmpty() {
if (this.next == null) {
return true;
}
if (Stack.isEmpty(next)) {
return true;
}
return false;
}
}
I don't know where to start. How can i solve this problem? My first ideas was to use the method "reverse" in Stack. But i'm not sure.
There are 2 ways to do this. First one is to push at the end of stack using your function reverse. Like this
public class Queue<E> {
private Stack<E> next;
public E front() {
return (E) Stack.<E>top(next);
}
public Queue<E> enqueue(E e) {
Stack<E> nextNew = Stack.reverse(next);
nextNew = Stack.push(nextNew, e);
nextNew = Stack.reverse(nextNew);
Queue<E> q = new Queue<E>();
q.next = nextNew;
return q;
}
public Queue<E> dequeue() {
Stack<E> nextNew = Stack.pop(next);
Queue<E> q = new Queue<E>();
q.next = nextNew;
return q;
}
public boolean isEmpty() {
if (this.next == null) {
return true;
}
if (Stack.isEmpty(next)) {
return true;
}
return false;
}
}
I think it's slickest with recursion:
public class Queue<E> {
private Stack<E> stack;
public Queue(Stack stack) {
this.stack = stack;
}
public E front() {
return Stack.top(stack);
}
public Queue<E> enqueue(E newValue) {
if (Stack.top(stack) == null) {
return new Queue(Stack.push(Stack.create(), newValue));
}
return new Queue<>(Stack.push(new Queue(Stack.pop(stack)).enqueue(newValue).stack, Stack.top(stack)));
}
public Queue<E> dequeue() {
return new Queue<>(Stack.pop(stack));
}
public String toString() {
return "Q: " + stack.toString();
}
}
Note that there's no reversing required.
I thought some of the code in the Stack class was a little unclear with the "a" parameters and extra variables, so I refactored it a bit and stripped out everything that wasn't used. I also added a toString() which traverses the stack recursively:
public class Stack<E> {
private final E value;
private final Stack<E> next;
private Stack(E value, Stack<E> next) {
this.value = value;
this.next = next;
}
public static <E> Stack<E> create() {
return new Stack<>(null, null);
}
public static <E> Stack<E> push(Stack<E> stack, E newValue) {
return new Stack<E>(newValue, stack);
}
public static <E> Stack<E> pop(Stack<E> stack) {
if (stack == null) {
return new Stack<E>(null, null);
} else if (stack.value == null) {
return new Stack<>(null, null);
} else if (stack.next == null) {
return new Stack<E>(null, null);
} else {
return stack.next;
}
}
public static <E> E top(Stack<E> stack) {
if (stack == null) {
return null;
} else if (stack.value == null) {
return null;
} else {
return stack.value;
}
}
public String toString() {
if (Stack.top(next) == null) {
return (value != null ? value.toString() : "Null");
}
return (value != null ? value.toString() : "Null") + " " + next.toString();
}
}
And a little testing class. To show the difference between the stack and the queue, it loads up a stack first, then passes it to a Queue and adds a few more elements, then pulls a few off the front:
public class QueueWithStackMain {
public static void main(String[] args) {
Stack<String> stack = Stack.create();
stack = Stack.push(stack, "1");
System.out.println(stack.toString());
stack = Stack.push(stack, "2");
System.out.println(stack.toString());
stack = Stack.push(stack, "3");
System.out.println(stack.toString());
Queue<String> queue = new Queue<>(stack);
System.out.println(queue.toString());
queue = queue.enqueue("4");
System.out.println(queue.toString());
queue = queue.enqueue("5");
System.out.println(queue.toString() + " -> " + queue.front());
queue = queue.dequeue();
System.out.println(queue.toString() + " -> " + queue.front());
}
}
The exception is in the ListCreator i 'when' method line with adding t
Do you guys can maybe tell me why I get this exception? Intelij doesnt see any mistakes in syntax and logic so whats the problem?
I wanted to make my own mapper and selector (filter) and i get this all the time. Is anything wrong here?
Can you please see what might cause this?
I need to use generics and I get lost with those.
package zad1;
import java.util.*;
public class ListCreator<T> {// Uwaga: klasa musi być sparametrtyzowana
List<T> list;
protected ListCreator(List<T> listcreate){
this.list = listcreate; //kreator robi nowa liste zeby nie modyfikowac
}
public static <T> ListCreator<T> collectFrom(List<T> srclist){
ListCreator<T> listCreator = new ListCreator<>(srclist);
return listCreator;
}
public ListCreator<T> when(Selector<T> selector){
List<T> nextlist = new ArrayList<>();
for (T t: this.list){
if (selector.select(t)){
this.list.add(t); //dla kazdego elementu list ktoremu kolejno odpowiada t, jesli spelnia warunek to element jest dodawany do nowej listy
}
}
this.list = nextlist;
return this;
}
public <S> List<S> mapEvery(Mapper<T,S> map){
List<S> dlist = new ArrayList<>();
for (T arg: this.list){
dlist.add(map.map(arg));
}
return dlist;
}
}
package zad1;
import java.util.*;
public class Main {
public Main() {
List<Integer> src1 = Arrays.asList(1,7,9,11,12);
System.out.println(test1(src1));
List<String> src2 = Arrays.asList("a", "zzzz", "vvvvvvv");
System.out.println(test2(src2));
}
public List<Integer> test1(List<Integer> src) {
Selector <Integer> sel = new Selector<Integer>() {
#Override
public boolean select(Integer arg) {
return arg < 10;
}
};
Mapper <Integer,Integer> map = new Mapper<Integer, Integer>() {
#Override
public Integer map(Integer arg) {
return arg + 10;
}
};
return ListCreator.collectFrom(src).when(sel).mapEvery(map);
}
public List<Integer> test2(List<String> src) {
Selector <String> sel = new Selector<String>() {
#Override
public boolean select(String arg) {
if (arg.length() > 3)
return true;
else
return false;
}
};
Mapper <String, Integer> map = new Mapper<String, Integer>() {
#Override
public Integer map(String arg) {
return arg.length() + 10;
}
};
return ListCreator.collectFrom(src).when(sel).mapEvery(map);
}
public static void main(String[] args) {
new Main();
}
}
import java.util.*;
public class MyStack {
private ArrayList<Object> list = new ArrayList<>();
public boolean isEmpty(){
return list.isEmpty();
}
public int getSize(){
return list.size();
}
public Object peek(){
return list.get(getSize()-1);
}
public Object pop(){
Object o = list.get(getSize() -1 );
list.remove(getSize() -1);
return o;
}
public void push(Object o ){
list.add(o);
}
#Override
public String toString() {
return "stack: " + list.toString();
}
public static void main(String[] args){
MyStack mystack = new MyStack();
mystack.push(mystack);
System.out.println(mystack.isEmpty());
System.out.println( mystack.getSize());
System.out.println( mystack.peek());
System.out.println(mystack.pop());
System.out.println(mystack.toString());
}
}
the problem is when I run this code I get a lot of exceptions and its because of the toString() method, and I don't know what is the problem can you help?
I need to loop through the stack until it is empty, adding each element to the queue. Then do the opposite. Loop through the queue until it is empty, adding each element back onto the stack
public class Q1 {
public static void reverseStack(Stack st){
}
}
Here is my test:
public class Q1Test {
#Test
public void testQ1() {
Stack st = new Stack(5);
st.push("A");
st.push("B");
Q1.reverseStack(st);
assertEquals("A",(String) st.top());
}
}
I have been trying to do the Q1 code and never get it to succeed and always end up fail. Can anyone implement a methods as said above to make the Q1 test succeed please?
Here is a working example:
Q1.java
import java.util.ArrayList;
import java.util.List;
public class Q1 {
private List<String> list;
public Q1() {
list = new ArrayList<String>();
}
public String pop_front() {
if (!list.isEmpty()) {
String front = list.get(0);
for (int i = 0; i < list.size()-1; i++) {
list.set(i, list.get(i+1));
}
list.remove(list.size()-1);
return front;
} else {
return null;
}
}
public void push_back(String obj) {
list.add(obj);
}
public String front() {
if (!list.isEmpty()) {
return list.get(0);
} else {
return null;
}
}
public boolean isEmpty() {
return list.isEmpty();
}
public static void reverseStack(Stack stack) {
Q1 queue = new Q1();
while (!stack.isEmpty()) {
queue.push_back(stack.pop());
}
while (!queue.isEmpty()) {
stack.push(queue.pop_front());
}
}
}
A Stack class:
Stack.java
package com.dub;
import java.util.ArrayList;
import java.util.List;
public class Stack {
private List<String> list;
public Stack() {
list = new ArrayList<String>();
}
public String pop() {
if (!list.isEmpty()) {
String top = list.get(list.size()-1);
list.remove(list.size()-1);
return top;
} else {
return null;
}
}
public void push(String obj) {
list.add(obj);
}
public String top() {
if (!list.isEmpty()) {
return list.get(list.size()-1);
} else {
return null;
}
}
public boolean isEmpty() {
return list.isEmpty();
}
}
and now the test method:
public static void main(String[] args) {
Stack stack = new Stack();
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println("top: " + stack.top());
Q1.reverseStack(stack);
System.out.println("top: " + stack.top());
}
It prints as expected:
top: C
top: A