Megs Maquito

Ranch Hand

Posts: 84

posted 12 years ago

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'

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'

I'm a Hood Ornament

Roger Chung-Wee

Ranch Hand

Posts: 1683

posted 12 years ago

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.

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.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3

posted 12 years ago

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?

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?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 12 years ago

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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Megs Maquito

Ranch Hand

Posts: 84

vipul kaushik

Greenhorn

Posts: 2

posted 12 years ago

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.

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.

Layne Lund

Ranch Hand

Posts: 3061

posted 12 years ago

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

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

posted 12 years ago

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.

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.

I'm a Hood Ornament

posted 12 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

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)

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)