# Thread: Whats wrong with my maths???

1. Member Join Date
Oct 2008
Posts
39
Rep Power
0

## 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 " );

}
}  Reply With Quote

2. ## 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?  Reply With Quote

3. ## 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:
Java 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
Last edited by CJSLMAN; 11-02-2008 at 04:45 PM. Reason: Corrected a misspell  Reply With Quote

4. ## 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.  Reply With Quote

5. Member Join Date
Oct 2008
Posts
19
Rep Power
0

## yea your code isn't clear enough but i wrote what i understands you wanted to seperate seconds from minutes from hours right??

Java 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•