Praktikum 8
Interfaces
Traversal.java
public interface Traversal {
public void inorder(Visitor visitor);
}
Tree.java
public interface Tree {
public void add(Object o);
public void remove(Object o);
public Traversal traversal();
}
Visitor.java
public interface Visitor {
public void visit(Object o);
}
Classes
Competitor.java
import java.lang.*;
class Competitor implements Comparable {
public String firstName, lastName, country;
public int age, ranking;
public char gender;
public Competitor(String f, String l, char g, int a, String c, int r) {
firstName = f;
lastName = l;
gender = g;
age = a;
country = c;
ranking = r;
}
public int compareTo(Object o) {
Competitor c = (Competitor) o;
return ranking - c.ranking;
}
public String returnLine() {
String bla = firstName + " " + lastName + " (" + age + ") " + country + " " + ranking + "\n";
return bla;
}
}
RankingTree.java
import java.util.*;
import java.lang.*;
public class RankingTree implements CommandInterpreter {
private SortedBinaryTree rankingList = new SortedBinaryTree();
public String interpret(String command) {
insert(command);
return getList();
}
public void insert(String command) {
StringTokenizer names, personToken;
String line;
String firstName, lastName, country;
int age, ranking;
char gender;
Competitor person;
names = new StringTokenizer(command, "\n");
while (names.hasMoreTokens()) {
firstName = "";
lastName = "";
country = "";
age = 0;
ranking = 0;
gender = ' ';
line = names.nextToken();
personToken = new StringTokenizer(line, " ");
if (personToken.hasMoreTokens()) firstName = personToken.nextToken();
if (personToken.hasMoreTokens()) lastName = personToken.nextToken();
if (personToken.hasMoreTokens()) gender = personToken.nextToken().charAt(0);
if (personToken.hasMoreTokens()) {
try {
age = Integer.parseInt(personToken.nextToken());
}
catch (NumberFormatException e) {
age = 100;
}
}
country = personToken.nextToken();
try {
ranking = Integer.parseInt(personToken.nextToken());
}
catch (NumberFormatException e) {
ranking = 1000000;
}
person = new Competitor(firstName, lastName, gender, age, country, ranking);
rankingList.add(person);
}
}
public String getList() {
RankingVisitor rankingVisitor = new RankingVisitor();
Traversal rankingTraversal = rankingList.traversal();
rankingTraversal.inorder(rankingVisitor);
return rankingVisitor.getList();
}
}
RankingVisitor.java
import java.util.*;
public class RankingVisitor implements Visitor {
private StringBuffer result;
public RankingVisitor() {
result = new StringBuffer();
}
public void visit(Object o) {
if (!(o instanceof TreeNode)) return;
TreeNode t = (TreeNode) o;
if (!(t.element instanceof Competitor)) return;
Competitor c = (Competitor) t.element;
result.append(c.returnLine());
}
public String getList() {
return result.toString();
}
}
SortedBinaryTree.java
import java.util.*;
public class SortedBinaryTree implements Tree {
private TreeNode root;
private TreeNode insertAt(TreeNode node, Comparable x) {
if (node == null) {
return new TreeNode(x);
}
else {
if (x.compareTo(node.element) <= 0)
node.left = insertAt(node.left, x);
else
node.right = insertAt(node.right, x);
return node;
}
}
public void add (Object x) {
root = insertAt(root, (Comparable)x);
}
TreeNode rep;
private TreeNode findRepAt(TreeNode node) {
if (node.right != null) {
node.right = findRepAt(node.right);
return node;
}
else {
rep = node;
return node.left;
}
}
private TreeNode removeAt(TreeNode node, Comparable x) {
if (node == null) {
return null;
}
else {
if (x.compareTo(node.element) == 0) {
if (node.left == null) return node.right;
else if (node.right == null) return node.left;
else {
node.left = findRepAt(node.left);
rep.left = node.left;
rep.right = node.right;
return rep;
}
}
else if (x.compareTo(node.element) <= 0)
node.left = removeAt(node.left, x);
else
node.right = removeAt(node.right, x);
return node;
}
}
public void remove(Object x) {
root = removeAt(root, (Comparable)x);
}
public Traversal traversal() {
return new TreeTraversal(root);
}
}
TreeNode.java
class TreeNode {
Object element;
TreeNode left,right;
TreeNode (Object elem) {
element = elem;
}
}
TreeTraversal.java
import java.util.*;
public class TreeTraversal implements Traversal {
TreeNode root;
public TreeTraversal(TreeNode n) {
root = n;
}
private void inorder(TreeNode node, Visitor visitor) {
if (node != null) {
inorder(node.left, visitor);
visitor.visit(node);
inorder(node.right, visitor);
}
}
public void inorder(Visitor visitor){
inorder(root, visitor);
}
}