Can anyone please explain this time conversion code logically?? after the second
ID: 3886209 • Letter: C
Question
Can anyone please explain this time conversion code logically?? after the seconds/60 equation, the number ive got is 0 and I don't understand how this code works.. cUsers1 DesktopHWJava Eile Edit Jools Project Language Level Help NewdOpenan Save Close H import java.util,Scanner ‰Cul @Copyt0 Pastea, Undo eRedo Find class HM publlo statlo void nain(Str ing args[M Scanner input = nen Scanner(System.in); int hours-0: int ainutes-0: int seconds 0: Systen.out.pr int In( Tine converslon: Enter your diglts'): seconds-input.nextint(): 1f( seconds 60 minutes seconds/60: seconds seconds-minutes *60; it (ninutes>-60)1 hours nInutes/60: minutes = minutes-hours*60; System,out.pr int in(+ hourshours ): Syston,out.pr lntin(+ ninutes +· minutes "); Systen,out.pr int In(+ seconds seconds "): nleradions Console Compiter Outout FindReplace lie lcone to DrJava. Working directory.is C:RUser sit HDesktop run H Tiae conversion: Enter your digits 2 hours 40 ninutes 39 secondsExplanation / Answer
I have added comments to help you understand the program. Let me know in comments if you still have any issue
import java.util.Scanner;
public class HW {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Initializing the variable for hours , minutes and seconds
int hours = 0;
int minutes = 0;
int seconds = 0;
//getting input from user
System.out.println("Time Conversion: Enter your digits");
seconds = input.nextInt();
//If seconds are greater that 60,
//than dividing seconds by 60 to get minutes
// eg 9999/60 = 166.65 but it is int to it will save 166 in minutes
//seconds = seconds - minutes*60 will work as 9999 - 166*60
// 9999 - 9960 = 39 will be saved in seconds
if(seconds >= 60) {
minutes = seconds / 60;
seconds = seconds - minutes*60;
}
//Now minute has 166 which is greater that 60. So we
//divide it by 60 to get hours which is 166/60 = 2.77
//but as it is int, hour will have value 2. Now minute will be
// 166 - 2*60 = 46.So minute will have value 46
if(minutes >= 60) {
hours = minutes / 60;
minutes = minutes - hours*60;
}
//After running above finally, hour will have 2, minute will have 46
//and seconds will have 39.
System.out.println(+hours+" hours ");
System.out.println(+minutes+" minutes ");
System.out.println(+seconds+" seconds ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.