Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Exercise that prints the cell phones' bills. Please help.

  1. #1
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Exercise that prints the cell phones' bills. Please help.

    This is a part from the code of my application. With the following method i want to calculate the cost of each call. Each call "belongs" to a service. So i call the method in main with argument the phone number of the service. The problem (specified with red color) is that i cannot calculate the cost correctly for the first call that overtakes the free time.
    For example, the free time is 30 min and 0,1 cost for every additional minute. The first call lasts 15 min and the second 20. The result must be: for the first call, cost 0, and for the second, cost 0,1*5=0,5.
    public void printCalls(String num) 
            {
                double j = 0;  /* total duration */
                double y = 0; /* cost of each call*/
     
                for(int i=0; i<Call.calls.size();i++)  /* for every call */
                    {
    			if (Call.calls.get(i).getCallingNumber().equals(num))
                                 /* if the calling number equals the service's number */
                                {    
     
                                    j+= Call.calls.get(i).getCallingDuration();
                                    /* increase j by duration of call*/
     
                                    if (j <= 30*60)    /* if j < free calling time*/
                                        {
                                            y = 0;
                                        }
                                  else y = Call.calls.get(i).getCallingDuration()*0.11;
                                               System.out.printf  
                                                ("%s. %s: %s %s: %s %s: %s %s %s: %s’’ %s: %.2f€\n",
                                                "Call" , "From" , Call.calls.get(i).getCallingNumber() ,
                                                " To" ,  Call.calls.get(i).getDialedNumber(),
                                                " Time" , Call.calls.get(i).getCallingDate(),Call.calls.get(i).getCallingTime(),
                                                " Duration" , Call.calls.get(i).getCallingDuration() ,
                                                " Cost" , y);                     
                                }          
    		}
            }
    I think the solution would be something like this (in pseudocode):
    if  total duration <= free duration
        cost = 0;
     
    else 
       {
          if total duration [B][U]of the previous iteration (i-1)[/U][/B] <= free duration
              cost = (duration - (free duration - total duration of the previous iteration) )* cost         
     
          else duration * cost
          }
    But the problem is that i don't know how to get the value of total duration of the previous iteration.
    Any Suggestions?
    Last edited by helloworld922; January 4th, 2012 at 08:05 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,952
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Exercise that prints the cell phones' bills. Please help.

    i cannot calculate the cost correctly for the first call that overtakes the free time.
    Are you saying that you need more logic than can be done in a single statement like your statement in red? What keeps you from getting the time as a variable and then using some if statements and arithmetic statements to break the time into the different chargeable times?

    Take a piece of paper and work out the steps for breaking the time up for the different charges.
    You need to understand how to do it with paper and pencil before you can write the code.

  3. The Following User Says Thank You to Norm For This Useful Post:

    andreas90 (January 5th, 2012)

  4. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Exercise that prints the cell phones' bills. Please help.

    Quote Originally Posted by Norm View Post
    Are you saying that you need more logic than can be done in a single statement like your statement in red? What keeps you from getting the time as a variable and then using some if statements and arithmetic statements to break the time into the different chargeable times?

    Take a piece of paper and work out the steps for breaking the time up for the different charges.
    You need to understand how to do it with paper and pencil before you can write the code.
    I 've done it and ended up with the solution that's been written in the end of the thread. I 've got all the variables i need, but i cannot find a way to retrieve the value of the total duration from the previous iteration, which i wanna use in my if statements

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,952
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Exercise that prints the cell phones' bills. Please help.

    a way to retrieve the value of the total duration from the previous iteration
    What do you mean by "the previous iteration"?
    In a loop you would have a variable that saves the value from the previous iteration:
    int lastValue = -1;  // will save value here
    for(....)  {
      value = (some formula)
       ... here lastValue if != -1 is the value from the previous iteration
       lastValue = value; // save this value for the next iteration
    } // end for loop

  6. The Following User Says Thank You to Norm For This Useful Post:

    andreas90 (January 5th, 2012)

  7. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Exercise that prints the cell phones' bills. Please help.

    Thanks a lot Norm! Problem solved!

Similar Threads

  1. Replies: 3
    Last Post: October 19th, 2011, 02:17 PM
  2. Help with code that prints number of evens, odds, and 0's
    By trogan234 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2011, 06:50 PM
  3. nothing prints after runing code
    By darego in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2011, 10:14 AM
  4. How to start writing Java Apps for Cell Phones?
    By Jchang504 in forum Java ME (Mobile Edition)
    Replies: 7
    Last Post: January 10th, 2011, 04:11 AM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM