Einführung
in die objektorientierte Programmierung
Objekte
Person
class Person{
String name;
int alter;
String strasse;
static int anzahl;
boolean equals(Person o){
if (alter==o.alter)
return true;
else return false;
}
public static void main(String[] args){
Person p1 = new Person();
p1.alter = 45;
System.out.println(p1.alter);
p1.anzahl= 1;
System.out.println(p1.anzahl);
Person p2 = new Person();
p2.alter = 45;
System.out.println(p2.alter);
p2.anzahl= 2;
System.out.println(p1.anzahl);
if (p1.equals(p2)) {
System.out.println("Objekte gleich");
} // end of if
else System.out.println("Objekte ungleich");
}
}
Person 2
class Person2{
String name;
int alter;
String strasse;
public void set_name(String name){
this.name = name;
}
public String get_name(){
return name;
}
static int anzahl;
public static void main(String[] args){
Person2 p1 = new Person2();
p1.set_name("Bernd");
System.out.println("Name: " + p1.get_name() );
Person2 p2= p1;
p1.set_name("Ulrike");
System.out.println("Name: " + p2.get_name() );
}
}
Person 3
class Person3{
String name;
int alter;
String strasse;
static int anzahl=0;
public void set_name(String name){
this.name = name;
}
public String get_name(){
return name;
}
Person3(String n, int alter, String str){
name=n;
this.alter = alter;
strasse = str;
anzahl++;
}
Person3(){
anzahl++;
}
public void ausgabe(){
System.out.println(name);
System.out.println(alter);
System.out.println(strasse);
System.out.println("Anzahl Personen: " + anzahl);
}
public static void main(String[] args){
Person3 p1 = new Person3("Kurt",87,"Moorweide 6");
p1.ausgabe();
Person3 p2 = p1;
for (int i=0; i<=100 ; i++ ) {
} // end of for
System.out.println(p1 + " " + p2);
Stud s1 = new Stud("name",20, "str", "123456");
}
}
class Stud extends Person3{
String matrikel;
Stud(String n, int a, String str, String m){
matrikel= m;
}
}
Game of Life
public class GameOfLife {
boolean[][] feld = { { false, false, false, false, false },
{ false, false, true, false, false },
{ false, false, true, false, false },
{ false, false, true, false, false },
{ false, false, false, false, false } };
public static void main(String[] args) {
GameOfLife myGame = new GameOfLife();
for (int i = 0; i < 10; i++) {
myGame.nextGeneration();
myGame.print();
System.out.println();
}
}
void print() {
for (int i = 0; i < feld.length; i++) {
for (int j = 0; j < feld[i].length; j++) {
if (feld[i][j] == true)
System.out.print("o ");
else
System.out.print(". ");
}
System.out.println();
}
}
void nextGeneration() {
boolean[][] alt = new boolean[feld.length][feld[0].length];
for (int i=0;i<feld.length ; i++) {
for (int j=0;j< feld[0].length; j++ ) {
alt[i][j] = feld[i][j];
} // end of for
} // end of for
for (int i=0;i<feld.length ; i++) {
for (int j=0;j< feld[0].length; j++ ) {
if (i==0 || j==0 || i==feld.length-1 || j==feld[0].length-1) {
feld[i][j]=false;
} // end of if
else{
int anz=0;
if (alt[i-1][j-1]) anz++;
if (alt[i-1][j]) anz++;
if (alt[i-1][j+1]) anz++;
if (alt[i][j-1]) anz++;
if (alt[i][j+1]) anz++;
if (alt[i+1][j-1]) anz++;
if (alt[i+1][j]) anz++;
if (alt[i+1][j+1]) anz++;
if (anz==3) feld[i][j]=true;
if (anz<2 || anz>3) feld[i][j]=false;
}
} // end of for
} // end of for
}
}
Summe Gauss
public class Summierer {
public static void main(String[] args) {
Summierer summierer = new Summierer();
int startWert = Integer.parseInt(args[0]);
System.out.println("Ergebnis mit for-Schleife: "
+ summierer.forSumme(startWert));
System.out.println("Ergebnis kleiner Gauss: "
+ summierer.kleinerGauss(startWert));
System.out.println("Ergebnis rekursiv: "
+ summierer.rekursiveSumme(startWert));
}
int forSumme(int startWert) {
int summe=0;
for (int i=0; i<= startWert ; i++) {
summe += i;
} // end of for
return summe;
}
int rekursiveSumme(int startWert) {
if (startWert==1) {
return 1;
} // end of if
else if(startWert<1){
return 0;
} // end of if-else
else {
return startWert + rekursiveSumme(startWert-1);
} // end of if-else
}
int kleinerGauss(int startWert) {
return (startWert*(startWert+1))/2;
}
}