diff --git a/TP2/build.sbt b/TP2/build.sbt new file mode 100644 index 0000000000000000000000000000000000000000..3b5eaa87a12c8b7d3e3dbf378f6c145ee4c23ee8 --- /dev/null +++ b/TP2/build.sbt @@ -0,0 +1,5 @@ +name := "scala2020" + +version := "0.1" + +scalaVersion := "2.13.1" diff --git a/TP2/src/main/scala/ch/hepia/tp2/Stack.scala b/TP2/src/main/scala/ch/hepia/tp2/Stack.scala new file mode 100644 index 0000000000000000000000000000000000000000..f6c6652c7f63bd91af53f193c8e417b0937b772a --- /dev/null +++ b/TP2/src/main/scala/ch/hepia/tp2/Stack.scala @@ -0,0 +1,42 @@ +package ch.hepia.tp2 + +import java.util._ + +class Stack[A] { + + private val content: List[A] = new ArrayList[A] + + //Return true if Stack is Empty + def isEmpty: Boolean = content.isEmpty + + //return size of stack + def size: Int = content.size + + //stack an element on the stack + def push(a: A): Unit = content.add(0, a) + + //Unstack an element and return it + def pop: A = { + if(this.size == 0 ) { + throw new NoSuchElementException + }else{ + content.remove(0) + } + } + + //Swap the 2 first elements of the stack, do nothing if less than 1 element + def swap: Unit = { + if(this.size > 1) { + this.push(content.remove(1)) + } + } + + override def toString: String = { + val sb: StringBuilder = new StringBuilder + sb.append("[") + this.content.forEach((e) => sb.append(e).append(", ")) + sb.append("]") + sb.toString + } + +} diff --git a/src/main/scala/ch/hepia/tp2/Stack.scala b/src/main/scala/ch/hepia/tp2/Stack.scala new file mode 100644 index 0000000000000000000000000000000000000000..240f0916256a8b263ced33f8e83308a0e40e448a --- /dev/null +++ b/src/main/scala/ch/hepia/tp2/Stack.scala @@ -0,0 +1,5 @@ +package ch.hepia.tp2 + +class Stack { + +}