1. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,232
Rep Power
23

## Re: setTime();

What setTime() method?
What classes are you allowed to use?

3. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,232
Rep Power
23

## 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?

5. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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.

1080/60=18??

7. Moderator
Join Date
Apr 2009
Posts
13,232
Rep Power
23

## 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.

8. ## Re: setTime();

Moved from Eclipse

db

9. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,232
Rep Power
23

## 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?

11. Moderator
Join Date
Apr 2009
Posts
13,232
Rep Power
23

## Re: setTime();

Oh yes, as for [code] tags [/code] just wrap your code in them.

12. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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. ## Re: setTime();

Originally Posted by Tolls
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

14. Moderator
Join Date
Apr 2009
Posts
13,232
Rep Power
23

## 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.

15. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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 07: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
•