Win a copy of Spring Boot in Practice this week in the Spring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# How to solve part 6 of the question?

Greenhorn
Posts: 8
1
• Number of slices to send:
Optional 'thank-you' note:
This is a problem from "Think Java" book.I'm kinda stuck at the last point(i.e. #6)

Exercise 2.3 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
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 2pm 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
working on this exercise.

Need some hint, i have solved other parts but not able to think how to do this.
Pasting my code:

Sheriff
Posts: 7113
184
• Number of slices to send:
Optional 'thank-you' note:
See if this helps.

Bartender
Posts: 3323
86
• Number of slices to send:
Optional 'thank-you' note:
As the OP isn't using Time objects now I suspect he/she is required to use System.currentTimeMillis() to get the milliseconds since epoch and then extract the current seconds, minutes and hours using a combination of the division and modulus operators.

Achyut Sharma
Greenhorn
Posts: 8
1
• Number of slices to send:
Optional 'thank-you' note:
naah..unable to make any logical statement for part 6...moving ON >>
anyways thanks.

Bartender
Posts: 1205
22
• 1
• Number of slices to send:
Optional 'thank-you' note:
If the values from Step 2 for hour, minute and second are 18, 15 and 30, respectively, and then in Step 6 hour2=23, minute2=59, second2=59, how long have you been working on the problem?  Do any of the tasks between 2 and 6 help?

Tony Docherty
Bartender
Posts: 3323
86
• 1
• Number of slices to send:
Optional 'thank-you' note:

Achyut Sharma wrote:naah..unable to make any logical statement for part 6...moving ON >>
anyways thanks.

Giving up when you get stuck is not the best attitude to have when learning to program because you're going to constantly find situations where you get stuck like this and you need to ask more questions so we can do a better job of explaining how to continue rather than moving on to the next problem.
As it happens it looks like the advice I gave wasn't correct as I had misunderstood the question. Ryan's post made me reread your original post and he's on the right lines. If you want to know more post a reply and I'll explain further.

Achyut Sharma
Greenhorn
Posts: 8
1
• Number of slices to send:
Optional 'thank-you' note:

Tony Docherty wrote:

Achyut Sharma wrote:naah..unable to make any logical statement for part 6...moving ON >>
anyways thanks.

Giving up when you get stuck is not the best attitude to have when learning to program because you're going to constantly find situations where you get stuck like this and you need to ask more questions so we can do a better job of explaining how to continue rather than moving on to the next problem.
As it happens it looks like the advice I gave wasn't correct as I had misunderstood the question. Ryan's post made me reread your original post and he's on the right lines. If you want to know more post a reply and I'll explain further.

int hour = 18;
int minute = 15;
int second = 30;
double pastSeconds = (hour * 3600) + (minute * 60) + second;
double totalSeconds = 24 * 3600;
double remainingSeconds = totalSeconds - pastSeconds;
int newHour = 19;
int newMinute = 21;
int newSecond = 40;

System.out.print("Past Seconds since midnight --> ");
System.out.println(pastSeconds);
System.out.print("Remaining seconds for midnight --> ");
System.out.println(remainingSeconds);
System.out.print("Percent of the day passed --> ");
System.out.println(pastSeconds * 100 / totalSeconds);
System.out.print("Percent of the day remaining --> ");
System.out.println(100 - (pastSeconds * 100 / totalSeconds));
System.out.println("Elapsed time will be --> ");
System.out.println((newHour - hour) +":"+ (newMinute - minute) +":"+ (newSecond - second));
}
}

and yes,its kinda hard to stay motivated everyday, maybe because i don't have a proper environment around me or maybe it's just me.
Got my copy of "Head First Java" ,finished first two chapters today and now i'm not reading "Think java" as i've heard about Head first Java positively in these forums,i sincerely hope to finish it.
Anyways many thanks to both of you Ryan and Tony

Tony Docherty
Bartender
Posts: 3323
86
• 1
• Number of slices to send:
Optional 'thank-you' note:
Well done for going back and solving the problem, that deserves a cow.
It can be really difficult when you are first starting but that's why sites like us are here.  Just remember give it your best shot and if you get stuck post here and we'll do our best to help.

Achyut Sharma
Greenhorn
Posts: 8
1
• Number of slices to send:
Optional 'thank-you' note:
Surely, i will ask more questions in near future.
And, wow my first cow!..that's kinda special and,
many thanks for telling me to be mentally strong,i will keep that in my mind.

 Consider Paul's rocket mass heater.