class Bruch{
final int zaehler;
final int nenner;
public Bruch(int z, int n){
zaehler=z; nenner=n;
}
public Bruch multipliziere(Bruch b){
Bruch erg= new Bruch(this.zaehler * b.zaehler,
this.nenner * b.nenner);
return erg;
}
public String toString(){
return this.zaehler+"/"+this.nenner;
}
public static void main(String[] args){
Bruch b1 = new Bruch(3,4);
Bruch b2 = new Bruch(2,3);
Bruch b3 ;
b3 = b1.multipliziere(b2);
System.out.println(b3.toString());
}
}
interface MitBeinen{
public int anzahlBeine();
}
abstract class Tier{
public abstract String nameDerTierart();
}
public class Fisch extends Tier{
public String nameDerTierart(){
return "Fisch";
}
}
public class Spinne extends Tier implements MitBeinen{
public String nameDerTierart(){
return "Spinne";
}
public int anzahlBeine(){
return 8;
}
}
public class Tisch implements MitBeinen{
public int anzahlBeine(){
return 4;
}
}
public class BoxIstVollException extends Exception{
// BoxIstVollException(){
// return "Box ist rappelvoll";
// }
}
public class BoxIstLeerException extends Exception{
// BoxIstLeerException(){
// return "Box ist leer";
// }
}
class Box {
String inhalt;
public void einlagern(String inhalt) throws BoxIstVollException{
if(this.inhalt != null)
throw new BoxIstVollException();
this.inhalt = inhalt;
}
public String entnehmen() throws BoxIstLeerException{
if(inhalt == null)
throw new BoxIstLeerException();
String entnommen = inhalt;
inhalt = null;
return entnommen;
}
public static void main(String[] args) {
Box box = new Box();
try{
System.out.println(box.entnehmen());
}
catch (BoxIstLeerException e){
System.out.println(e);
}
try{
box.einlagern("abc");
}
catch (BoxIstVollException e){
System.out.println(e);
}
}
}
class Tiere{
public enum ZooTier{Tiger, Ente, Schnecke;
public boolean istGleichschnellWie(ZooTier tier){
return (this == tier);
}
public boolean istSchnellerAls(ZooTier tier){
return (((this == Tiger) && (tier != Tiger))
| ((this == Ente ) && (tier == Schnecke)));
}
public boolean istLangsamerAls(ZooTier tier){
return ((! this.istGleichschnellWie(tier) )
& (! this.istSchnellerAls(tier))) ;
}
}
public static void main(String[] args) {
System.out.println(ZooTier.Tiger.istGleichschnellWie(ZooTier.Tiger)); // true
System.out.println(ZooTier.Ente.istSchnellerAls(ZooTier.Schnecke)); //true
System.out.println(ZooTier.Schnecke.istLangsamerAls(ZooTier.Schnecke)); // false
}
}
Mit Hilfe der Ordnungszahlen
class TiereOrd{
public enum ZooTier{Tiger, Ente, Schnecke;
public boolean istGleichschnellWie(ZooTier tier){
return (this == tier);
}
public boolean istSchnellerAls(ZooTier tier){
return (this.ordinal() < tier.ordinal());
}
public boolean istLangsamerAls(ZooTier tier){
return (this.ordinal() > tier.ordinal());
}
}
public static void main(String[] args) {
System.out.println(ZooTier.Tiger.ordinal());
System.out.println(ZooTier.Tiger.istGleichschnellWie(ZooTier.Tiger)); // true
System.out.println(ZooTier.Ente.istSchnellerAls(ZooTier.Schnecke)); //true
System.out.println(ZooTier.Schnecke.istLangsamerAls(ZooTier.Schnecke)); // false
}
}
class Cd {}
class Wiedergabegeraet<P> {
P medium;
public void mediumEinlegen(P med) {
medium = med;
System.out.println(medium);
}
public P mediumEntnehmen(){
P tmp= medium;
System.out.println(medium);
medium = null;
return tmp;
}
}
public class WiedergabegeraetDemo {
public static void main(String[] args) {
Wiedergabegeraet<Cd> player = new Wiedergabegeraet<Cd>();
Cd cd = new Cd();
player.mediumEinlegen(cd);
Cd medium = player.mediumEntnehmen();
}
}
interface Hoerbar{
void liedSpielen(int liedNr);
int anzahlLieder();
}
class Cd implements Hoerbar {
public void liedSpielen(int liedNr) {
}
public int anzahlLieder(){
return 42;
}
}
class Wiedergabegeraet<P extends Hoerbar> {
P medium;
public void mediumEinlegen(P med) {
medium = med;
System.out.println(medium);
}
public P mediumEntnehmen(){
P tmp= medium;
System.out.println(medium);
medium = null;
return tmp;
}
public void alleSpielen(){
for (int i=0;i<medium.anzahlLieder() ; i++) {
medium.liedSpielen(i);
} // end of for
}
}
public class WiedergabegeraetDemo {
public static void main(String[] args) {
Wiedergabegeraet<Cd> player = new Wiedergabegeraet<Cd>();
Cd cd = new Cd();
player.mediumEinlegen(cd);
player.alleSpielen();
Cd medium = player.mediumEntnehmen();
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SchleifenDemo {
public static void main(String[] args) {
List<String> eineListe = new ArrayList<String>();
eineListe.add("Alle");
eineListe.add("meine");
eineListe.add("Entchen");
eineListe.add("...");
schreiben(eineListe);
}
static void schreiben(List<String> liste) {
// for-Schleife mit indiziertem Zugriff auf die Elemente.
for (int i=0;i<liste.size() ; i++) {
System.out.print(liste.get(i)+" ");
} // end of for
System.out.println();
// for-Schleife, die Iterable<String> ausnutzt.
Iterator it= liste.listIterator();
for (it = liste.listIterator(0); it.hasNext(); ) {
System.out.print(it.next()+" ");
} // end of for
System.out.println();
// for-Schleife, die Iterable<String> ausnutzt mit for-each
for (String s : liste ) {
System.out.print(s+" ");
} // end of for
System.out.println();
// while-Schleife mit indiziertem Zugriff auf die Elemente.
int i=0;
while (i<liste.size()) {
System.out.print(liste.get(i)+" ");
i++;
} // end of while
System.out.println();
// while-Schleife, die Iterable<String> ausnutzt.
it= liste.listIterator();
while (it.hasNext()) {
System.out.print(it.next()+" ");
} // end of while
}
}
Geben Sie eine alternative Implementierung an, die dieses Problem vermeidet.
public synchronized Object entnehmen() {
Object letztes = gegenstaende.getLast();
gegenstaende.removeLast();
return letztes;
}
public class Fuhrpark {
public static void main(String[] args) {
LKW lkw = new Sattelschlepper();
PKW pkw1 = new PKW();
PKW pkw2 = new Kleinwagen();
Kombi kombi = new Kombi();
Sattelschlepper sattelschlepper = new Sattelschlepper();
Super sup1 = new Super();
Super sup2 = new Sub();
sup1.m(pkw2, lkw);
sup1.m(pkw1, sattelschlepper);
sup1.m(kombi, pkw2);
sup2.m(pkw1, lkw);
sup2.m(pkw1, sattelschlepper);
}
}
class Super {
public void m(Fahrzeug t, LKW f) {
System.out.println("1");
}
public void m(Fahrzeug t1, Fahrzeug t2) {
System.out.println("2");
}
}
class Sub extends Super {
public void m(PKW v, LKW f) {
System.out.println("3");
}
public void m(Fahrzeug t1, LKW t2) {
System.out.println("4");
}
}
class Fahrzeug {}
class LKW extends Fahrzeug {}
class Sattelschlepper extends LKW {}
class PKW extends Fahrzeug {}
class Kombi extends PKW {}
class Kleinwagen extends PKW {}
sup1.m(pkw2, lkw);
Ausgabe 1
erste Methode spezieller als 2. Methode
sup1.m(pkw1, sattelschlepper);
Ausgabe 1
erste Methode spezieller als 2. Methode
sup1.m(kombi, pkw2);
Ausgabe 2
nur 2. Methode passt
sup2.m(pkw1, lkw);
Ausgabe 4
In Super ist es Methode 1,
aber die wird in SUb durch Methode 4 überschrieben
sup2.m(pkw1, sattelschlepper);
Ausgabe 4
In Super ist es Methode 1,
aber die wird in SUb durch Methode 4 überschrieben
a.m(o1); Ausgabe B.m(Object o)
a.m(o2); Ausgabe B.m(Object o)
a.m(d); Ausgabe B.m(Object o)
a.m(s); Ausgabe B.m(String s)