Solved

Posted on 2004-10-30

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.

15 Comments

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

Regards

Sudhakar

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("betwee

System.out.println("and doubling for every day worked.");

System.out.println("\nPlea

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");}

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

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.

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?

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("betwee

System.out.println("and doubling for every day worked.");

System.out.println("\nPlea

// 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-

return total;

}

}

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

{

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("betwee

System.out.println("and doubling for every day worked.");

System.out.println("\nPlea

// 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,day

return total;

}

}

***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-

***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-

return total;

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,day

return total;

}

fior same above example

total(2,0.01) can be resolved into

0.01*2^(2-1)+total(1,0.01)

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

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("betwee

System.out.println("and doubling for every day worked.");

System.out.println("\nPlea

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,day

return total;

}

}

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

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Deactivate - DEBUG print on console | 2 | 20 | |

wordsCount challenge | 10 | 39 | |

count7 challenge | 12 | 28 | |

countPairs challenge | 7 | 20 |

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

Connect with top rated Experts

**5** Experts available now in Live!