Commit 9923836b authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

kopiert fra f04

parent 70e192c2
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<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>inf101v16f05</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>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
package inf101.v16.datastructures;
public interface IList {
/**
* Legg til et element på slutten av listen.
*
* @param s Elementet som skal legges til
*
* Etterpå vil size() øke med én, og get(size()-1) vil returnere elementet.
*/
void add(String s);
/**
* Fjern et element fra listen
*
* @param i Indeksen til elementet
* @return elementet som ble fjernet
*
* Etterpå vil alle senere indekser i listen flyttes én posisjon frem.
*/
String remove(int i);
/**
* Returner elementet på posisjon i
* @param i
* @return
*/
String get(int i);
boolean isEmpty();
int size();
void set(int i, String s);
}
package inf101.v16.datastructures;
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();
/**
Slett alle elementene fra stabelen
*/
void clear();
}
package inf101.v16.datastructures;
public class Main {
public static void main(String[] args) {
IList list = new MyList();
list.add("Eple");
list.add("Banan");
list.add("Kaffe");
while (list.size() < 20)
list.add("Eple");
while( ! list.isEmpty())
System.out.println(list.remove(list.size()-1));
String kaffe = list.remove(list.size()-1);
String banan = list.remove(list.size()-1);
String eple = list.remove(list.size()-1);
// // String ingenting = stack.pop();
//
System.out.println("Kaffe=" + kaffe);
System.out.println("Banan=" + banan);
System.out.println("Eple=" + eple);
}
}
package inf101.v16.datastructures;
import java.util.Arrays;
public class MyList implements IList, IStack {
private int length = 0;
private String data[] = new String[10];
@Override
public void add(String s) {
if(length == data.length) {
System.out.println("Dobler størrelsen av listen!");
//String newData[] = new String[data.length*2];
//for(....) { .... }
data = Arrays.copyOf(data, data.length*2);
}
data[length] = s;
length = length + 1;
}
@Override
public String remove(int i) {
String element = data[i];
for(int x = i; x < length-1; x++) {
data[x] = data[x+1];
}
length = length -1;
return element;
}
@Override
public String get(int i) {
return data[i];
}
@Override
public boolean isEmpty() {
return length == 0;
}
@Override
public int size() {
return length;
}
@Override
public void set(int i, String s) {
data[i] = s;
}
@Override
public void push(String s) {
add(s);
}
@Override
public String pop() {
return remove(size()-1);
}
@Override
public String top() {
return get(size()-1);
}
@Override
public int maxSize() {
return Integer.MAX_VALUE;
}
@Override
public void clear() {
length = 0;
}
}
package inf101.v16.datastructures;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MyListStackTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void pushPopTest() {
IStack stack = new MyList();
stack.push("Hei");
assertEquals("Hei", stack.pop());
}
@Test
public void pushPopTest2() {
IStack stack = new MyList();
stack.push("Hei");
stack.push("Kaffe");
assertEquals("Kaffe", stack.pop());
assertEquals("Hei", stack.pop());
}
@Test
public void clearTest() {
IStack stack = new MyList();
stack.push("Hei");
stack.push("Kaffe");
stack.clear();
// test
assertTrue(stack.isEmpty());
assertEquals(0, stack.size());
}
}
package inf101.v16.datastructures;
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;
}
@Override
public void clear() {
for(int i = 0; i < length; i++)
data[i] = null;
length = 0;
}
}
package inf101.v16.datastructures;
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());
}
@Test
public void clearTest() {
IStack stack = new MyStack();
stack.push("Hei");
stack.push("Kaffe");
stack.clear();
// test
assertTrue(stack.isEmpty());
assertEquals(0, stack.size());
}
}
package inf101.v16.datastructures;
import java.util.ArrayList;
import java.util.List;
public class YourStack implements IStack {
private List<String> data;
public YourStack() {
this.data = new ArrayList<String>();
}
@Override
public void push(String s) {
data.add(s);
}
@Override
public String pop() {
return data.remove(data.size() - 1);
}
@Override
public String top() {
return data.get(data.size() - 1);
}
@Override
public int maxSize() {
return Integer.MAX_VALUE;
}
@Override
public boolean isEmpty() {
return data.isEmpty();
}
@Override
public int size() {
return data.size();
}
@Override
public void clear() {
data.clear();
}
}
package inf101.v16.datastructures;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class YourStackTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void pushPopTest() {
IStack stack = new YourStack();
stack.push("Hei");
assertEquals("Hei", stack.pop());
}
@Test
public void pushPopTest2() {
IStack stack = new YourStack();
stack.push("Hei");
stack.push("Kaffe");
assertEquals("Kaffe", stack.pop());
assertEquals("Hei", stack.pop());
}
@Test
public void clearTest() {
IStack stack = new YourStack();
stack.push("Hei");
stack.push("Kaffe");
stack.clear();
// test
assertTrue(stack.isEmpty());
assertEquals(0, stack.size());
}
}
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