Results 1 to 5 of 5
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default Whats wrong with my maths???

    Can someone please help, keep getting told there is a problem with the following code i have highlighted in bold, any advise?

    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");
    distance=UserInput.readDouble();

    time=distance/speed;


    double hours;
    double minutes;
    int seconds;

    int addMinutes;
    int addHours;

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

    minutes=((int)time)-(time);
    minutes=minutes*100+addMinutes;
    addHours=minutes/60;
    minutes=minutes%60;

    hours=time+addHours;


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




    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,778
    Rep Power
    25

    Default

    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?

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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 05:45 PM. Reason: Corrected a misspell
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  5. #5
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    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

Similar Threads

  1. whats my next step
    By thirumurugan.sethu in forum New To Java
    Replies: 4
    Last Post: 10-02-2008, 10:03 PM
  2. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:57 PM
  3. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 01:54 PM
  4. Replies: 15
    Last Post: 11-30-2007, 06:19 PM
  5. Generates random maths questions
    By cachi in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 05:48 AM

Posting Permissions

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