Java Again.. Well not really... Maybe a little :)

Krueger81

Diamond Member
Feb 4, 2000
4,196
3
81
each worker receives 5% raise each year.

lets say I type in 20000 for starting salary and the dude is age 65 how would I computer that each year

here is the code I have so far and it gives me 21000 as the asnwer. I know my math is not right somewhere


WHOLE CODE:

import java.io.*;

public class problem6
{
public static void main(String[] args)
throws java.io.IOException
{
double age, lcv, startsalary, endsalary = 0;
String s1, s2, s3;

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.println ("Please enter your name:");
s1 = br.readLine().toUpperCase();

System.out.println ("Please enter your age:");
s2 = br.readLine();
age = Double.parseDouble(s2);

System.out.println ("Please enter your starting salary:");
s3 = br.readLine();
startsalary = Double.parseDouble(s3);

for (lcv = 0; lcv <= 65; lcv++)

{
endsalary = (startsalary *= 1.05);

}

System.out.println (age + "\t" +s1+ "\t" +startsalary);
System.out.println (endsalary);
}
}
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
for (lcv = 0; lcv <= age; lcv++)

{
startsalary = startsalary + (startsalary *.05);
}
 

Krueger81

Diamond Member
Feb 4, 2000
4,196
3
81
well that got me closer

but doesn't it have to be yr 1+5% +y2 +5% and then you ad those together till you get to the year that you have typed in?
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Well in VB it's this:
function whatever(PayStart as long, Years as long)

for i=1 to Years
PayStart = PayStart * 1.05
next i
end function
So in java it should be this
for (lcv = 0; lcv <= age; lcv++)

{
startsalary = startsalary + (startsalary *.05);
}

what am I missing?

this should work too

for (lcv = 0; lcv <= age; lcv++)

{
startsalary = (startsalary *1.05);
}


Doesn't this work:
import java.io.*;

public class problem6
{
public static void main(String[] args)
throws java.io.IOException
{
double age, lcv, startsalary, endsalary = 0;
String s1, s2, s3;

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.println ("Please enter your name:");
s1 = br.readLine().toUpperCase();

System.out.println ("Please enter your age:");
s2 = br.readLine();
age = Double.parseDouble(s2);

System.out.println ("Please enter your starting salary:");
s3 = br.readLine();
startsalary = Double.parseDouble(s3);

for (lcv = 0; lcv <= 65; lcv++)

{
startsalary = (startsalary * 1.05);

}

System.out.println (age + "\t" +s1+ "\t" +startsalary);
System.out.println (startsalary);
}
}
 

Krueger81

Diamond Member
Feb 4, 2000
4,196
3
81
Originally posted by: Evadman
Well in VB it's this:
function whatever(PayStart as long, Years as long)

for i=1 to Years
PayStart = PayStart * 1.05
next i
end function
So in java it should be this
for (lcv = 0; lcv <= age; lcv++)

{
startsalary = startsalary + (startsalary *.05);
}

what am I missing?

this should work too

for (lcv = 0; lcv <= age; lcv++)

{
startsalary = (startsalary *1.05);
}

that gives me an answer of 21000

 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Originally posted by: Krueger81
that gives me an answer of 21000
You were outputting the endsalery, not the startsalery. I changed the code. Try my edit.
 

Krueger81

Diamond Member
Feb 4, 2000
4,196
3
81
Originally posted by: Evadman
Originally posted by: Krueger81
that gives me an answer of 21000
You were outputting the endsalery, not the startsalery. I changed the code. Try my edit.

here is the actual problem maybe that will help.

" Write a program to estimate how much a young worker will make before retiring at age 65. Request the worker?s name, age, and starting salary as input. Assume the worker receives a 5 percent raise each year. For example, if the user enters Helen, 25, and 20000, then the following should be displayed on your screen:
Helen will earn about $2415995."

Phil
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Did you try my edit?

import java.io.*;

public class problem6
{
public static void main(String[] args)
throws java.io.IOException
{
double age, lcv, startsalary, endsalary = 0;
String s1, s2, s3;

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.println ("Please enter your name:");
s1 = br.readLine().toUpperCase();

System.out.println ("Please enter your age:");
s2 = br.readLine();
age = Double.parseDouble(s2);

System.out.println ("Please enter your starting salary:");
s3 = br.readLine();
startsalary = Double.parseDouble(s3);

for (lcv = 0; lcv <= 65; lcv++)

{
startsalary = (startsalary * 1.05);

}

System.out.println (age + "\t" +s1+ "\t" +startsalary);
System.out.println (startsalary);
}
}

I am just assuming it would work really. *shrug*
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: Krueger81
Originally posted by: Evadman
Originally posted by: Krueger81
that gives me an answer of 21000
You were outputting the endsalery, not the startsalery. I changed the code. Try my edit.

here is the actual problem maybe that will help.

" Write a program to estimate how much a young worker will make before retiring at age 65. Request the worker?s name, age, and starting salary as input. Assume the worker receives a 5 percent raise each year. For example, if the user enters Helen, 25, and 20000, then the following should be displayed on your screen:
Helen will earn about $2415995."

Phil

You need a variable that keeps track of earnings.

Add the startsalary to earnings, then multiply startsalary by 1.05.
 

Krueger81

Diamond Member
Feb 4, 2000
4,196
3
81
ok this is confusing.

should it not be:

yr1 + 5% = end

end +yr2 +5% =end2 and so on

quote me if I am wrong.
 

Krueger81

Diamond Member
Feb 4, 2000
4,196
3
81
Originally posted by: BigJ
Originally posted by: Krueger81
Originally posted by: Evadman
Originally posted by: Krueger81
that gives me an answer of 21000
You were outputting the endsalery, not the startsalery. I changed the code. Try my edit.

here is the actual problem maybe that will help.

" Write a program to estimate how much a young worker will make before retiring at age 65. Request the worker?s name, age, and starting salary as input. Assume the worker receives a 5 percent raise each year. For example, if the user enters Helen, 25, and 20000, then the following should be displayed on your screen:
Helen will earn about $2415995."

Phil

You need a variable that keeps track of earnings.

Add the startsalary to earnings, then multiply startsalary by 1.05.

for (lcv = age; lcv <= 65; lcv++)

{
earnings = salary + (salary * .05);
salary = earnings + (salary + (salary * .05));

}

System.out.println (age + "\t" +s1+ "\t" +salary);
System.out.println (earnings);


will this do???

 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Originally posted by: Krueger81
ok this is confusing.

should it not be:

yr1 + 5% = end

end +yr2 +5% =end2 and so on

quote me if I am wrong.

Math would be like this.

Year 1 = 20000
Year 2 = 20000 * 1.05 (or 21000)
year 3 = 21000 * 1.05 (or 22050)
Year 4 = 22050 * 1.05 (or 23152.5)
Year 5 = 23152.5 * 1.05 (or 24310.125)
etc.

So the math is correct.

Your code probelm is with this:

endsalary = (startsalary *= 1.05)

If that is in a loop, you are continualy setting endsalary to startsalary *= 1.05

You need to add the endsalary again, or you will always get 21000.

Put a printout in the loop, and you will see what I mean.

for (lcv = 0; lcv <= 65; lcv++)

{
endsalary = (startsalary *= 1.05);
System.out.println (endsalary);
}

yours will output
21000210002100021000210002100021000...
when the rigth answer would be
210002205023152.524310.125...
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: Krueger81
Originally posted by: BigJ
Originally posted by: Krueger81
Originally posted by: Evadman
Originally posted by: Krueger81
that gives me an answer of 21000
You were outputting the endsalery, not the startsalery. I changed the code. Try my edit.

here is the actual problem maybe that will help.

" Write a program to estimate how much a young worker will make before retiring at age 65. Request the worker?s name, age, and starting salary as input. Assume the worker receives a 5 percent raise each year. For example, if the user enters Helen, 25, and 20000, then the following should be displayed on your screen:
Helen will earn about $2415995."

Phil

You need a variable that keeps track of earnings.

Add the startsalary to earnings, then multiply startsalary by 1.05.

for (lcv = age; lcv <= 65; lcv++)

{
earnings = salary + (salary * .05);
salary = earnings + (salary + (salary * .05));

}

System.out.println (age + "\t" +s1+ "\t" +salary);
System.out.println (earnings);


will this do???

No. Think about it outside of the context of code.

Helen starts working at the age of 25 at $20,000 a year. Her total earnings are $20,000 after the first year. She then gets a 5% raise. to $21,000.

After the second year, her total earnings are now $41,000. She gets a 5% raise, and now her salary is $22,050.

After the third year, her total earnings are now $63,050. She gets a 5% raise, and now her salary is $23,152.50.

Now look at your code and tell me why it's wrong.
 

Krueger81

Diamond Member
Feb 4, 2000
4,196
3
81
Originally posted by: tfinch2
What level are you? Freshman in college? Quit programming.

thank you for the kind words.. now go fvck off

for all the others thank you very much for the help
 

pcnerd37

Senior member
Sep 20, 2004
944
0
71
If you had used Scanner instead of BufferedReader, i could have helped, but I have never learned buffereedreader, I just use Scanner.
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: pcnerd37
If you had used Scanner instead of BufferedReader, i could have helped, but I have never learned buffereedreader, I just use Scanner.

His usage of Scanner or BufferedReader is irrelevant to his problems with the code.
 

JujuFish

Lifer
Feb 3, 2005
11,448
1,056
136
How about:

earnings = 20000

for loop {
salary *= 1.05
earnings += salary
}
?

Edit: Lockage.