Javakurs/Übungsaufgaben/Methoden: Unterschied zwischen den Versionen
< Javakurs | Übungsaufgaben
(→Fehler?) |
(→Kommentare) |
||
| (12 dazwischenliegende Versionen von 8 Benutzern werden nicht angezeigt) | |||
| Zeile 7: | Zeile 7: | ||
* Führe den Code aus und vergleiche das Ergebnis mit deinen Erwartungen. | * Führe den Code aus und vergleiche das Ergebnis mit deinen Erwartungen. | ||
| − | + | <pre> | |
| − | class | + | public class Print { |
| − | public static void main ( String [] arguments ) { | + | public static void main(String[] arguments) { |
| − | + | print(1, "Martin"); | |
| − | + | print(2, "Arthur"); | |
| − | + | print(3, "Florian"); | |
| − | + | print(4, "Robert"); | |
} | } | ||
| − | static void | + | public static void print(int zahl, String name) { |
| − | System.out.println( | + | System.out.println(zahl + ": " + name); |
} | } | ||
} | } | ||
| − | </ | + | </pre> |
| − | |||
== Rückgabewert == | == Rückgabewert == | ||
| Zeile 27: | Zeile 26: | ||
* Führe den Code jeweils aus und vergleiche das Ergebnis mit deinen Erwartungen. | * Führe den Code jeweils aus und vergleiche das Ergebnis mit deinen Erwartungen. | ||
| − | + | <pre> | |
| − | + | public class ReturnDouble { | |
| − | class | + | public static void main(String[] arguments) { |
| − | + | System.out.println(gibMirNeZahl()); | |
| − | + | } | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | public static double gibMirNeZahl() { | |
| − | + | return 15.3; | |
| − | + | } | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| − | + | </pre> | |
| − | + | <pre> | |
| − | + | public class ReturnBoolean { | |
| − | class | + | public static void main(String[] arguments) { |
| − | + | System.out.println(mathematik(1, 2)); | |
| − | + | System.out.println(mathematik(1, 5)); | |
| − | + | System.out.println(mathematik(3, 4)); | |
| − | + | } | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | } | ||
| − | |||
| − | + | public static boolean mathematik(int argument1, int argument2) { | |
| − | + | return (argument1 + 5) < (argument2 * 2); | |
| − | + | } | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| − | + | </pre> | |
| − | == | + | <pre> |
| − | + | public class ReturnInt { | |
| + | public static void main(String[] arguments) { | ||
| + | System.out.println("2 + 7 = " + add(2, 7)); | ||
| + | System.out.println("0 + 8 = " + add(0, 8)); | ||
| + | System.out.println("-7 + 7 = " + add(-7, 7)); | ||
| + | } | ||
| − | + | public static int add(int int1, int int2) { | |
| − | + | return int1 + int2; | |
| − | + | } | |
| − | + | } | |
| − | + | </pre> | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | Die Teilaufgabe mit dem Code "Rückgabewert" compiliert nicht. | |
| + | Es kommt folgende Fehlermeldung: | ||
| − | + | 2 errors | |
| + | .\ReturnDouble.java:11: error: class ReturnBoolean is public, should be declared in a file named ReturnBoolean.java | ||
| + | public class ReturnBoolean { | ||
| + | ^ | ||
| + | .\ReturnDouble.java:23: error: class ReturnInt is public, should be declared in a file named ReturnInt.java | ||
| + | public class ReturnInt { | ||
| + | ^ | ||
| − | + | Wie ich es verstanden habe, müsste ich hier noch zusätzliche "Hilfsprogramme" schreiben? | |
Aktuelle Version vom 7. März 2014, 10:39 Uhr
Hinweis: Alle in diese Übung verwendeten Klassen findest du auch als Vorgabedateien unter http://docs.freitagsrunde.org/Veranstaltungen/javakurs_2007/vorgaben/Methoden.tar
Argumente
- Überlege dir, was der folgende Code macht. Was erwartest du als Ausgabe?
- Führe den Code aus und vergleiche das Ergebnis mit deinen Erwartungen.
public class Print {
public static void main(String[] arguments) {
print(1, "Martin");
print(2, "Arthur");
print(3, "Florian");
print(4, "Robert");
}
public static void print(int zahl, String name) {
System.out.println(zahl + ": " + name);
}
}
Rückgabewert
- Überlege dir, was der folgende Code in den folgenden Beispielen macht. Was erwartest du als Ausgabe?
- Führe den Code jeweils aus und vergleiche das Ergebnis mit deinen Erwartungen.
public class ReturnDouble {
public static void main(String[] arguments) {
System.out.println(gibMirNeZahl());
}
public static double gibMirNeZahl() {
return 15.3;
}
}
public class ReturnBoolean {
public static void main(String[] arguments) {
System.out.println(mathematik(1, 2));
System.out.println(mathematik(1, 5));
System.out.println(mathematik(3, 4));
}
public static boolean mathematik(int argument1, int argument2) {
return (argument1 + 5) < (argument2 * 2);
}
}
public class ReturnInt {
public static void main(String[] arguments) {
System.out.println("2 + 7 = " + add(2, 7));
System.out.println("0 + 8 = " + add(0, 8));
System.out.println("-7 + 7 = " + add(-7, 7));
}
public static int add(int int1, int int2) {
return int1 + int2;
}
}
Die Teilaufgabe mit dem Code "Rückgabewert" compiliert nicht.
Es kommt folgende Fehlermeldung:
2 errors .\ReturnDouble.java:11: error: class ReturnBoolean is public, should be declared in a file named ReturnBoolean.java public class ReturnBoolean {
^
.\ReturnDouble.java:23: error: class ReturnInt is public, should be declared in a file named ReturnInt.java public class ReturnInt {
^
Wie ich es verstanden habe, müsste ich hier noch zusätzliche "Hilfsprogramme" schreiben?