Help with salary calculations

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.
nikeseAsked:
Who is Participating?
 
sudhakar_koundinyaConnect With a Mentor Commented:
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
 
sudhakar_koundinyaCommented:
homework??
0
 
sudhakar_koundinyaCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nikeseAuthor Commented:
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
 
nikeseAuthor Commented:
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
 
nikeseAuthor Commented:
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
 
nikeseAuthor Commented:
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
 
nikeseAuthor Commented:
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
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
nikeseAuthor Commented:
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
 
sudhakar_koundinyaCommented:
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
 
nikeseAuthor Commented:
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
 
sudhakar_koundinyaCommented:

  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
All Courses

From novice to tech pro — start learning today.