• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to compute time?

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i've got an exercise to do wherein i have to get two inputs of time in military format. I need to print out the result as e.g. "8 hours 21 mins".

I did the TimeInterval class and basically i use Math.min & Math.max to determine which to subtract from which.

Anyway, to cut a long story short i have to divide it by 100 so that I could extract the hours from the minutes.

Problem is, I can't figure out what is wrong. Is it my math or my logic.

Question: Is there a better way to do this? Like a class or sumthin'
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having extracted the hours, how about multiplying by 60 to get the minutes. If you then convert the two times into minutes, subtract them and divide by 60, you should get hours and the remainder in minutes.

So, 2301 and 1615 will convert to 1381 minutes and 975 minutes. Substraction results in 406, which is 6hr 46min when divided by 60.

I haven't coded this, so I'm not sure if you will have a problem in correctly getting the remainder.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Megs,

post your code, and we'll be happy to look at it. it's hard to tell if you logic is wrong without seeing it.

also, tell us exactly what result you are getting vs. what result you are expecting.

finally, there may be a class that could help you, but what is the purpose of this assignment... are you supposed to find a utility class that does what you need, or are you supposed to figure out how to code it yourself?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a hint, snoop the JavaDoc for interesting ways to handle dates. They didn't separate time so you won't find much about time without date.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again here's my code

i put in 200 and 200, i'm supposed to get 0 hours and 0 mins, but i get 1.0 hours and some minutes.

TIA
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made some changes and got the result. The changed code is:

public class TimeInterval {
/** constructs an object that computes the time interval between two times
* @param aTime the first time@param anotherTime the second time */
public TimeInterval(int aTime, int anotherTime) {
time1 = aTime;
time2 = anotherTime;
intervalTime = 0;
hours = 0;
}
/** computes for the number of hours of interval less the minutes
* @return interval in hours */
public double getHours() {
if (time1 != time2) {
System.out.println("max =" + Math.max(time1, time2));
System.out.println("min =" + Math.min(time1, time2));
double timea = (Math.max(time1, time2)) / 100;
double timeb = (Math.min(time1, time2)) / 100;
System.out.println("timea =" + timea);
System.out.println("timeb =" + timeb);
int hour1 = (int)timea;
double remMin1 = (timea - hour1) * 100;
int hourMin1 = hour1 * 60;
timea = hourMin1 + remMin1;
/*hour1 = hour1 * 60;
time1 = hour1 + remMin1;Commented from original code*/
int hour2 = (int)timeb;
double remMin2 = (timeb - hour2) * 100;
int hourMin2 = hour2 * 60;
timeb = hourMin2 + remMin2;
/*double remMin2 = (timeb - hour2) * 100;
hour2 = hour2 * 60;
time2 = hour2 + remMin2;
intervalTime = (time1 - time2) / 100;Commented from original code*/
intervalTime = (timea - timeb) / 60;
System.out.println("intervalTime =" + intervalTime);
return hours = (int)intervalTime;
} else {
hours = 0;
intervalTime = 0;
return hours;
}
}
/** computes the number of minutes excess of one hour of interval
* @return interval minutes */
public double getMinutes() {
minutes = (intervalTime - hours ) * 60;
return minutes;
}

public double totalTime() {
totaltime = ((hours * 60) + minutes) / 60;
return totaltime;
}
private double intervalTime, time1, time2, totaltime, minutes;
private int hours;
}



I got fairly good results. One thing to note is you should not give inputs in three digits as 200 or 210 as time is always in four digits.

I put some output statements for tracking some outputs.
Here I have one question: When I give input as 2100 and 0100
I got output of Math.min(2100, 0100) as 64.0?
I don't why? If anybody can then please explain.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I put some output statements for tracking some outputs.
Here I have one question: When I give input as 2100 and 0100
I got output of Math.min(2100, 0100) as 64.0?
I don't why? If anybody can then please explain.


How do you enter these time values in your program? The behavior described leads me to believe that they are "hardcoded" in your program. If you have a number in a Java program that starts with a leading zero, it is interpreted as octal (base 8). 0100 base 8 is the same as 64 base 10. If you are unfamiliar with number systems, you should google for information on how to convert between numbers in different bases.

If you let the user enter these numbers, either as command line arguments or using an InputStream or Reader of some sort, you can avoid these issues.

HTH

Layne
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Layne,

The input will be through JOptionPane and I can't use boolean yet as this is an exercise for just Chapter 3 of the book.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'd make one suggestion... when you do your

time1 = (Math.max(time1, time2)) / 100;

you're sort of losing the original value you got from the user. i'd personally use another temp. variable, to store the hours calculated value. after all, what happens if in 3 months, you need that input value again?

doing that might actually simplify your code. to get the hours, you can just do a division by 100 and a cast. you can then use the modulus operator to get the minutes, and then get the total minutes by a simple calculation:

(i think this is right)
 
vipul kaushik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya, I think it is right.
Thanks
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic