-
Language
Java using Swing
-
Description
Need to write a program to convert Seconds in to Hours Minutes and Seconds. You should obtain number of seconds as user input (Integer Value). Convert the seconds obtain in to Hours, Minutes and Seconds and display that to the User.
Need to have the output of the code in this format:
Enter a number of seconds: 500
Here is the time in hours, minutes, and seconds:
Hours: 0.0
Minutes: 8.0
Seconds: 20.0
where "500" is the input and the other numbers are the output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ConvertsANumberOfSecondsInToHoursAndMinutes extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField field_number_of_seconds;
private JLabel label_hours, label_minutes, label_seconds;
private JButton button;
public Algorithm() {
field_number_of_seconds = new JTextField(4);
label_hours = new JLabel("", JLabel.RIGHT);
label_minutes = new JLabel("", JLabel.RIGHT);
label_seconds = new JLabel("", JLabel.RIGHT);
button = new JButton("Process");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
JPanel panel, subpanel;
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(new JLabel("Enter the value of number of seconds:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(field_number_of_seconds);
panel.add(subpanel);
pane.add(panel, BorderLayout.NORTH);
panel = new JPanel(new FlowLayout());
panel.add(button);
pane.add(panel);
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(3, 1));
subpanel.add(new JLabel("Value of hours:"));
subpanel.add(new JLabel("Value of minutes:"));
subpanel.add(new JLabel("Value of seconds:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(3, 1));
subpanel.add(label_hours);
subpanel.add(label_minutes);
subpanel.add(label_seconds);
panel.add(subpanel);
pane.add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
int hours, minutes, number_of_seconds, seconds;
try {
number_of_seconds = Integer.parseInt(field_number_of_seconds.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
seconds=number_of_seconds%60;
minutes=(number_of_seconds-seconds)/60;
hours=(minutes-minutes%60)/60;
minutes=minutes%60;
label_hours.setText(String.valueOf(hours));
label_minutes.setText(String.valueOf(minutes));
label_seconds.setText(String.valueOf(seconds));
pack();
}
public static void main(String[] args) {
new Algorithm().setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
public class ConvertsANumberOfSecondsInToHoursAndMinutes extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField field_number_of_seconds;
private JLabel label_hours, label_minutes, label_seconds;
private JButton button;
public Algorithm() {
field_number_of_seconds = new JTextField(4);
label_hours = new JLabel("", JLabel.RIGHT);
label_minutes = new JLabel("", JLabel.RIGHT);
label_seconds = new JLabel("", JLabel.RIGHT);
button = new JButton("Process");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
JPanel panel, subpanel;
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(new JLabel("Enter the value of number of seconds:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(field_number_of_seconds);
panel.add(subpanel);
pane.add(panel, BorderLayout.NORTH);
panel = new JPanel(new FlowLayout());
panel.add(button);
pane.add(panel);
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(3, 1));
subpanel.add(new JLabel("Value of hours:"));
subpanel.add(new JLabel("Value of minutes:"));
subpanel.add(new JLabel("Value of seconds:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(3, 1));
subpanel.add(label_hours);
subpanel.add(label_minutes);
subpanel.add(label_seconds);
panel.add(subpanel);
pane.add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
int hours, minutes, number_of_seconds, seconds;
try {
number_of_seconds = Integer.parseInt(field_number_of_seconds.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
seconds=number_of_seconds%60;
minutes=(number_of_seconds-seconds)/60;
hours=(minutes-minutes%60)/60;
minutes=minutes%60;
label_hours.setText(String.valueOf(hours));
label_minutes.setText(String.valueOf(minutes));
label_seconds.setText(String.valueOf(seconds));
pack();
}
public static void main(String[] args) {
new Algorithm().setVisible(true);
}
}