Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › how to use if-else, do-while, casting, and various operators.
New Posts  All Forums:Forum Nav:

# how to use if-else, do-while, casting, and various operators.

(i) Create a new folder called 'assign42' under C:\Java\Lesson4. Copy HelloWorld.java from Lesson 1 into it, and rename the file to "Retirement.java". Replace the word "HelloWorld" with "Retirement". Replace the System.out line with instructions to declare variables, and display output as described below:
(ii) Declare an integer ('int')-type variable called age and initialize it with a value of 30.
(iii) Declare a 'double' type variable called 'salary' and initialize it with value of 30000.0.
(iv) Declare an 'int' type variable called 'limit' and initialize it with value of 0.
(v) Print a header as follows:
Age Salary (\$) Limit (%) Contri. (\$) Cumul. Contribution (\$)

(vi) Increment age by 1 in a loop going from age 30 to age 60 (both inclusive) (Use a do-while loop)
(vii) In each iteration of the loop:
(A) Compute a cumulative total retirement contribution.
The limit to contributions is determined by the age, as follows:
-- if age is greater than 50, limit is 40%.
-- if age is greater than 40 but less than or equal to 50, limit is 30%.
-- limit is 25 % in all other cases.
( Assume the contributions begin at age 30 and amount equal to the limit is contributed.)
(B) Increment the salary by 3 % every year
(C) Using a product of salary and limit percent, compute the current year's contribution and add to the cumulative total
(D) Check if current age is a multiple of 5, using the modulus (%) operator. If so, print out the current values of: age, salary (truncate fraction or cents.. example: print 38504 if value is 38504.82), limit (%), contribution and cumulative contribution. Don't worry about the columnar alignment with headers.)
(viii) Declare any other variables and intiailize them as necessary.
(ix) For your reference, the output would look like:

Age Salary (\$) Limit (%) Contribution (\$) Cumul. Contri. (\$)
30 30000 25 7500 7500
35 34778 25 8694 48513
40 40317 25 10079 96058
45 46739 30 14021 162200
50 54183 30 16255 238876
55 62813 40 25125 357395
60 72817 40 29127 494790

I have done so far..

public class Retirement {

public static void main(String[] args) {
int age =30;
double salary=30000.0;
int limit =0;
int temp;

System.out.println("
");
System.out.println(" Age Salary(\$) Limit(%) Contri.(\$) Cumul.Contribustion(\$) ");
System.out.println("
");

do
{
if(age>50)
{
limit=40;
}
else if(age>40 && age<=50)
{
limit=30;
}
else{
limit=25;
}
temp = age*((int)salary*3/100);
int cont = (temp * limit)/100;
System.out.println(age + " "+ temp + " " + limit + " "+cont);
age+=5;

}while(age >30 && age<=60);
}

}
Use the code tags when posting code
highlight your code and press the code tags button

This is what you have so far.
Code:
``````public class Retirement {

public static void main(String[] args) {
int age =30;
double salary=30000.0;
int limit =0;

//int temp;
// renaming to contribution just for read ability
double contribution;

// you also need one more variable for the total contributions

System.out.println("");

// note you are printing 5 columns age, salary, limit, contributions and total contributions
System.out.println(" Age Salary(\$) Limit(%) Contri.(\$) Cumul.Contribustion(\$) ");

System.out.println("");

do
{
if(age>50){
limit=40;
}

else if(age>40 && age<=50{
limit=30;
}

else{
limit=25;
}

// (C) Using a product of salary and limit percent, compute the current year's contribution and add to the cumulative total
// contribution is your salary times the limit as a percent
contribution =
total +=

// note you will be printing 5 columns age, salary, limit, contributions and total contributions
// and it would be a good idea to cast the doubles as ints here.
// modulus (%) math http://www.javaranch.com/drive/modulo.html
// (D) Check if current age is a multiple of 5, using the modulus (%) operator.
// If so, print out the current values of:
// age,
// salary (truncate fraction or cents.. example: print 38504 if value is 38504.82),
// limit (%),
// contribution (truncate fraction or cents) and
// cumulative contribution. (truncate fraction or cents)
System.out.println(age + " "+ temp + " " + limit + " "+cont); // fix this

// increase the salary by 3%
// (B) Increment the salary by 3 % every year

// age increases by 1 not 5
age+=5; // fix this

// you don't care if age is greater then 30, your counting up and age is already 30 + 1 by the end of the first loop
} while(age >30 && age<=60); // fix this
}

}```
```
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840