Commit 4324fad0 authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

Kode fra forelesningen

parent 9923836b
package inf101.v16.datastructures;
public interface IList {
public interface IList<T> {
/**
* Legg til et element på slutten av listen.
*
......@@ -8,7 +8,7 @@ public interface IList {
*
* Etterpå vil size() øke med én, og get(size()-1) vil returnere elementet.
*/
void add(String s);
void add(T s);
/**
* Fjern et element fra listen
......@@ -18,18 +18,39 @@ public interface IList {
*
* Etterpå vil alle senere indekser i listen flyttes én posisjon frem.
*/
String remove(int i);
T remove(int i);
/**
* Returner elementet på posisjon i
* @param i
* @return
*/
String get(int i);
T get(int i);
/**
* @return True hvis listen er tom
*/
boolean isEmpty();
/**
* @return Antall elementer i listen
*/
int size();
void set(int i, String s);
/**
* Sett elementet på indeks i til s
*
* @param i Index
* @param s Nytt element
*
* Etterpå vil get(i) == s
*/
void set(int i, T s);
/**
* Fjern alle elementer fra listen.
*
* Etterpå vil size() == 0
*/
void clear();
}
package inf101.v16.datastructures;
public interface IStack {
public interface IStack<T> {
/**
* Push et nytt element på stacken.
......@@ -11,7 +11,7 @@ public interface IStack {
*
* (Stabelen må ikke være full, size() < maxSize())
*/
void push(String s); // puttPå
void push(T s); // puttPå
/**
* Pop et element fra stacken.
......@@ -22,7 +22,7 @@ public interface IStack {
*
* (Stabelen må ha minst ett element, isEmpty() == false)
*/
String pop(); // hent
T pop(); // hent
/**
* Se på øverste element på stacken.
......@@ -33,7 +33,7 @@ public interface IStack {
*
* (Stabelen må ha minst ett element, isEmpty() == false)
*/
String top(); // sePå
T top(); // sePå
/**
* @return Maksimalt antall elementer som kan pushes på stabalen
......
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);
}
}
......@@ -2,37 +2,46 @@ package inf101.v16.datastructures;
import java.util.Arrays;
public class MyList implements IList, IStack {
private int length = 0;
private String data[] = new String[10];
public class MyList<T> implements IList<T> {
private int length;
private T[] data;
public MyList() {
length = 0;
data = (T[]) new Object[10];
}
public MyList(int initialSize) {
if(initialSize < 0)
throw new IllegalArgumentException("initialSize must be positive: " + initialSize);
length = 0;
data = (T[]) new Object[initialSize];
}
@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);
public void add(T s) {
if (length == data.length) {
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];
public T remove(int i) {
T element = data[i];
for (int x = i; x < length - 1; x++) {
data[x] = data[x + 1];
}
length = length -1;
length = length - 1;
return element;
}
@Override
public String get(int i) {
public T get(int i) {
return data[i];
}
......@@ -47,30 +56,10 @@ public class MyList implements IList, IStack {
}
@Override
public void set(int i, String s) {
public void set(int i, T 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;
import static org.junit.Assert.*;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MyListTest {
Random random = new Random();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void setGetTest() {
IList<String> list = new MyList<>();
// enkelt forsøk på å lage tilfeldige testdata
for(int i = 0; i < random.nextInt(10000)+1; i++)
list.add(Integer.toString(random.nextInt()));
for(int i = 0; i < 10000; i++) {
setGetProperty(list, random.nextInt(list.size()), "banan");
}
}
@Test
public void setGetTest2() {
IList<String> list = new MyList<>();
list.add("Hei");
list.add("Hadet");
setGetProperty2(list, 1, 0, "banan");
setGetProperty2(list, 0, 1, "fdsaf");
}
/**
* Egenskap: hvis vi kaller add(), vil siste element være verdien vi la til
*
* @param list
* @param s
*/
public void addGetProperty(IList<String> list, String s) {
list.add(s);
assertEquals(s, list.get(list.size()-1));
}
/**
* Egenskap: hvis vi kaller add(), vil størrelsen øke med 1
*
* @param list
* @param s
*/
public void addSizeProperty(IList<String> list, String s) {
int size = list.size();
list.add(s);
assertEquals(size+1, list.size());
}
/**
* Egenskap: hvis vi kaller set(), vil get() returnere samme verdi
*
* @param list
* @param i
* @param s
*/
public void setGetProperty(IList<String> list, int i, String s) {
list.set(i, s);
assertEquals(s, list.get(i));
}
/**
* Egenskap: hvis vi kaller set(), vil verdiene på andre indekser ikke endre seg
*
* @param list
* @param i
* @param j
* @param s
*/
public void setGetProperty2(IList<String> list, int i, int j, String s) {
if(i != j) {
String jString = list.get(j);
list.set(i, s);
assertEquals(jString, list.get(j));
}
}
}
package inf101.v16.datastructures;
public class MyStack implements IStack {
public class MyStack<T> implements IStack<T> {
private int length;
private String[] data;
private T[] data;
public MyStack() {
super();
this.length = 0;
this.data = new String[20];
this.data = (T[]) new Object[20];
}
@Override
public void push(String s) {
public void push(T s) {
data[length] = s;
length = length + 1;
// length++;
}
@Override
public String pop() {
public T pop() {
// --length;
length = length - 1;
String x = data[length];
T x = data[length];
return x;
}
@Override
public String top() {
public T top() {
return data[length-1];
}
......
......@@ -18,14 +18,14 @@ public class StackTest {
@Test
public void pushPopTest() {
IStack stack = new MyStack();
IStack<String> stack = new MyStack<>();
stack.push("Hei");
assertEquals("Hei", stack.pop());
}
@Test
public void pushPopTest2() {
IStack stack = new MyStack();
IStack<String> stack = new MyStack<>();
stack.push("Hei");
stack.push("Kaffe");
......@@ -35,7 +35,7 @@ public class StackTest {
@Test
public void clearTest() {
IStack stack = new MyStack();
IStack<String> stack = new MyStack<>();
stack.push("Hei");
stack.push("Kaffe");
......
......@@ -3,25 +3,25 @@ package inf101.v16.datastructures;
import java.util.ArrayList;
import java.util.List;
public class YourStack implements IStack {
private List<String> data;
public class YourStack<T> implements IStack<T> {
private List<T> data;
public YourStack() {
this.data = new ArrayList<String>();
this.data = new ArrayList<T>();
}
@Override
public void push(String s) {
public void push(T s) {
data.add(s);
}
@Override
public String pop() {
public T pop() {
return data.remove(data.size() - 1);
}
@Override
public String top() {
public T top() {
return data.get(data.size() - 1);
}
......
......@@ -18,14 +18,14 @@ public class YourStackTest {
@Test
public void pushPopTest() {
IStack stack = new YourStack();
IStack<String> stack = new YourStack<>();
stack.push("Hei");
assertEquals("Hei", stack.pop());
}
@Test
public void pushPopTest2() {
IStack stack = new YourStack();
IStack<String> stack = new YourStack<>();
stack.push("Hei");
stack.push("Kaffe");
......@@ -35,7 +35,7 @@ public class YourStackTest {
@Test
public void clearTest() {
IStack stack = new YourStack();
IStack<String> stack = new YourStack<>();
stack.push("Hei");
stack.push("Kaffe");
......
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