Nur Main: Unterschied zwischen den Versionen
(correction(NurMain): Added examples & keypoints to make it more understandable [issue-13]) |
(correction(NurMain): Corrected the example codes [issue-13]) |
||
| Zeile 25: | Zeile 25: | ||
* Laden einer Konfigurationsdatei. | * Laden einer Konfigurationsdatei. | ||
* Erzeugen und Starten der Hauptanwendung (Applikation-Klasse). | * 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 | {{Inhaltsblock | ||
| Zeile 30: | Zeile 31: | ||
|vorher=Positivbeispiel | |vorher=Positivbeispiel | ||
|Beispiel=public static void main(String[] args) { | |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 { | try { | ||
// Laden der Konfigurationsdatei | // 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(); | |||
} | } | ||
}} | }} | ||
| Zeile 60: | Zeile 64: | ||
return; | return; | ||
} | } | ||
int | 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; | int result; | ||
if ( | if (x < 0 {{!}}{{!}} y < 0) { | ||
System. | System.err.println("Negative numbers are not allowed."); | ||
return; | |||
} else { | } else { | ||
result = | result = x + y; | ||
} | } | ||
System.out.println("Result: " + result); | System.out.println("Result: " + result); | ||
| Zeile 82: | Zeile 94: | ||
Calculator calculator = new Calculator(); | Calculator calculator = new Calculator(); | ||
Parser parser = new Parser(); | Parser parser = new Parser(); | ||
int | |||
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) { | ||
if (x < 0 {{!}}{{!}} y < 0) { | if (x < 0 {{!}}{{!}} y < 0) { | ||
System. | System.err.println("Negative numbers are not allowed."); | ||
return | return; | ||
} | } | ||
int result = x + y; | |||
System.out.println("Result: " + result); | |||
} | } | ||
} | } | ||
public class Parser { | public class Parser { | ||
public int | public int parseX(String[] args) { | ||
if (args.length < 1) { | if (args.length < 1) { | ||
// Throws exception | // Throws exception | ||
| Zeile 107: | Zeile 128: | ||
} | } | ||
public int | public int parseY(String[] args) { | ||
// Parses the passed in | // Parses the passed in y and checks if correct | ||
} | } | ||
} | } | ||
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
}
}