Created
December 15, 2018 16:31
-
-
Save hannojg/563603afceb3569cf1d549e7c58becda to your computer and use it in GitHub Desktop.
Medizingeräte mit abstrakten Klassen - Einfuehrung in die Programmierung 2018/19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Geraet { | |
protected boolean istEingeschaltet; | |
private String hersteller; | |
public Geraet(String hersteller) { | |
this.hersteller = hersteller; | |
} | |
public String gibHersteller() { | |
return hersteller; | |
} | |
public abstract void drueckePowerKnopf(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
/** | |
* Abstrakte Klasse zur Modellierung eines Medizingeraetes | |
* | |
* @author Dennis Labitzke | |
*/ | |
public abstract class Medizingeraet extends Geraet { | |
/** Schwellwert fuer das Ausloesen eines Alarms. */ | |
int alarmSchwellwert; | |
/** Instanz einer Klasse zum Erzeugen von Zufallszahlen. */ | |
Random rand = new Random(); | |
/** | |
* Konstruktor der Klasse. | |
* | |
* @param hersteller Der Hersteller des modellierten Geraetes. | |
* @param alarmSchwellwert Der Wert, welcher, wenn ueberstiegen, einen Alarm ausloest. | |
*/ | |
Medizingeraet(String hersteller, int alarmSchwellwert) { | |
super(hersteller); | |
istEingeschaltet = false; | |
this.alarmSchwellwert = alarmSchwellwert; | |
} | |
/** | |
* Methode, welche das modellierte Geraet an- bzw. ausschaltet. | |
*/ | |
@Override | |
public void drueckePowerKnopf() { | |
if (istEingeschaltet) { | |
System.out.println("Schalte Geraet aus."); | |
} | |
else { | |
System.out.println("Schalte Geraet ein."); | |
} | |
istEingeschaltet = !istEingeschaltet; | |
} | |
/** | |
* Ueberpruefen, ob ein Alarm ausgeloest werden muss. | |
*/ | |
public abstract void ueberpruefeAlarm(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Sensor extends Medizingeraet { | |
private int messwert; | |
public Sensor(String hersteller, int alarmSchwellwert) { | |
super(hersteller, alarmSchwellwert); | |
} | |
private void messen() { | |
messwert = rand.nextInt(101); | |
} | |
public void ueberpruefeAlarm() { | |
if(!istEingeschaltet) { | |
System.out.println("Geraet ist ausgeschaltet!"); | |
return; | |
} | |
messen(); | |
if(messwert > alarmSchwellwert) | |
System.out.println("Alarm! Messwert uebersteigt Schwellwert."); | |
else if(messwert <= alarmSchwellwert) | |
System.out.println("Messwert ist in Ordnung."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
😄