Klassen:Javakurs: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Marc (Diskussion | Beiträge) |
|||
| (7 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt) | |||
| Zeile 32: | Zeile 32: | ||
this.seed = System.currentTimeMillis(); | this.seed = System.currentTimeMillis(); | ||
} | } | ||
Random(long seed){ | public Random(long seed){ | ||
this.seed = seed; | this.seed = seed; | ||
} | } | ||
long next() { | public long next() { | ||
this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989); | this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989); | ||
return this.seed; | return this.seed; | ||
} | } | ||
long next(long min, long max){ | public long next(long min, long max){ | ||
long rc = next(); | long rc = next(); | ||
rc = min == max ? min : min + rc % (max - min); | rc = min == max ? min : min + rc % (max - min); | ||
return rc; | return rc; | ||
} | |||
private char[] decimals = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; | |||
public char nextDecimal(){ | |||
return decimals[next(0, 10)]; | |||
} | } | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
| Zeile 52: | Zeile 56: | ||
} | } | ||
class RealRandom extends Random { | class RealRandom extends Random { | ||
double next(double min, double max){ | public double next(double min, double max){ | ||
double rc = next(0, 1000000000) / 1000000000.0; | double rc = next(0, 1000000000) / 1000000000.0; | ||
rc = min == max ? min : min + rc * (max - min); | rc = min == max ? min : min + rc * (max - min); | ||
| Zeile 58: | Zeile 62: | ||
} | } | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
RealRandom random = new RealRandom(); | |||
for (int ix = 0; ix < 10; ix++) | for (int ix = 0; ix < 10; ix++) | ||
System.out.print(String.format("%.3f ", random.next(-1.0, 1.0)); | System.out.print(String.format("%.3f ", random.next(-1.0, 1.0)); | ||
| Zeile 64: | Zeile 68: | ||
} | } | ||
} | } | ||
</pre> | |||
== Aufgaben == | |||
* Leite von Random eine Klasse CharRandom ab, die folgende Methoden enthält: | |||
** char nextVoval(); | |||
** char nextConsonant(); | |||
** char nextPunctuation(); | |||
* Modelliere eine Klasse PasswordGenerator, der zufällige Passwörter generiert: | |||
** Mindestens 8 Zeichen lang | |||
** Abwechselnd Vokal/Konsonant. | |||
** Eingestreut mindestens eine Zahl und mindestens ein Satzzeichen | |||
Hinweis: | |||
* Strings können mit char addiert werden: "hallo" + '!' ergibt "hallo!" | |||
* Um ein Zeichen in einen String einzufügen, wird der erste Teilstring mit dem Zeichen und dem Reststring addiert. | |||
<pre>// Einfügen von 'x' an Index 3: | |||
String x = "123456"; | |||
x = x.substring(0, 3) + 'x' + x.substring(3); | |||
</pre> | |||
<pre> | |||
char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', | |||
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; | |||
char[] punctuations = { '!', ':', '.', '$', '%', '/', '?', '=', '_' }; | |||
</pre> | </pre> | ||
Aktuelle Version vom 4. November 2014, 20:04 Uhr
Theorie
[Bearbeiten]- Eine Klasse beschreibt ein Objekt.
- Eine Klasse besteht aus Eigenschaften (Variablen) und Aktionen (Methoden)
- Es gibt spezielle Methoden: Konstruktoren genannt.
- Ein Konstruktor hat den Namen der Klasse
- Ein Konstruktor hat keinen Ergebnistyp
- Wird ein neues Objekt der Klasse erzeugt, wird ein Konstruktor aufgerufen. Daher muss ein Konstruktor alle Initialisierunngen der Klasse enthalten
- Welcher Konstruktor genommen wird, wird aufgrund der Argumente bestimmt.
- Ist kein Konstruktor ohne Argumente vorhanden, wird ein impliziter definiert
Syntax
[Bearbeiten]Klassendefinition
[Bearbeiten]'class' <name> [ 'extends' <base-class> ] '{' { <method-or-variable> }* '}'
<method-or-variable> ::= <method> | <variable>
Erzeugung eines Objekts
[Bearbeiten]<class-name> <name> '=' 'new' <class-name> '(' <argument-list> ')' ';'
Beispiel
[Bearbeiten]class Random {
private long seed = 0;
Random() {
this.seed = System.currentTimeMillis();
}
public Random(long seed){
this.seed = seed;
}
public long next() {
this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989);
return this.seed;
}
public long next(long min, long max){
long rc = next();
rc = min == max ? min : min + rc % (max - min);
return rc;
}
private char[] decimals = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
public char nextDecimal(){
return decimals[next(0, 10)];
}
public static void main(String[] args) {
Random random = new Random();
for (int ix = 0; ix < 10; ix++)
System.out.print(String.format("%d ", random.next(-100, 100));
System.out.println();
}
}
class RealRandom extends Random {
public double next(double min, double max){
double rc = next(0, 1000000000) / 1000000000.0;
rc = min == max ? min : min + rc * (max - min);
return rc;
}
public static void main(String[] args) {
RealRandom random = new RealRandom();
for (int ix = 0; ix < 10; ix++)
System.out.print(String.format("%.3f ", random.next(-1.0, 1.0));
System.out.println();
}
}
Aufgaben
[Bearbeiten]- Leite von Random eine Klasse CharRandom ab, die folgende Methoden enthält:
- char nextVoval();
- char nextConsonant();
- char nextPunctuation();
- Modelliere eine Klasse PasswordGenerator, der zufällige Passwörter generiert:
- Mindestens 8 Zeichen lang
- Abwechselnd Vokal/Konsonant.
- Eingestreut mindestens eine Zahl und mindestens ein Satzzeichen
Hinweis:
- Strings können mit char addiert werden: "hallo" + '!' ergibt "hallo!"
- Um ein Zeichen in einen String einzufügen, wird der erste Teilstring mit dem Zeichen und dem Reststring addiert.
// Einfügen von 'x' an Index 3: String x = "123456"; x = x.substring(0, 3) + 'x' + x.substring(3);
char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] punctuations = { '!', ':', '.', '$', '%', '/', '?', '=', '_' };