Einführung in die objektorientierte Programmierung

Aufgabe 1

public class Foo {
private int x;
private static char c = 'c';
Foo(int x) {
this.x = x;
}
Foo() {
x = 42;
}
class Bar {
Object y;
void m(Foo foo) {
y = new Object();
}
void m(String foo) {
y = foo;
}
Bar() {
y = new Object();
}
}
Object z() {
return new String();
}
static class X {
void n() {
System.out.println(x);
System.out.println(c);
}
}
}

Aufgabe 3

class BinaryNode {
private BinaryNode leftSon, rightSon;
private int value;
public BinaryNode(int v) {
value = v;
}
public boolean contains(int v) {
if (value==v) {
return true;
} // end of if
else {
if (value<v) {
if (rightSon==null) {
return false;
} // end of if
else {
return rightSon.contains(v);
} // end of if-else
} // end of if
else {
if (leftSon==null) {
return false;
} // end of if
else {
return leftSon.contains(v);
} // end of if-else
} // end of if-else
} // end of if-else
}
public void insert(int v) {
BinaryNode stelle=this;
BinaryNode neu;
while ((v<stelle.value && stelle.leftSon!=null)
|| (v>stelle.value && stelle.rightSon!=null)) {
if (v<stelle.value) {
stelle=stelle.leftSon;
} // end of if
else {
stelle=stelle.rightSon;
} // end of if-else
} // end of while
neu= new BinaryNode(v);
if (v<stelle.value) {
stelle.leftSon= neu;
} // end of if
else {
stelle.rightSon= neu;
} // end of if-else
}
public void inorder() {
if (leftSon!=null) {
leftSon.inorder();
} // end of if
System.out.println(value);
if (rightSon!=null) {
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);
System.out.println(myTree.contains(17));
myTree.inorder();
}
}

Aufgabe 4

interface Car {
String readMotorPowerInKiloWatt();
} class RealCar implements Car {
private double power;
public RealCar(double power) {
this.power = power;
}
public String readMotorPowerInKiloWatt() {
return "Car: My power is " + power + " KW!";
}
// Methode zum Tunen des Autos
public void tunePower() {
power = power * 1.2;
}
} class CarFactory {
public Car giveMeACar() {
return new RealCar(98);
}
} class Main{
public static void main(String[] args){
// Fabrik erzeugen
CarFactory fabrik= new CarFactory();
//Auto erzeugen
//Car auto= fabrik.giveMeACar();
RealCar auto= (RealCar) fabrik.giveMeACar();
System.out.println(auto.readMotorPowerInKiloWatt());
//Tunen
auto.tunePower();
System.out.println(auto.readMotorPowerInKiloWatt());

}
}