Document

Werbung
6. AP 2 Praktikum
Die Aufgabenblätter sind hier zu finden: http://www.gm.fh-koeln.de/~ehses/ap/index.html
Aufgabe 1:
Node.java:
package search.util;
public class Node {
public Object value;
public Node next;
Node (Object value, Node next) {
this.value = value;
this.next = next;
}
}
ConcatenateList.java:
package search.util;
import java.util.Comparator;
public class ConcatenateList {
private Node first = null;
public boolean isEmpty() {
return first == null;
}
public Object removeFirst() {
Object retValue = first.value;
first = first.next;
return retValue;
}
public void addLast(Object o) {
Node newNode = new Node(o, null);
if (first == null) {
first = newNode;
}
else {
Node temp = first;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
public void clear() {
first = null;
}
public void add(Object o, Comparator cmp) {
if ( isEmpty() ) {
first = new Node(o, null);
}
else {
Node temp = first;
Node temp_prev = null;
while ( temp != null && cmp.compare(o, temp.value) > 0 ) {
temp_prev = temp;
temp = temp.next;
}
if (temp == null) {
temp_prev.next = new Node (o, null);
}
else if (temp == first){
first = new Node (o, temp);
}
else{
temp_prev.next = new Node (o, temp);
}
}
}
public Object removeLast() {
Node temp = first;
Node temp_prev = null;
while (temp.next != null) {
temp_prev = temp;
temp = temp.next;
}
if (temp == first) {
first = null;
return temp.value;
}
else {
temp_prev.next = null;
return temp.value;
}
}
}
FIFOQueue.java:
package search.util;
import java.util.NoSuchElementException;
public final class FIFOQueue implements IQueue {
private ConcatenateList snake = new ConcatenateList();
public void clear() {
snake.clear();
}
public Object get() {
if (this.isEmpty()) throw new NoSuchElementException(“Queue is empty”);
return snake.removeFirst();
}
public boolean isEmpty() {
return snake.isEmpty();
}
public void put(Object p) {
snake.addLast(p);
}
}
LIFOQueue.java:
package search.util;
import java.util.NoSuchElementException;
public final class LIFOQueue implements IQueue {
private ConcatenateList stack = new ConcatenateList();
public void clear() {
stack.clear();
}
public Object get() {
if (this.isEmpty()) throw new NoSuchElementException(“Queue is empty”);
return stack.removeLast();
}
public boolean isEmpty() {
return stack.isEmpty();
}
public void put(Object p) {
stack.addLast(p);
}
}
PriorityQueue.java:
package search.util;
import java.util.Comparator;
import java.util.NoSuchElementException;
public final class PriorityQueue implements IQueue {
private ConcatenateList prio = new ConcatenateList();
private Comparator cmp;
public PriorityQueue(Comparator cmp) {
this.cmp = cmp;
}
public void clear() {
prio.clear();
}
public Object get() {
if (this.isEmpty()) throw new NoSuchElementException(“Queue is empty”);
return prio.removeFirst();
}
public boolean isEmpty() {
return prio.isEmpty();
}
public void put(Object p) {
prio.add(p, this.cmp);
}
}
Aufgabe 3:
numberOfNodes():
public static int numberOfNodes(ITreeNode root) {
if(root==null){
return 0;
}
else{
int countNodes = 1;
for (Iterator iter = root.getChildren().iterator(); iter.hasNext();) {
ITreeNode child = (ITreeNode) iter.next();
countNodes += numberOfNodes(child);
}
return countNodes;
}
}
Aufgabe 4:
getPathToGoal():
public static List getPathToGoal(ITreeNode root, Object goalNode) {
List pathList = null;
if (root == null) {
return pathList;
}
if (goalNode.equals(root.getValue())) {
pathList = new ArrayList();
pathList.add(goalNode.toString());
return pathList;
}
for (Iterator iter = root.getChildren().iterator(); iter.hasNext();) {
ITreeNode child = (ITreeNode) iter.next();
pathList = getPathToGoal(child, goalNode);
if (pathList != null) {
pathList.add(0, root.getValue().toString());
return pathList;
}
}
return pathList;
}
5. AP 2 Praktikum
Bevor ihr euch die Lösungen anschaut eines vorweg: Die Lösungen dieses Praktikums dienen
wirklich nur zum Vergleich oder als Hilfestellung, wenn irgendwo etwas noch hapern sollte
im Programmcode.
Ihr müsst, um das Praktikum bestehen zu können, den Zusammenhang der einzelnen Klassen
untereinander sowie die Sortier- und Suchalgorithmen verstanden haben und auch mit eigenen
Worten im Praktikum erklären können!
Aufgabe 2a:
ConsoleLogger.java:
public final class ConsoleLogger extends AbstractLogger{
protected void logMsg(String msg, int level) {
System.out.println(composeLogMsg(msg, level));
}
public void close() {
System.out.close();
}
}
FileLogger.java:
import java.io.*;
public final class FileLogger extends AbstractLogger{
private PrintStream uebergabe;
public FileLogger(String string) throws FileNotFoundException {
uebergabe = new PrintStream(new FileOutputStream(string));
}
protected void logMsg(String msg, int level){
uebergabe.println(composeLogMsg(msg, level));
}
public void close() {
System.out.close();
}
}
Aufgabe 3b:
MyArrays.java:
static void merge(int[] src, int[] dest, int lo, int mid, int hi) {
Log.info(“src :”+arrayToString(src, lo, mid, hi));
int end_lo=mid;
int start_hi=mid;
int srclo=lo;
int destlo=lo;
while(srclo < end_lo && start_hi < hi){
if(src[srclo]<src[start_hi]){
dest[destlo++]=src[srclo++];
}
else{
dest[destlo++]=src[start_hi++];
}
}
while(srclo<end_lo){
dest[destlo++]=src[srclo++];
}
while(start_hi<hi){
dest[destlo++]=src[start_hi++];
}
Log.info(“dest:”+arrayToString(dest, lo, mid, hi));
}
//Hier noch ein dickes Dankeschön an meinen Kommilitonen für den Hinweis, dass noch eine
Variable fehlte! Er weiß wer gemeint ist.
public static int binSearch(int[] feld, int x) {
int start = 0;
int end = feld.length;
int middle = (start+end)/2;
while(start<end && feld !=null){
Log.info(arrayToString(feld, start, middle, end));
if(x==feld[middle]){
Log.info(“found at ” + middle);
return middle;
}
else if(x<feld[middle]){
end = middle;
}
else {
start = middle+1;
}
middle = (start+end)/2;
}
Log.info(“not found, return value = ” + -(middle+1));
return -(middle+1);
}
4. AP 2 Praktikum
Aufgabe 1:
Die Testklassen CircleTest und RectangleTest findet ihr unter Aufgabe 2!
Circle.java:
public class Circle extends AbstractShape{
private double radius;
Circle(String name, double radius){
super(name);
this.radius=radius;
}
public double getArea(){
double temp = Math.PI * radius * radius;
return temp;
}
}
Rectangle.java:
public class Rectangle extends AbstractShape{
private double laenge, breite;
Rectangle(String name, double laenge, double breite){
super(name);
this.laenge=laenge;
this.breite=breite;
}
public double getArea(){
double temp = laenge * breite;
return temp;
}
}
AbstractShape.java:
public String toString() {
return getClass().getName()+”.”+ name;
}
Aufgabe 2:
AbstractShapeTest.java:
import junit.framework.TestCase;
public class AbstractShapeTest extends TestCase {
protected IShape s1;
protected IShape s2;
protected IShape s3;
public void testArea() {
double area13 = Math.PI * 10.5 * 10.5;
double area2 = Math.PI * 11.0 * 11.0;
assertEquals(area13, s1.getArea(), 1e-7);
assertEquals(area2, s2.getArea(), 1e-7);
assertEquals(area13, s3.getArea(), 1e-7);
}
public void testName() {
assertEquals(“a”, s1.getName());
assertEquals(“b”, s2.getName());
assertEquals(“c”, s3.getName());
}
public void testCompare() {
assertEquals(0, s1.compareTo(s3));
assertEquals(0, s3.compareTo(s1));
assertTrue(s1.compareTo(s2) < 0);
assertTrue(s2.compareTo(s1) > 0);
assertTrue(s3.compareTo(s2) < 0);
assertTrue(s2.compareTo(s3) > 0);
}
public void testEquals() {
assertFalse(s1.equals(s2));
assertFalse(s1.equals(s3));
assertFalse(s3.equals(s2));
}
public void testToString() {
String className = s1.getClass().getName();
assertEquals(className+”.a”, s1.toString());
assertEquals(className+”.b”, s2.toString());
assertEquals(className+”.c”, s3.toString());
}
}
CircleTest.java:
public class CircleTest extends AbstractShapeTest {
public void setUp() {
s1 = new Circle(“a”, 10.5);
s2 = new Circle(“b”, 11.0);
s3 = new Circle(“c”, 10.5);
}
}
RectangleTest.java:
public class RectangleTest extends AbstractShapeTest {
public void setUp() {
s1 = new Rectangle(“a”, Math.PI, 10.5 * 10.5);
s2 = new Rectangle(“b”, Math.PI, 11.0 * 11.0);
s3 = new Rectangle(“c”, Math.PI, 10.5 * 10.5);
}
}
Aufgabe 3:
ShapeNameComparator.java:
import java.util.Comparator;
public class ShapeNameComparator implements Comparator{
public int compare(Object arg0, Object arg1) {
return (((IShape)arg0).getName().compareTo(((IShape)arg1).getName()));
}
}
Herunterladen