Solved

Help with salary calculations

Posted on 2004-10-30
781 Views
Last Modified: 2008-03-18
I am currently in a java class and am looking for some help on a program that is due soon. It is called Pennies for Pay and wants the user to input a # of days worked between 1 and 40. The salary starts at $.01 for day 1 and doubles for every day worked. What I have done, is wrote my program with 40 if then statements which requires no calculation at all just an input that corresponds with an output. This has made my code very long. Other people in the class say their code is only 11 lines long and includes simple equations such as salary=pay+salary and pay=pay*2. The problem is that I am confused as to how to do the calculation without 40 if then statements. I have the rest of the program done and I already have a full program as stated before, I just would rather turn in it with a calculation and less code. Any help would be urgently appreciated. I am not looking for the answers or someone to do it for me, just a head in the right direction on this smal portion. Thank you in advance.
0
Question by:nikese
    15 Comments
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    homework??
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    Homework can't be done here as per EE rules and regulations. If u have done something with your code and facing some problems like compile time errors and runtime errors then people can help you.

    So let us know how far you know abt java annd how far you have written the code for your current task

    Regards
    Sudhakar
    0
     

    Author Comment

    by:nikese
    Well I know what the rules are and thats why I was upfront about it being homework. I am not asking for the answers. As previously stated I have a program that works with about 40 if then statements. All I am wanting is help on an alternate way of possibly doing a program like this with a calculation instead. This help would not solely be used for this assignment but as fututre reference on how a calculation like this can be done. I am not asking for a program or someone to do it for me. Just a little insight.
    0
     

    Author Comment

    by:nikese
    This is basically what it looks like now except that it goes all the way to 40 days.

    public class PenniesXXX

    {

          public static void main(String[] args)

                {

                /* Variables */

                int days = 0;

                /* Description of program for user. */

                System.out.println("This program will calculate your salary for a number of days");
                System.out.println("between 0 and 40, with your salary starting at $.01");
                System.out.println("and doubling for every day worked.");

                System.out.println("\nPlease enter the number of days you worked.");
                days = SavitchIn.readLineInt();

                /* Days entered with a value greater than 40 or less than 0 are invalid. */

                if ((days > 40) && (days < 0));
                System.out.println("You have entered an invalid number of days.");

                /* Program outputs for # of days entered. */

                if (days == 1) { System.out.println("You worked 1 day. \nYour pay on day 1 is $.01.");
                      System.out.println("Your total salary is $.01");}
                if (days == 2) { System.out.println("You worked 2 days. \nYour pay on day 2 is $.02.");
                      System.out.println("Your total salary is $.03");}
                if (days == 3) { System.out.println("You worked 3 days. \nYour pay on day 3 is $.04.");
                      System.out.println("Your total salary is $.07");}
                if (days == 4) { System.out.println("You worked 4 days. \nYour pay on day 4 is $.08.");
                      System.out.println("Your total salary is $.15");}
    0
     

    Author Comment

    by:nikese
    This is the table I did in excel to get the calculations to type into the program.

    Pay Day      Pay      Total Salary

    1      $0.01      $0.01
    2      $0.02      $0.03
    3      $0.04      $0.07
    4      $0.08      $0.15
    5      $0.16      $0.31
    6      $0.32      $0.63
    7      $0.64      $1.27
    8      $1.28      $2.55
    9      $2.56      $5.11
    10      $5.12      $10.23
    11      $10.24      $20.47
    12      $20.48      $40.95
    13      $40.96      $81.91
    14      $81.92      $163.83
    15      $163.84      $327.67
    16      $327.68      $655.35
    17      $655.36      $1,310.71
    18      $1,310.72      $2,621.43
    19      $2,621.44      $5,242.87
    20      $5,242.88      $10,485.75
    21      $10,485.76      $20,971.51
    22      $20,971.52      $41,943.03
    23      $41,943.04      $83,886.07
    24      $83,886.08      $167,772.15
    25      $167,772.16      $335,544.31
    26      $335,544.32      $671,088.63
    27      $671,088.64      $1,342,177.27
    28      $1,342,177.28      $2,684,354.55
    29      $2,684,354.56      $5,368,709.11
    30      $5,368,709.12      $10,737,418.23
    31      $10,737,418.24      $21,474,836.47
    32      $21,474,836.48      $42,949,672.95
    33      $42,949,672.96      $85,899,345.91
    34      $85,899,345.92      $171,798,691.83
    35      $171,798,691.84      $343,597,383.67
    36      $343,597,383.68      $687,194,767.35
    37      $687,194,767.36      $1,374,389,534.71
    38      $1,374,389,534.72      $2,748,779,069.43
    39      $2,748,779,069.44      $5,497,558,138.87
    40      $5,497,558,138.88      $10,995,116,277.75
    0
     

    Author Comment

    by:nikese
    The specific code from above gives the following output:

    You have entered an invalid number of days.
    You worked 35 days.
    Your pay on day 35 is $1.7179869184E8
    Your total salary is 3.4359738366999996E8

    I think that for the solution of decimal point I need in import a decimal formatting file that I have. I guess the biggest problem is the original problem from the beginning, the calculations. The answer the program gave isn't even on the table. Hmmmmm, I will keep brainstorming and I hope that you are still willing to help. Thanks you again.
    0
     

    Author Comment

    by:nikese
    The above code gives the output:

    You have entered an invalid number of days
    You worked 35 days.
    Your pay on day 35 is $1.7179869184E8
    You total salary is 3.4359738366999996E8

    I'm thinking my original problem is still here, the calculations. I am going to continue to brainstorm. I think to fix the decimal issue, I need to import a decimal formatting file that I have. I am going to study the above explanations you gave me and try to find out where to fix the calculation. The wierd thing is, if I enter 25 days worked, it gives me, $167772.16 for pay and 335544.31000000006 as total salary. I think the calculations are actually correct, it's just the decimal formatting is wrong. Will the importing of that file fix that?
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    OK

    I understand your requirement

    Check this code

    public class Penies

    {
       
        public static void main(String[] args)
       
        {
           
            /* Variables */
           
            int days = 0;
           
            /* Description of program for user. */
           
            System.out.println("This program will calculate your salary for a number of days");
            System.out.println("between 0 and 40, with your salary starting at $.01");
            System.out.println("and doubling for every day worked.");
           
            System.out.println("\nPlease enter the number of days you worked.");
            //    days = SavitchIn.readLineInt();
           
            /* Days entered with a value greater than 40 or less than 0 are invalid. */
           
            if ((days > 40) && (days < 0));
            System.out.println("You have entered an invalid number of days.");
           
            /* Program outputs for # of days entered. */
            for(int i=1;i<=40;i++) {
                print(i,0.1)  ;
            }
           
        }
       
        public static void print(int days, double value) {
            System.out.println("You worked "+days +" days. \nYour pay on day "+days+" is $"+.01*Math.pow(2,days-1)+".");
            double total=total(days,value);
            System.out.println("Your total salary is "+total);
        }
        public static double total(int days,double value) {
            if(days==0)
                return 0;
            double total=.01*Math.pow(2,days-1)+total(days-1,value);
            return total;
        }
    }
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    As per your requirement  your code should be



               days = SavitchIn.readLineInt();      
            /* Days entered with a value greater than 40 or less than 0 are invalid. */      
            if ((days > 40) && (days < 0));
            System.out.println("You have entered an invalid number of days.");
                print(days,0.1)  ;


    so change the above code as needed

    Regards
    Sudhakar
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    public class Penies

    {
       
        public static void main(String[] args)
       
        {
           
            /* Variables */
           
            int days = 0;
           
            /* Description of program for user. */
           
            System.out.println("This program will calculate your salary for a number of days");
            System.out.println("between 0 and 40, with your salary starting at $.01");
            System.out.println("and doubling for every day worked.");
           
            System.out.println("\nPlease enter the number of days you worked.");
            //    days = SavitchIn.readLineInt();
           
            /* Days entered with a value greater than 40 or less than 0 are invalid. */
           
            if ((days > 40) && (days < 0));
            System.out.println("You have entered an invalid number of days.");
           
            /* Program outputs for # of days entered. */
            for(int i=1;i<=40;i++) {
                print(i,0.1)  ;
            }
           
        }
       
        public static void print(int days, double value) {
            System.out.println("You worked "+days +" days. \nYour pay on day "+days+" is $"+value*Math.pow(2,days-1)+".");
            double total=total(days,value);
            System.out.println("Your total salary is "+total);
        }
        public static double total(int days,double value) {
            if(days==0)
                return 0;
            double total=value*Math.pow(2,days-1)+total(days-1,value);
            return total;
        }
    }
    0
     

    Author Comment

    by:nikese
    Thank you very much for your understanding and your help. I have a few questions. In the last comment posted, what is the print(days,0.1) for and what does it do. Also, is there any way you could explain the math portions of the code that you posted. I am sorry, and you have helped lots, I just want to make sure I can understand it and be able to use it again down the road. This is how I understand it, please tell me if and where I am wrong.

    ***Here, first I would have to define "i" at the beginning of my program. Then this states that i is equal to 1 and is less than or equal to 40. I'm thinking this just keeps the range between 1 and 40. i++ is adding 1 to i.

            /* Program outputs for # of days entered. */
            for(int i=1;i<=40;i++) {
                print(i,0.1)  ;
            }
           
        }
       ***Here is particulary what I think I was looking for.  Is there any way you specifically explain these lines:
    Math.pow(2,days-1)
    double total=.01*Math.pow(2,days-1)+total(days-1,value);
    ***Thank you again for all your help

        public static void print(int days, double value) {
            System.out.println("You worked "+days +" days. \nYour pay on day "+days+" is $"+.01*Math.pow(2,days-1)+".");
            double total=total(days,value);
            System.out.println("Your total salary is "+total);
        }
        public static double total(int days,double value) {
            if(days==0)
                return 0;
            double total=.01*Math.pow(2,days-1)+total(days-1,value);
            return total;
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    a small  change hence above code.

    Now I will try to explain how  this works. I created two methods for thsi to work



       public static void print(int days, double value) {
            System.out.println("You worked "+days +" days. \nYour pay on day "+days+" is $"+value*Math.pow(2,days-1)+".");
            double total=total(days,value);
            System.out.println("Your total salary is "+total);
        }


    this formula calculates the current days salary
    value*Math.pow(2,days-1)

    Ex :
    value = 0 .01
    days=2

    0.01* 2^1= 0.02  so for day 2 the salary is $0.02

    Hope you understand till here

    now to calculate the total salary below method works. this is recursive

     public static double total(int days,double value) {
            if(days==0)
                return 0;
            double total=value*Math.pow(2,days-1)+total(days-1,value);
            return total;
        }

    fior same above example
    total(2,0.01) can be resolved into

    0.01*2^(2-1)+total(1,0.01); this further can be resolved into
    0.01*2^(2-1)+0.01*2^(1-1);
    0.0.2+0.01=0.03

    Let me know if you need further explanation

    Regards
    Sudhakar
    0
     
    LVL 14

    Accepted Solution

    by:
    Test this code and ask me your questions. I am ready to give you explanation about  this execution

    Regards
    Sudhakar

    public class Penies {
        public static void main(String[] args) {
            int days = 0;
            System.out.println("This program will calculate your salary for a number of days");
            System.out.println("between 0 and 40, with your salary starting at $.01");
            System.out.println("and doubling for every day worked.");
            System.out.println("\nPlease enter the number of days you worked.");
            days = SavitchIn.readLineInt();
            if ((days > 40) && (days < 0));
            System.out.println("You have entered an invalid number of days.");
            print(days,0.01)  ;
        }
        public static void print(int days, double value) {
            System.out.println("You worked "+days +" days. \nYour pay on day "+days+" is $"+value*Math.pow(2,days-1)+".");
            double total=total(days,value);
            System.out.println("Your total salary is "+total);
        }
        public static double total(int days,double value) {
            if(days==0)
                return 0;
            double total=value*Math.pow(2,days-1)+total(days-1,value);
            return total;
        }
    }
    0
     

    Author Comment

    by:nikese
    I just wanted to say thank you very much to Sudhakar for all your help and explanations. Help is no good without the explanation of how to get where you are wanting to go. This is definately something that I think will be useful in all of my further programming. You were patient and thourough. Thanks a million!!
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya

      if ((days > 40) && (days < 0));
            System.out.println("You have entered an invalid number of days.");


    must be
      if ((days > 40) || (days <= 0));
       {
            System.out.println("You have entered an invalid number of days.");
                    return;
       }
     print(days,0.01)  ;

    It was my mistake friend,  I haven't noticed. Sorry for this confusion
     
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Suggested Solutions

    Title # Comments Views Activity
    Deactivate - DEBUG print on console 2 20
    wordsCount challenge 10 39
    count7 challenge 12 28
    countPairs challenge 7 20
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now