Commit fc1c283c authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

Filer fra forelesning 2

parent a07cc70d
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>inf101v16f02</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
package inf101v16f02;
public interface IStack {
/**
* Push et nytt element på stacken.
*
* @param s Elementet som skal pushes.
*
* Etterpå vil top() == s
*
* (Stabelen må ikke være full, size() < maxSize())
*/
void push(String s); // puttPå
/**
* Pop et element fra stacken.
*
* Øverste element fjernes og returneres
*
* @return Øverste element.
*
* (Stabelen må ha minst ett element, isEmpty() == false)
*/
String pop(); // hent
/**
* Se på øverste element på stacken.
*
* Elementet forblir på stabelen.
*
* @return Øverste element
*
* (Stabelen må ha minst ett element, isEmpty() == false)
*/
String top(); // sePå
/**
* @return Maksimalt antall elementer som kan pushes på stabalen
*/
int maxSize();
/**
* @return True hvis stabelen er tom
*/
boolean isEmpty();
/**
* @return Antall elementer på stabelen
*/
int size();
}
package inf101v16f02;
public class Main {
public static void main(String[] args) {
IStack stack = new MyStack();
stack.push("Eple");
stack.push("Banan");
stack.push("Kaffe");
while (stack.size() < stack.maxSize())
stack.push("Eple");
while( ! stack.isEmpty())
System.out.println(stack.pop());
// String kaffe = stack.pop();
// String banan = stack.pop();
// String eple = stack.pop();
// // String ingenting = stack.pop();
//
// System.out.println("Kaffe=" + kaffe);
// System.out.println("Banan=" + banan);
// System.out.println("Eple=" + eple);
}
}
package inf101v16f02;
public class MyStack implements IStack {
private int length;
private String[] data;
public MyStack() {
super();
this.length = 0;
this.data = new String[20];
}
@Override
public void push(String s) {
data[length] = s;
length = length + 1;
// length++;
}
@Override
public String pop() {
// --length;
length = length - 1;
String x = data[length];
return x;
}
@Override
public String top() {
return data[length-1];
}
@Override
public int maxSize() {
return data.length;
}
@Override
public boolean isEmpty() {
return length == 0;
}
@Override
public int size() {
return length;
}
}
package inf101v16f02;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StackTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void pushPopTest() {
IStack stack = new MyStack();
stack.push("Hei");
assertEquals("Hei", stack.pop());
}
@Test
public void pushPopTest2() {
IStack stack = new MyStack();
stack.push("Hei");
stack.push("Kaffe");
assertEquals("Kaffe", stack.pop());
assertEquals("Hei", stack.pop());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment