Einführung in die objektorientierte Programmierung

Aufg1

class Entry<ET>{
ET element;
Entry<ET> next;
Entry<ET> previous;

Entry(ET elem){
this.element = elem;
}
} class Queue<ET>{

Entry<ET> first;

int size;

Queue(int anzahl){
size=anzahl;
if (size==0) {
first=null;
}
else {
Entry<ET> zeiger=null;
first=new Entry<ET>(null);
Entry<ET> alt= first;
for (int i=1; i<size; i++ ) {
zeiger= new Entry<ET>(null);
alt.next=zeiger;
zeiger.previous=alt;
alt=zeiger;
} // end of for
zeiger.next= first;
first.previous= zeiger;
} // end of if-else
}

public void insert(ET wert){
first = first.next;
first.element=wert;
}

public void print(){
Entry<ET> zeiger=first;
for (int i=1;i<=size ;i++ ) {
if (zeiger.element != null) {
System.out.println(String.valueOf( zeiger.element));
}
zeiger= zeiger.previous;
} // end of while
}


public static void main(String[] args){
Queue<Object> q= new Queue<Object>(4);

q.insert(new String("1"));
q.insert(new String("2"));
q.insert(new String("3"));
// q.insert(new String("4"));
// q.insert(new String("5"));
// q.insert(new String("6"));

q.print();
}

}

Aufg3

import java.util.*;
class Morse{
   LinkedList<String> morsealphabet = new LinkedList<String>();
   
   Morse(){
   morsealphabet.addLast(".-");
   morsealphabet.addLast("-...");
   morsealphabet.addLast("-.-.");
   morsealphabet.addLast("-..");
   morsealphabet.addLast(".");
   morsealphabet.addLast("..-.");
   }
   
   public String code(char b) throws NoSuchElementException{
   int index= ((int) b) - 65;
   //return morsealphabet.get(((int) b) - 65); 
   //  return (index >=0 && index<=5) ? 
   //  morsealphabet.get(((int) b) - 65): "Fehler!"; 
   if (index >=0 && index<=5) {
   return morsealphabet.get(((int) b) - 65);
   } // end of if
   else {
   throw new NoSuchElementException();
   } // end of if-else
   
   }
   
   public static void main(String[] args){
   Morse uebersetzer = new Morse();
   String wort= args[0];
   for (int i=0; i<wort.length() ;i++ ) {
   try{
   System.out.println( uebersetzer.code(wort.charAt(i)) );
   }
   catch (NoSuchElementException e) {
   System.out.println("Geschafft");
   }
   } // end of for
   }
   }

Aufg5

class BinaryNode {
private BinaryNode leftSon, rightSon;
private int value;
public BinaryNode(int i) {
leftSon = rightSon = null;
value = i;
}
public boolean contains(int v) {
if (v<this.value) {
if (this.leftSon==null) {
return false;
} // end of if
else{
return this.leftSon.contains(v);
}
} // end of if
if(v>this.value){
if (this.rightSon==null) {
return false;
} // end of if
else{
return this.rightSon.contains(v);
}
} // end of if-else
return true;
}

public void insert(int v) {
if (v<this.value) {
if (this.leftSon==null) {
this.leftSon = new BinaryNode(v);
} // end of if
else {
this.leftSon.insert(v);
} // end of if-else
} // end of if
else if (v>this.value) {
if (this.rightSon==null) {
this.rightSon = new BinaryNode(v);
} // end of if
else {
this.rightSon.insert(v);
} // end of if-else
} // end of if
}


public void inorder() {
if (this.leftSon != null) {
this.leftSon.inorder();
} // end of if
System.out.println(this.value);
if (this.rightSon != null) {
this.rightSon.inorder();
} // end of if
}

}

public class TestTree {
public static void main(String[] args) {
BinaryNode myTree = new BinaryNode(6);
myTree.insert(5);
myTree.insert(4);
myTree.insert(12);
myTree.insert(11);
myTree.insert(10);

myTree.inorder();

System.out.println(myTree.contains(44) );
}
}

Pakete

Beispiel:

In G:\fernuni_hamburg\objektor_sprachen\2017\Pakete\paketA befindet sich:

package paketA;
import paketB.*;
public class A{
   public static void main(String[] args){
   B b =new B();
   b.m(7);
   }
}

In G:\fernuni_hamburg\objektor_sprachen\2017\Pakete\paketB befindet sich:

package paketB;
public class B{
   public void m(int n){
   System.out.println(n);
   }
}
Im Class Path steht

G:\Fernuni_Hamburg\Objektor_Sprachen\2017\Pakete