Add files for exercise 09

This commit is contained in:
Manuel Thalmann 2022-11-15 14:22:00 +01:00
parent 785b359d37
commit 29bd296ceb
7 changed files with 3670 additions and 0 deletions

3166
HashRangZuerich.csv Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,45 @@
package ch.nuth.zhaw.exbox;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
public class HashServer implements CommandExecutor {
private final static int STARTNR = 0;
private final static int NAME = 1;
private final static int TIME = 2;
private final Map<MyCompetitor, MyCompetitor> data = new MyHashtable<>(4000);
private long parseTime(String s) throws Exception {
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
Date date = sdf.parse(s);
return date.getTime();
}
public void load(Map<MyCompetitor, MyCompetitor> data, String list) {
String[] lines = list.split("\n");
for (String line : lines) {
String[] items = line.split(";");
MyCompetitor c = new MyCompetitor(Integer.parseInt(items[STARTNR]), // startNr
items[NAME], // name
items[TIME]); // time
data.put(c, c);
}
}
public String execute(String arg) {
if (arg.toUpperCase().startsWith("GET")) {
String[] items = arg.substring(3).trim().split(";");
MyCompetitor key = new MyCompetitor(Integer.parseInt(items[1]), items[0], "00:00:00.0");
MyCompetitor o = data.get(key);
if (o != null) {
return items[0] + " " + items[1] + " -> " + o + "\n";
}
return "not found\n";
}
load(data, arg);
return "" + data.size() + " loaded\n";
}
}

View file

@ -0,0 +1,65 @@
package ch.nuth.zhaw.exbox;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyCompetitor implements Comparable<MyCompetitor> {
private final String name;
private final String time;
private int rank;
public MyCompetitor(int rank, String name, String time) {
this.rank = rank;
this.name = name;
this.time = time;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getTime() {
return time;
}
public String getName() {
return name;
}
private static long parseTime(String s) {
try {
DateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(s);
return date.getTime();
} catch (Exception e) {System.err.println(e);}
return 0;
}
private static String timeToString(int time) {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(new Date(time));
}
public String toString() {
return ""+ rank + " "+name+" "+time;
}
@Override
public int compareTo(MyCompetitor o) {
// to be done
return -1;
}
@Override
public int hashCode() {
// to be done
return -1;
}
@Override
public boolean equals (Object o) {
// to be done
return false;
}
}

View file

@ -0,0 +1,96 @@
package ch.nuth.zhaw.exbox;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
@SuppressWarnings("unchecked")
public class MyHashtable<K, V> implements Map<K, V> {
private K[] keys = (K[]) new Object[10];
private V[] values = (V[]) new Object[10];
private int hash(Object k) {
int h = Math.abs(k.hashCode());
return h % keys.length;
}
public MyHashtable(int size) {
// to be done
}
// Removes all mappings from this map (optional operation).
public void clear() {
// to be done
throw new UnsupportedOperationException();
}
// Associates the specified value with the specified key in this map (optional operation).
public V put(K key, V value) {
// to be done
throw new UnsupportedOperationException();
}
// Returns the value to which this map maps the specified key.
public V get(Object key) {
// to be done
throw new UnsupportedOperationException();
}
// Removes the mapping for this key from this map if present (optional operation).
public V remove(Object key) {
// to be done (Aufgabe 3)
throw new UnsupportedOperationException();
}
// Returns the number of key-value mappings in this map.
public int size() {
// to be done
throw new UnsupportedOperationException();
}
// UnsupportedOperationException ===================================================================
// Returns a collection view of the values contained in this map.
public Collection<V> values() {
throw new UnsupportedOperationException();
}
// Returns true if this map contains a mapping for the specified key.
public boolean containsKey(Object key) {
throw new UnsupportedOperationException();
}
// Returns true if this map maps one or more keys to the specified value.
public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}
// Returns a set view of the mappings contained in this map.
public Set<Map.Entry<K, V>> entrySet() {
throw new UnsupportedOperationException();
}
// Compares the specified object with this map for equality.
public boolean equals(Object o) {
throw new UnsupportedOperationException();
}
// Copies all of the mappings from the specified map to this map (optional operation).
public void putAll(Map<? extends K, ? extends V> t) {
throw new UnsupportedOperationException();
}
// Returns the hash code value for this map.
public int hashCode() {
throw new UnsupportedOperationException();
}
// Returns true if this map contains no key-value mappings.
public boolean isEmpty() {
throw new UnsupportedOperationException();
}
// Returns a set view of the keys contained in this map.
public Set<K> keySet() {
throw new UnsupportedOperationException();
}
}

View file

@ -0,0 +1,35 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS9_2_test {
MyCompetitor c1 = new MyCompetitor(1, "Kiptum Daniel", "02:11:31");
MyCompetitor c2 = new MyCompetitor(1, "Kiptum Daniel", "02:11:31");
MyCompetitor c3 = new MyCompetitor(2, "Ancay Tarcis", "02:20:02");
@Test
public void testEquals() {
assertEquals(c1, c2);
assertNotEquals(c1, c3);
}
@Test
public void testCompareTo() {
assertEquals(0, c1.compareTo(c2));
assertNotEquals(0, c1.compareTo(c3));
}
@Test
public void testHashcode() {
assertEquals(c1.hashCode(), c2.hashCode());
assertNotEquals(c1.hashCode(), c3.hashCode());
}
}

View file

@ -0,0 +1,144 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Town {
int hashCode;
String name;
String nb;
Town (int hashCode, String name, String nb) {
this.name = name; this.hashCode = hashCode; this.nb = nb;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Town)) return false;
return ((Town)o).name.equals(this.name);
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public String toString() {
return name + " " + hashCode;
}
}
public class ADS9_3_test {
Map<Town, Town> hashmap;
List<Town> towns;
@BeforeEach
public void setUp() {
towns = List.of(
new Town(5, "Bari", "BA"),
new Town(8, "Bologna", "BO"),
new Town(3, "Catania", "CA"),
new Town(9, "Firenze", "FI"),
new Town(0, "Genova", "GV"),
new Town(12, "Milano", "MI"),
new Town(7, "Napoli", "NA"),
new Town(7, "Palermo", "PA"),
new Town(7, "Roma", "RM"),
new Town(5, "Torino", "TO")
);
hashmap = new MyHashtable<>(100);
}
@Test
public void testAdd() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd2() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.put(t1, t1);
Town t2 = hashmap.get(t0);
assertEquals(t0, t2);
t2 = hashmap.get(t1);
assertEquals(t1, t2);
}
@Test
public void testAdd3() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.remove(t0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd4() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
}
@Test
public void testSize() {
hashmap.clear();
assertEquals(0, hashmap.size());
testAdd2();
assertEquals(2, hashmap.size());
}
@Test
public void testRemove() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.remove(t0);
assertEquals(0, hashmap.size());
hashmap.put(t0, t0);
hashmap.remove(t1);
assertEquals(1, hashmap.size());
hashmap.remove(t0);
assertEquals(0, hashmap.size());
}
@Test
public void testMixed() {
hashmap.clear();
Map<Town, Town> hashmap2 = new HashMap<>();
for (int i = 0; i < 10000; i++) {
Town c = towns.get((int)(Math.random() * towns.size()));
int op = (int)(Math.random() * 2);
switch (op) {
case 0: hashmap.put(c, c); hashmap2.put(c, c); break;
case 1: hashmap.remove(c); hashmap2.remove(c); break;
}
}
assertEquals(hashmap2.size(), hashmap.size());
for (Town t : towns) {
Town c1 = hashmap.get(t);
Town c2 = hashmap2.get(t);
assertEquals(c1, c2);
}
}
}

View file

@ -0,0 +1,119 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ADS9_4_test {
Map<Town, Town> hashmap;
List<Town> towns;
@BeforeEach
public void setUp() {
towns = List.of(
new Town(5, "Bari", "BA"),
new Town(8, "Bologna", "BO"),
new Town(3, "Catania", "CA"),
new Town(9, "Firenze", "FI"),
new Town(0, "Genova", "GV"),
new Town(12, "Milano", "MI"),
new Town(7, "Napoli", "NA"),
new Town(7, "Palermo", "PA"),
new Town(7, "Roma", "RM"),
new Town(5, "Torino", "TO")
);
hashmap = new MyHashtable<>(2);
}
@Test
public void testAdd() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd2() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.put(t1, t1);
Town t2 = hashmap.get(t0);
assertEquals(t0, t2);
t2 = hashmap.get(t1);
assertEquals(t1, t2);
}
@Test
public void testAdd3() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.remove(t0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd4() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
}
@Test
public void testSize() {
hashmap.clear();
assertEquals(0, hashmap.size());
testAdd2();
assertEquals(2, hashmap.size());
}
@Test
public void testRemove() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.remove(t0);
assertEquals(0, hashmap.size());
hashmap.put(t0, t0);
hashmap.remove(t1);
assertEquals(1, hashmap.size());
hashmap.remove(t0);
assertEquals(0, hashmap.size());
}
@Test
public void testMixed() {
hashmap.clear();
Map<Town, Town> hashmap2 = new HashMap<>();
for (int i = 0; i < 1000; i++) {
Town c = towns.get((int)(Math.random() * towns.size()));
int op = (int)(Math.random() * 2);
switch (op) {
case 0: hashmap.put(c, c); hashmap2.put(c, c); break;
case 1: hashmap.remove(c); hashmap2.remove(c); break;
}
}
assertEquals(hashmap2.size(), hashmap.size());
for (Town t : towns) {
Town c1 = hashmap.get(t);
Town c2 = hashmap2.get(t);
assertEquals(c1, c2);
}
}
}