Java :: Aufgabe #2 :: Lösung #19
21 Lösungen
#2
Jahr auf Schaltjahr überprüfen
Anfänger - Java
von Gustl
- 06.08.2012 um 23:26 Uhr
Schreiben Sie eine Methode, welche überprüft ob das übergebene Jahr (
Rückgabewert ist ein
Testen Sie die Methode!
Methodenrumpf:
Java-Code
int) ein schaltjahr ist oder nicht. Rückgabewert ist ein
boolean-Wert (true oder false)Testen Sie die Methode!
Methodenrumpf:
public static boolean isSchaltjahr(int year) {
}
#19
von kollar (340 Punkte)
- 04.12.2019 um 10:50 Uhr
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Schaltjahr {
public static void main(String[] args) {
SchaltjahrPruefen schp = new SchaltjahrPruefen("Schaltjahr prüfen");
}
}
class SchaltjahrPruefen extends JFrame implements ActionListener {
JLabel lbl = new JLabel("Geben Sie das Jahr ein: ");
JTextField tfEingabe = new JTextField(10);
JTextField tfAusgabe = new JTextField(20);
JPanel pEingabe = new JPanel();
JPanel pAusgabe = new JPanel();
SchaltjahrPruefen(String titel) {
super(titel);
setVisible(true);
setSize(300, 300);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
tfEingabe.addActionListener(this);
tfAusgabe.setEditable(false);
pEingabe.add(lbl);
pEingabe.add(tfEingabe);
pAusgabe.add(tfAusgabe);
add(pEingabe);
add(pAusgabe);
}
public static boolean isSchaltjahr(int jahr) {
if ((jahr % 4 == 0 && !(jahr % 100 == 0)) || (jahr % 400 == 0))
return true;
else
return false;
}
@Override
public void actionPerformed(ActionEvent evt) {
try {
int zahl = Integer.parseInt(tfEingabe.getText());
isSchaltjahr(zahl);
if (isSchaltjahr(zahl)) {
tfAusgabe.setText(tfEingabe.getText() + " ist Schaltjahr");
} else {
tfAusgabe.setText(tfEingabe.getText() + " ist kein Schaltjahr");
}
} catch (NumberFormatException e) {
tfAusgabe.setText("Bitte Ganzzahl eingeben!");
}
}
}
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
