Results 1 to 15 of 15

Thread: setTime();

  1. #1
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Question setTime();

    The Steltel telephone company has the following rate structure:

    Any call started after 6pm (1800 hours) gets a 50% discount.
    Any call started after 8am (0800 hours) is charged at full price.
    All calls are subject to a 4% VAT (value added tax).
    The regular rate for a call is R0.40 per minute.
    Any call longer than 60 minutes receives a 15% discount on its cost (after any other discount is taken but before tax is added).

    Write a program with the user's starting time and ending time of a telephone call as command-line argument, and displays the cost of the call. Make sure to test your program for all the combinations of possible conditions, even phone calls that last past midnight!

    ------------------------------------------------------------------------------------------------------------------

    The only thing I need help with is how to set two different times (Begin time and End time) so that I can calculate the cost of the phone call. I tried to use the setTime() function but could not get two differrent times set. I also tried creating double vairibles and give it a value of 1800 for 18:00 but then the minutes would not work correctly. That is the closest I could come.
    Any advice and remember that I am no expert, I only started last week to program with java.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: setTime();

    What setTime() method?
    What classes are you allowed to use?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: setTime();

    I used this video to try and figure out how to do it...
    Java Programming Tutorial - 36 - Time Class - YouTube

    That did not work for me so I passed.
    I then tried this...

    double startTime = 1800;
    double endTime = 1900;
    double dif = endTime - startTime;

    But with this I do not get the desired answer as this is not set to time. According what I typed there, there is 100 minutes in an hour in stead of the normal 60. This give met he incorrect answers. If I could just figure out how to do this then it will be a breeze. :0

    I don't know what you mean wih classes, but I only use public class and public static void main(String[] args).
    I don't know how to make commandline arguments, only variables. We were told to ignore that for the time being.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: setTime();

    OK, so no external classes (that would be things like Java's own Date class, for example).

    There are a couple of ways you could go.
    Use a startHour, startMinute, endHour and endMinute then provide a way of calculating the difference between them in minutes.
    Or a startTime and endTime which represent the time of day in minutes (so 0800 would be 480, and 1800 would be 1080 (I think)). That would require you to interpret the input (when you get to taking in input), but your calculation would be a lot easier.

    Other than that, you could write a Time class, but you seem to have had a look at that and gone *eek*, so pick one of the above?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: setTime();

    I'm hearing what you are saying. But that is where I'm stuck. 18:00pm would be 1080 minutes like you are saying, but when you say 18:59 that would normally = 1139 minutes. But if you take 1859 and convert that to minutes using /100 * 60 you get 1115,4 which is incorrect. This is were I seem to be getting my poblem. Maybe Im just missing something very small.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: setTime();

    1080/60=18??
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: setTime();

    As PhHein shows, you divide by 60 to get the hours, and the mod 60 gives you the minutes.
    Java Code:
    int hours = startTime/60;  // Stick to int division.
    int minutes = startTime%60;
    If everything is an int (which it should be) then int division results in any remainder being dropped off.
    You don't need to use doubles.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: setTime();

    Moved from Eclipse

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: setTime();

    @PhHein
    Sorry, I did not know what you ment, I thought that you did not know that sum would give that answer.

    @Tolls
    Thank you for your help. I tried what you said and I still did not get it right quite lke I want it to. But thanks to you showing the usement of the modus it gave me an idea...

    public static void main(String[] args) {

    int startTime = 1645;
    int hours = (startTime/100)%100;
    int minutes = startTime%100;

    System.out.println(hours);
    System.out.println(minutes);
    As far as I can tell I'm getting the desired results.
    PS: How do you add code like you did???

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: setTime();

    How does:
    Java Code:
    int hours = (startTime/100)%100;
    int minutes = startTime%100;
    compare with the sample I wrote above?
    Java Code:
    int hours = startTime/60;
    int minutes = startTime%60;
    Do you know what that '60' represents in my code there?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: setTime();

    Oh yes, as for [code] tags [/code] just wrap your code in them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: setTime();

    If I'm not mistaken the 60 in hours will divide the startTime so that I can get the number of hours. The 60 at the minutes will also divide but will give me the reamaining numbers from startTime ex. 1125 = 18 hours and 45 minutes.

    But I'm not looking to write the minutes every time I want to write it in this format hh:mm:ss. With my code if I write 1845 it will give me 18 hours and 45 minutes. Hope I did not get mixed up somewhere.
    I evolved my code even a bit more by adding seconds ex. 184559 will now give me 18 hours 45 minutes 59 seconds.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: setTime();

    Quote Originally Posted by Tolls View Post
    Do you know what that '60' represents in my code there?
    Two thirds of a right angle? The average age of a hippopotamus? 2x2x3x5? Nah, I give up ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: setTime();

    You wanted a way of doing the calculation.
    The example I gave was one way of making that as easy as possible, that is storing the time in minutes rather than hours/minutes. Though it now sounds like you need it in seconds, but that's just a simple extension of the above.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: setTime();

    I'm to lazy to convert it to minutes every time, I may have more code than you but I think my way is better for this that I'm trying to do. But I would have never have thought of ot if you did not give your piece of code. That opended the doors fo me :) In a way I'm still using your code but in a different way. I just want to know if I was right about the 60 you asked about. I have to try and pick every trick as my university classes start next week.

    When I'm finished with what I'm doing ( next day or 2, I'm a very lazy person ) I'll post the code here and then maybe you'll agrea with me about the way I'm typing the time. :():
    Last edited by Asmicor; 01-28-2013 at 06:52 PM.

Posting Permissions

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