Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Help with salary calculations

Posted on 2004-10-30
Medium Priority
787 Views
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 7

LVL 14

Expert Comment

ID: 12455848
homework??
0

LVL 14

Expert Comment

ID: 12455853
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

ID: 12455865
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

ID: 12455930
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 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.");
if (days == 2) { System.out.println("You worked 2 days. \nYour pay on day 2 is \$.02.");
if (days == 3) { System.out.println("You worked 3 days. \nYour pay on day 3 is \$.04.");
if (days == 4) { System.out.println("You worked 4 days. \nYour pay on day 4 is \$.08.");
0

Author Comment

ID: 12455936
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

ID: 12456116
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

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

ID: 12456139
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

ID: 12456053
OK

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

LVL 14

Expert Comment

ID: 12456057

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

ID: 12456065
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 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);
}
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);
}
}
0

Author Comment

ID: 12456080
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);
}
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);
0

LVL 14

Expert Comment

ID: 12456084
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);
}

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

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

sudhakar_koundinya earned 2000 total points
ID: 12456094

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

Author Comment

ID: 12460680
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

ID: 12516638

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

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Javaâ€¦
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â€¦
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 22 hours left to enroll