Einführung in die objektorientierte Programmierung

Aufgabe 1

class LinkedList1618<ET> {
private Entry header = new Entry(null, null, null);
private int size = 0;
public LinkedList1618() {
header.next = header;
header.previous = header;
}
public ET getLast() {
if (size == 0) throw new java.util.NoSuchElementException();
return header.previous.element;
}
public ET removeLast() {
Entry lastEntry = header.previous;
if (lastEntry == header) throw new java.util.NoSuchElementException();
lastEntry.previous.next = lastEntry.next;
lastEntry.next.previous = lastEntry.previous;
size--;
return lastEntry.element;
}
public void addLast(ET e) {
Entry newEntry = new Entry(e, header, header.previous);
header.previous.next = newEntry;
header.previous = newEntry;
size++;
}

public int size() {
return size;
}
private class Entry {
private ET element;
private Entry next;
private Entry previous;
private Entry(ET element, Entry next, Entry previous) {
this.element = element;
this.next = next;
this.previous = previous;
}
}
public class ListIterator {
private int nextIndex = 0;
private Entry next = header.next;
boolean hasNext() {
return nextIndex != size;
}
ET next() {
if (nextIndex == size) throw new java.util.NoSuchElementException();
ET elem = next.element;
next = next.next;
nextIndex++;
return elem;
}
}
public ListIterator listIterator() {
return new ListIterator();
}
}

Aufgabe 2

class MaximumContainer<T extends Comparable<T>>{
T max;

public void store(T o){
if (max==null || max.compareTo(o)<0) {
max=o;
} // end of if
}

public T get(){
return max;
}

public static void main(String[] args){
MaximumContainer<Obst> m= new MaximumContainer<Obst>();

Apfel a= new Apfel(3);
m.store(a);
System.out.println(m.get().gewicht);

Birne b= new Birne(10);
m.store(b);
System.out.println(m.get().gewicht);

a= new Apfel(5);
m.store(a);
System.out.println(m.get().gewicht);

}

} abstract class Obst implements Comparable<Obst>{
final int gewicht;

Obst(int gewicht){
this.gewicht= gewicht;
}

public int compareTo(Obst o){
return this.gewicht-o.gewicht;
}


} class Apfel extends Obst{
Apfel(int gewicht){
super(gewicht);
}

} class Birne extends Obst{
Birne(int gewicht){
super(gewicht);
}

}

Aufgabe 3

 
import java.io.*;
interface AddressBook extends Serializable{
   void addPerson(Person person);
   void print();
   }

class AddressBookException extends Exception {
AddressBookException(String message) {
super(message);
}
} class ArrayListAddressBook implements AddressBook {
private java.util.ArrayList<Person> persons;
public ArrayListAddressBook(int initialCapacity) {
initialCapacity = initialCapacity < 1 ? 1 : initialCapacity;
persons = new java.util.ArrayList<Person>(initialCapacity);
}
@Override
public void addPerson(Person person) {
persons.add(person);
}
@Override
public void print() {
for (Person person : persons) {
System.out.println(person.getName());
}
}

static public void main(String[] args){
ArrayListAddressBook ab= new ArrayListAddressBook(3);
ab.addPerson(new Person("Hans"));
ab.addPerson(new Person("Ute"));
ab.addPerson(new Person("Bernd"));

SerializationToFilePersistence speicher=
new SerializationToFilePersistence();

// try{
// speicher.storeBook(ab,"adressen1");
//
// }
// catch(AddressBookException e){
// System.out.println(e );
// }

AddressBook gelesen=null;

try{
gelesen = speicher.loadBook("adressen1");

}
catch(AddressBookException e){
System.out.println(e );
}

gelesen.print();

}
}
interface FullPersistence {
AddressBook loadBook(String name) throws AddressBookException;
void storeBook(AddressBook book, String name) throws AddressBookException;
}
import java.io.*;
class Person implements Serializable{
   private String name;
   public Person(String name) {
   this.name = name;
   }
   public String getName() {
   return name;
   }
   }
import java.io.*;
class SerializationToFilePersistence implements FullPersistence{
   
   public AddressBook loadBook(String name) throws AddressBookException {
   try{
   InputStream is = new FileInputStream(name);
   ObjectInputStream ois = new ObjectInputStream(is);
   AddressBook ab = (AddressBook) ois.readObject();
   ois.close();
   return ab;
   }
   catch(Exception e){
   throw new AddressBookException("Datei nicht gefunden");
   }
   
   }
   
   public void storeBook(AddressBook book, String name) throws AddressBookException{
   try{
   OutputStream os = new FileOutputStream(name);
   ObjectOutputStream oos = new ObjectOutputStream(os);
   oos.writeObject(book);
   oos.close();
   
   }
   catch(Exception e){
   throw new AddressBookException("Datei nicht gefunden");
   } 
   }
   
   } 
 

Ausnahmen

 
public class KeinKaffeeException extends Exception {
private double restMenge;
KeinKaffeeException(double kaffeeMenge ) {
restMenge = kaffeeMenge;
}
public double getRestMenge() {
return restMenge;
}
} public class KaffeeMaschine {
private int speicher;

void fuellenFilter(double benoetigteMenge)
throws KeinKaffeeException {
double restMenge = speicher;
if(restMenge < benoetigteMenge) {
throw new KeinKaffeeException(restMenge);
}

}

public static void main(String[] args){
KaffeeMaschine k= new KaffeeMaschine();
k.speicher=4;
try{
k.fuellenFilter(10);
}
catch(KeinKaffeeException e){
System.out.println("Kaffee reicht nicht");
}
}
}

Parametrische Polymorphie

 
class Person{
String name;

Person(String n){
name=n;
}

Person(){
}

} class Student extends Person{
String name;

Student(String n){
name="Name "+n;
}

} class Container<T extends Person & String>{

T objekt;

Container(T o){
objekt=o;
}

public static void main(String[] args){

Student p= new Student("Ute");
Container<Student> c = new Container<Student>(p);

}
}