-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeApp.java
More file actions
37 lines (28 loc) · 1014 Bytes
/
Copy pathTimeApp.java
File metadata and controls
37 lines (28 loc) · 1014 Bytes
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
package LearningPackage;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Scanner;
public class TimeApp {
public static void main(String[] args) {
//Get number of seconds from the user.
System.out.println("Please enter the number of seconds");
Scanner in=new Scanner(System.in);
int seconds=in.nextInt();
//convert seconds to hours,min,sec.
//timeFromSeconds(seconds);
int [] time = timeFromSeconds(seconds);
System.out.printf(seconds+" seconds = %s hours, %s minutes, %s seconds", time[0],time[1],time[2]);
}
//parameters time in seconds
//returns int Array with 3 elements ; 0=hours, 1=minutes, 2=seconds
@NotNull
@Contract(value = "_ -> new", pure = true)
static int[] timeFromSeconds(int seconds) {
int hours, mins, sec, rem;
hours= seconds/3600;
rem=seconds%3600;
mins=rem/60;
sec=rem%60;
return new int[] {hours,mins,sec};
}
}