Peter Heisig
Software consultant and developer from Dresden.
Softwaretechnologie I / Übung II
slides.com/phdd/st1e2/live
class Student extends Person {
int semester;
int getSemester() {
return semester;
}
}
class Person {
String name;
String getName() {
return name;
}
}
Book search(String title) {
}
Book search(String title) {
Book found = null;
for (int i = 0; i < number; i++)
if (title.equals(myBooks[i].toString()))
found = myBooks[i];
if (found == null)
System.out.println("The book with the title "
+ title + " does not exist in the library!");
else
System.out.println("The book with the title "
+ title + " exists in the library!");
return found;
}
public class Book {
// Zustandsdefinition
Book(String title) {
// Initialzustand
}
// ausleihen
// zurückbringen
// Zustandsabfrage
}
public class Book {
// Zustandsdefinition
private String state;
Book(String title) {
// Initialzustand
state = "available";
}
// ausleihen
Book loan() {
state = "lent";
return this;
}
// zurückbringen
Book bringBack() {
state = "available";
return this;
}
// Zustandsabfrage
String getState() {
return state;
}
}
Book loan(String title) {
}
Book loan(String title) {
Book book = search(title);
if (book == null)
return null;
if ("available".equals(book.getState()))
return book.loan();
System.out.println("Book " + book + " not available");
return null;
}
class Inhabitant {
private int income;
void setIncome(int income) {
this.income = income;
}
int taxableIncome() {
return income;
}
int tax() {
int tax = taxableIncome() / 10;
if (tax < 1) {
tax = 1;
}
return tax;
}
}
class Inhabitant {
private int income;
void setIncome(int income) {
this.income = income;
}
int taxableIncome() {
return income;
}
int tax() {
int tax = taxableIncome() / 10;
if (tax < 1) {
tax = 1;
}
return tax;
}
}
class King {
@Override
int tax() {
return 0;
}
}
class King extends Inhabitant {
@Override
int tax() {
return 0;
}
}
class Noble {
@Override
int tax() {
int tax = super.tax();
if (tax < 20) {
tax = 20;
}
return tax;
}
}
class Noble extends Inhabitant {
@Override
int tax() {
int tax = super.tax();
if (tax < 20) {
tax = 20;
}
return tax;
}
}
class Peasant {
@Override
int tax() {
int tax = super.tax();
if (tax < 20) {
tax = 20;
}
return tax;
}
}
class Peasant extends Inhabitant {
}
class Serf {
@Override
int tax() {
int tax = super.tax();
if (tax < 20) {
tax = 20;
}
return tax;
}
}
class Serf extends Peasant {
@Override
int taxableIncome() {
int taxableIncome =
super.taxableIncome() - 12;
if (taxableIncome < 0)
taxableIncome = 0;
return taxableIncome;
}
}
public class Kingdom {
public static void main(String[] args) {
taxBillFor(new King(), 20);
taxBillFor(new Noble(), 20);
taxBillFor(new Peasant(), 20);
taxBillFor(new Serf(), 20);
}
static void taxBillFor(Inhabitant inhabitant, int income) {
inhabitant.setIncome(income);
System.out.println(inhabitant.getClass().getName()
+ ": income " + income
+ ", taxable income " + inhabitant.taxableIncome()
+ ", tax " + inhabitant.tax());
}
}
pehei.de
slides.com/phdd
twitter.com/_phdd
By Peter Heisig