Answer to Question #215475 in Java | JSP | JSF for Mathias Azaria

Question #215475
The point of this exercise is to (1) use some of the arithmetic
operators, and (2) start thinking about compound entities (like time of day)
that are represented with multiple values.
1. Create a new program called Time.java. From now on, we won’t remind
you to start with a small, working program, but you should.
2. Following the example program in Section 2.4, create variables named
hour, minute, and second. Assign values that are roughly the current
time. Use a 24-hour clock so that at 2:00 PM the value of hour is 14.
3. Make the program calculate and display the number of seconds since
midnight.
4. Calculate and display the number of seconds remaining in the day.
5. Calculate and display the percentage of the day that has passed. You
might run into problems when computing percentages with integers, so
consider using floating-point.
6. Change the values of hour, minute, and second to reflect the current
time. Then write code to compute the elapsed time since you started
1
Expert's answer
2021-07-09T08:24:24-0400


public class Time
{
    
	public static void main(String[] args) {
		
		int hour, minute,second;
		hour=13;
		minute=46;
		second=45;
		int total_sec1=(hour*3600)+(60*minute)+second;
		int sec_since_midnight=(second + (minute*60) + (hour*3600));
		System.out.println("Number of seconds since midnight: "+sec_since_midnight);
		
		int sec_end_day=(60-second) + ((60-1-minute)*60) + (24-1-hour)*3600;
		System.out.println("Number of seconds remaining in the day: "+sec_end_day);
		
		float total=sec_since_midnight+sec_end_day;
		float perc_day_passed=(sec_since_midnight/total)*100;
		
		System.out.println("Percentage of the day that has passed: "+perc_day_passed);
		hour=14;
		minute=46;
		second=45;
		int total_sec2=(hour*3600)+(60*minute)+second;
		
		int sec_elapsed=total_sec2-total_sec1;
		System.out.println("Elapsed time: "+sec_elapsed+" seconds");
		
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS