programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to solve part 6 of the question?

Achyut Sharma
Greenhorn
Posts: 8
1
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:

Knute Snortum
Sheriff
Posts: 4279
127
See if this helps.

Tony Docherty
Bartender
Posts: 3271
82
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
naah..unable to make any logical statement for part 6...moving ON >>
anyways thanks.

Ryan McGuire
Ranch Hand
Posts: 1143
9
• 1
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: 3271
82
• 1
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
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: 3271
82
• 1
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
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.