Skip to content
Snippets Groups Projects
Commit 8c67b131 authored by Quentin Leblanc's avatar Quentin Leblanc
Browse files

tp2

parent 6e40a7dc
Branches
Tags
No related merge requests found
name := "scala2020"
version := "0.1"
scalaVersion := "2.13.1"
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
}
}
package ch.hepia.tp2
class Stack {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment