Nur Main: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
(correction(NurMain): Corrected the example codes [issue-13]) |
||
| (4 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
{{ | {{CategoryBlock | ||
| | |Baustelle=Ja | ||
| | |Java Grundlagen=Nein | ||
| | |Organisation=Nein | ||
Besonders heißt das, dass | |Programmierstil=Ja | ||
Die | |Bewertungsrichtlinie=Ja | ||
|blattAnnotation=1 | |||
| | |blattAbzug=2 | ||
| | }} | ||
public class Main { | {{Inhaltsblock | ||
|vorher====Beschreibung=== | |||
Die Main-Methode <syntaxhighlight inline lang="Java">public static void main (String[] args)</syntaxhighlight> ist lediglich der Eingangspunkt für das Programm. Sie sollte deshalb so kurz wie möglich sein und keine weiteren Aspekte behandeln. | |||
Besonders heißt das, dass nie das gesamte Programm in der Main-Methode sein sollte. Nutzen Sie mehrere Klassen und Methoden, um das Programm nach den Prinzipien der Objektorientierung und des [[Geheimnisverrat|Geheimnisverrats]] zu strukturieren. | |||
}} | |||
{{Inhaltsblock | |||
|vorher====In Kürze=== | |||
* So kurz wie möglich halten | |||
* Nicht das gesamte Programm in der Main-Methode implementieren. | |||
* Die Funktionalität auf mehrere Klassen und Methoden aufteilen | |||
}} | |||
{{Inhaltsblock | |||
|vorher====Was soll ich in der Main-Methode machen?=== | |||
* Validieren der Parameter, die für das Starten des Programms wichtig sind und demnach nicht an das Programm selbst übergeben werden können. | |||
* Laden einer Konfigurationsdatei. | |||
* Erzeugen und Starten der Hauptanwendung (Applikation-Klasse). | |||
* Abfangen von Exceptions, die an anderen Stellen nicht behandelt werden können, und Ausgeben einer entsprechenden Fehlermeldung. | |||
}} | |||
{{Inhaltsblock | |||
|color=green | |||
|vorher=Positivbeispiel | |||
|Beispiel=public static void main(String[] args) { | |||
// Validieren der Parameter | |||
if (args.length < 1) { | |||
System.err.println("Please enter the path to configuration file"); | |||
return; | |||
} | |||
Config config; | |||
try { | |||
// Laden der Konfigurationsdatei | |||
config = ConfigLoader.load(args[0]); | |||
} catch (IllegalArgumentException exception) { | |||
// Abfangen von Exceptions | |||
System.err.println("The configuration file could not be loaded:" + | |||
exception.getMessage()); | |||
return; | |||
} | |||
// Erzeugen und Starten der Hauptanwendung | |||
Application app = new Application(config); | |||
app.run(); | |||
} | |||
}} | |||
{{Inhaltsblock | |||
|vorher====Beispiele=== | |||
}} | |||
{{Inhaltsblock | |||
|color=red | |||
|vorher=Negativbeispiel: | |||
|Beispiel=public class Main { | |||
public static void main(String[] args) { | public static void main(String[] args) { | ||
if (args.length < 2) { | |||
return; | |||
} | |||
int x; | |||
int y; | |||
try { | |||
x = Integer.parseInt(args[0]); | |||
y = Integer.parseInt(args[1]); | |||
} catch (NumberFormatException exception) { | |||
System.err.println(exception.getMessage()); | |||
return; | |||
} | |||
int result; | |||
if (x < 0 {{!}}{{!}} y < 0) { | |||
System.err.println("Negative numbers are not allowed."); | |||
return; | |||
} else { | |||
result = x + y; | |||
} | |||
System.out.println("Result: " + result); | |||
} | } | ||
} | } | ||
|beispielname=NurMainBad | |||
| | |nachher=Hier befindet sich die gesamte Logik in der Main-Methode, wodurch sie zu viel Funktionalität enthält. | ||
public class Main { | }} | ||
{{Inhaltsblock | |||
|color=green | |||
|vorher=Positivbeispiel: | |||
|Beispiel=public class Main { | |||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Calculator calculator = new Calculator(); | Calculator calculator = new Calculator(); | ||
calculator.add( | Parser parser = new Parser(); | ||
int x; | |||
int y; | |||
try { | |||
x = parser.parseX(args); | |||
y = parser.parseY(args); | |||
} catch (IllegalArgumentException exception) { | |||
System.err.println("Parsing failed:" + exception.getMessage()); | |||
return; | |||
} | |||
calculator.add(first, second); | |||
} | } | ||
} | } | ||
public class Calculator { | public class Calculator { | ||
public | public void add(int x, int y) { | ||
return x + y; | if (x < 0 {{!}}{{!}} y < 0) { | ||
System.err.println("Negative numbers are not allowed."); | |||
return; | |||
} | |||
int result = x + y; | |||
System.out.println("Result: " + result); | |||
} | |||
} | |||
public class Parser { | |||
public int parseX(String[] args) { | |||
if (args.length < 1) { | |||
// Throws exception | |||
} | |||
return Integer.parseInt(args[0]); | |||
} | |||
public int parseY(String[] args) { | |||
// Parses the passed in y and checks if correct | |||
} | } | ||
} | } | ||
|beispielname=NurMainGood | |||
| | |nachher=Die Funktionalität der Main-Methode wurde auf mehrere Hilfsmethoden verteilt, wodurch der Code besser strukturiert und verständlicher ist. | ||
| | |||
}} | }} | ||
Aktuelle Version vom 27. Oktober 2025, 10:29 Uhr
| 🚧 | Diese Seite befindet sich in Bearbeitung | 🚧 |
| 🤓 | Diese Seite ist eine Bewertungsrichtlinie, die ab Blatt 1 annotiert und ab Blatt 2 abgezogen wird. | 🤓 |
Beschreibung
Die Main-Methode public static void main (String[] args) ist lediglich der Eingangspunkt für das Programm. Sie sollte deshalb so kurz wie möglich sein und keine weiteren Aspekte behandeln.
Besonders heißt das, dass nie das gesamte Programm in der Main-Methode sein sollte. Nutzen Sie mehrere Klassen und Methoden, um das Programm nach den Prinzipien der Objektorientierung und des Geheimnisverrats zu strukturieren.
In Kürze
- So kurz wie möglich halten
- Nicht das gesamte Programm in der Main-Methode implementieren.
- Die Funktionalität auf mehrere Klassen und Methoden aufteilen
Was soll ich in der Main-Methode machen?
- Validieren der Parameter, die für das Starten des Programms wichtig sind und demnach nicht an das Programm selbst übergeben werden können.
- Laden einer Konfigurationsdatei.
- Erzeugen und Starten der Hauptanwendung (Applikation-Klasse).
- Abfangen von Exceptions, die an anderen Stellen nicht behandelt werden können, und Ausgeben einer entsprechenden Fehlermeldung.
Positivbeispiel
public static void main(String[] args) {
// Validieren der Parameter
if (args.length < 1) {
System.err.println("Please enter the path to configuration file");
return;
}
Config config;
try {
// Laden der Konfigurationsdatei
config = ConfigLoader.load(args[0]);
} catch (IllegalArgumentException exception) {
// Abfangen von Exceptions
System.err.println("The configuration file could not be loaded:" +
exception.getMessage());
return;
}
// Erzeugen und Starten der Hauptanwendung
Application app = new Application(config);
app.run();
}
Beispiele
Negativbeispiel:
public class Main {
public static void main(String[] args) {
if (args.length < 2) {
return;
}
int x;
int y;
try {
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
} catch (NumberFormatException exception) {
System.err.println(exception.getMessage());
return;
}
int result;
if (x < 0 || y < 0) {
System.err.println("Negative numbers are not allowed.");
return;
} else {
result = x + y;
}
System.out.println("Result: " + result);
}
}
Positivbeispiel:
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
Parser parser = new Parser();
int x;
int y;
try {
x = parser.parseX(args);
y = parser.parseY(args);
} catch (IllegalArgumentException exception) {
System.err.println("Parsing failed:" + exception.getMessage());
return;
}
calculator.add(first, second);
}
}
public class Calculator {
public void add(int x, int y) {
if (x < 0 || y < 0) {
System.err.println("Negative numbers are not allowed.");
return;
}
int result = x + y;
System.out.println("Result: " + result);
}
}
public class Parser {
public int parseX(String[] args) {
if (args.length < 1) {
// Throws exception
}
return Integer.parseInt(args[0]);
}
public int parseY(String[] args) {
// Parses the passed in y and checks if correct
}
}