# Whats wrong with my maths???

• 11-02-2008, 01:58 PM
soc86
Whats wrong with my maths???

public class travel{

public static void main (String[]arg) {

double speed =85;
double time;
double distance;

System.out.println("Please enter the distance in Kilometres");

time=distance/speed;

double hours;
double minutes;
int seconds;

seconds =((int)time*100)-(time*100);
seconds=seconds+0.00555;
seconds=seconds*100;
seconds=seconds%60;

minutes=((int)time)-(time);
minutes=minutes%60;

System.out.println("To travel "+ distance +" kilometres it takes "+ (int)hours +" hours "+ (int)minutes +" minutes and "+ (int)seconds +" seconds " );

}
}
• 11-02-2008, 03:08 PM
Norm
Quote:

getting told there is a problem with the following code i have highlighted in bold, any advise?
What have you been told?
Do you get error messages when you compile?
What is the highlighted code supposed to do? Does it do that?
Is the output correct?
• 11-02-2008, 04:41 PM
CJSLMAN
How to calc time
A couple of things:
• Give the forum the information it needs to help you. Like Norm said, explain why it is not working: what errors are you getting? What results are you expecting?
• Put comments in your code: it helps you and help others that look at your code. For example, why are you adding 0.00555 when calculating the seconds?
• Not remembering my math classes, the first thing that stands out is the statement:
Code:

`hours=time+addHours;`
You can't add anything to "hours", much less "time". Hours is a subset of time. For example, if time = 2.35294, then hours = 2. There's nothing to add.
• I don't understand the way you calculate the minutes and seconds. I think it's wrong. Hint: calculate first the minutes, then calculte the seconds

CJSL
• 11-03-2008, 05:34 AM
Eranga
First of all, you should clearly explain what you code suppose to do. Seems you are doing some calculations, but the way you have done is completely wrong. First find the correct way that conversions in time. Then identify the correct data types to handle the application.
• 11-03-2008, 06:52 PM
gamilah
yea your code isn't clear enough but i wrote what i understands you wanted to seperate seconds from minutes from hours right??

Code:

```import java.util.Scanner; public class travel{ public static void main (String[]arg) { double speed =85;  //  that's in kilometres per hour double time; double distance; System.out.println("Please enter the distance in Kilometres"); Scanner UserInput= new Scanner (System.in); distance= UserInput.nextDouble(); time=distance/speed;// time in hours (double) double hours; double minutes; double seconds; hours =(int)time; minutes =(int)((time-hours)*60); seconds=(int)((((time-hours)*60)-minutes)*100); System.out.println("To travel "+ distance +" kilometres it takes "+ hours +" hours "+ minutes +" minutes and "+ seconds +" seconds " ); } }```
i hope its right:D