 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
projnaza Guest
|
Posted: Sun Feb 20, 2005 3:25 am Post subject: Need help on java project pls |
|
|
I'm a total greenhorn in this language and my lecturer threw me an
assignment to do recently. It seems like I have to create an
interactive Salary Computation program that calculates the gross
salary, net salary for a set of employees (a loop is needed for this
function) and prints the results.
There are 2 types of Employees : 1 and 2.
Type 1 employees calculate their gross pay as regular pay for hours up
to 40 and one and a half time the regular rate for hours over 40. Type
2 employees calculate their gross pay as (rate*hours+200.0)
Taxes are calculated as 20% if the gross pay is less than or equal to
Euro$500.0 and 30% if the gross is over $500.0. Net pay is gross pay
minus taxes.
The program is to run in a loop. Each time it asks for the following:
1.Type of employee: 1 or 2
2.Employee Name
3.Hours worked/week: any positive number
4.Hourly rate: any positive number
The program then computes and prints out the employee type, name, gross
pay, income tax withheld and the net pay.
the loop is terminated by typing 0 in place of a valid employee type
when no more employees are to be processed.
the program then prints a summary including the total number of
employees processed, average gross salary of an employee and the
average net salary of an employee.
Here's what i've come up with so far...it's terribly wrong though :
import javax.swing.JOptionPane;
public class Salary {
public static void main(String args[])
{
int type = 1 || 2;
double wages;
// input employee type
String group = JOptionPane.showInputDialog(null, "Enter employee type
(1 or 2 only)", "Computation of Salary", JOptionPane.QUESTION_MESSAGE);
int intNumber=Integer.parseInt(group);
if (type == 1) {
if (hours <= 40) {
wages = rate*hours;
else (hours >40)
wages = rate*1.5*hours; }
}
else if (type == 2) {
wages = rate*hours + 200.0;
}
else if (type ==0) {
System.exit(0);
// input Employee Name
String name = JOptionPane.showInputDialog(null, "Enter employee
name", "Computation of Salary", JOptionPane.QUESTION_MESSAGE);
// convert string to
// input
// input Number of hours worked
String doublehours = JOptionPane.showInputDialog(null, "Enter Number
of hours worked", "Computation of Salary",
JOptionPane.QUESTION_MESSAGE);
// convert string into double
double doubleHours = Double.parseDouble(hours);
// input hourly rate
String doublerate = JOptionPane.showInputDialog(null, "Enter Number
of hours worked", "Computation of Salary",
JOptionPane.QUESTION_MESSAGE);
// convert string into double
double doubleRate = Double.parseDouble(rate);
// Display results
String output = "The employee is" +name+ "nOf employee type" +type+
"nGross pay" +Gross pay+ "nIncome Tax withheld" +IncomeTaxWithheld+
"nNet Pay" +NetPay;
JoptionPane.showMessageDialog(null,output,"Summary of the computation
of the salary of the employee just entered",
JOptionPane.INFORMATION_MESSAGE);
}
}
Much help is appreciated...thanx a lot
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Sun Feb 20, 2005 9:00 am Post subject: Re: Need help on java project pls |
|
|
"projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote
| Quote: |
I'm a total greenhorn in this language and my lecturer threw
me an assignment to do recently. It seems like I have to
create an interactive Salary Computation program that
calculates the gross salary, net salary for a set of employees
(a loop is needed for this function) and prints the results.
There are 2 types of Employees : 1 and 2.
Type 1 employees calculate their gross pay as regular pay
for hours up to 40 and one and a half time the regular rate
for hours over 40. Type 2 employees calculate their gross
pay as (rate*hours+200.0)
Taxes are calculated as 20% if the gross pay is less than
or equal to Euro$500.0 and 30% if the gross is over
$500.0. Net pay is gross pay minus taxes.
The program is to run in a loop. Each time it asks for the
following:
1.Type of employee: 1 or 2
2.Employee Name
3.Hours worked/week: any positive number
4.Hourly rate: any positive number
The program then computes and prints out the employee
type, name, gross pay, income tax withheld and the net pay.
the loop is terminated by typing 0 in place of a valid
employee type when no more employees are to be
processed.
the program then prints a summary including the total
number of employees processed, average gross salary of
an employee and the average net salary of an employee.
SNIP
|
Based on the code you've posted it appears that you:
* Are not required to package program functionailty into
separate methods
* Use the 'JOptionPane.showXXXDialog' for performing
interactive I/O
Assuming this is the case, then you appear to be on the right track, though
you've incorrectly declared a couple of variables, and did not encase your
code within a loop.
Below you will find a tidied up [executable, but incomplete] version of your
code which rectifies these problems. It should be enough to enable you to
continue with your project.
I hope this helps.
Anthony Borla
// -----------------------------------------
import javax.swing.JOptionPane;
public class Salary
{
public static void main(String args[])
{
// Commence loop ...
do
{
// input employee type
String typeStr = JOptionPane.showInputDialog(null,
"Enter employee type (1 or 2 only - 0 to exit)",
"Computation of Salary", JOptionPane.QUESTION_MESSAGE);
int type = Integer.parseInt(typeStr);
// Check whether to exit program
if (type == 0)
break;
// input Employee Name
String name = JOptionPane.showInputDialog(null,
"Enter employee name",
"Computation of Salary", JOptionPane.QUESTION_MESSAGE);
// convert string to
// input
// input Number of hours worked
String hoursStr = JOptionPane.showInputDialog(null,
"Enter Number of hours worked",
"Computation of Salary", JOptionPane.QUESTION_MESSAGE);
// convert string into double
double hours = Double.parseDouble(hoursStr);
// input hourly rate
String rateStr = JOptionPane.showInputDialog(null,
"Enter Rate", "Computation of Salary",
JOptionPane.QUESTION_MESSAGE);
// convert string into double
double rate = Double.parseDouble(rateStr);
double grossPay = 0.0;
if (type == 1)
{
if (hours <= 40)
grossPay = rate * hours;
else
grossPay = rate * 1.5 * hours;
}
else if (type == 2)
{
grossPay = rate * hours + 200.0;
}
else
{
// Invalid type ... display error message or something ..
;
}
// Compute net pay ...
double incomeTaxWithheld = 0.0;
double netPay = 0.0;
// Display results
String output = "The employee is" + name + "nOf employee type"
+ type + "nGross pay" + grossPay + "nIncome Tax withheld"
+ incomeTaxWithheld + "nNet Pay" + netPay;
JOptionPane.showMessageDialog(null,output,
"Summary of the computation of the salary of the employee",
JOptionPane.INFORMATION_MESSAGE);
} while (true);
System.exit(0);
}
}
|
|
| Back to top |
|
 |
Big Jim Guest
|
Posted: Sun Feb 20, 2005 8:27 pm Post subject: Re: Need help on java project pls |
|
|
sounds to me like your lecturer is posing you a problem in inheritance i.e.
he wants an employee class subclassed by employee 1 and employee 2 classes,
are you sure your approach is on the right track? did he mention inheritance
in recent lectures?
"projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote
| Quote: | I'm a total greenhorn in this language and my lecturer threw me an
assignment to do recently. It seems like I have to create an
interactive Salary Computation program that calculates the gross
salary, net salary for a set of employees (a loop is needed for this
function) and prints the results.
There are 2 types of Employees : 1 and 2.
Type 1 employees calculate their gross pay as regular pay for hours up
to 40 and one and a half time the regular rate for hours over 40. Type
2 employees calculate their gross pay as (rate*hours+200.0)
Taxes are calculated as 20% if the gross pay is less than or equal to
Euro$500.0 and 30% if the gross is over $500.0. Net pay is gross pay
minus taxes.
The program is to run in a loop. Each time it asks for the following:
1.Type of employee: 1 or 2
2.Employee Name
3.Hours worked/week: any positive number
4.Hourly rate: any positive number
The program then computes and prints out the employee type, name, gross
pay, income tax withheld and the net pay.
the loop is terminated by typing 0 in place of a valid employee type
when no more employees are to be processed.
the program then prints a summary including the total number of
employees processed, average gross salary of an employee and the
average net salary of an employee.
Here's what i've come up with so far...it's terribly wrong though :
import javax.swing.JOptionPane;
public class Salary {
public static void main(String args[])
{
int type = 1 || 2;
double wages;
// input employee type
String group = JOptionPane.showInputDialog(null, "Enter employee type
(1 or 2 only)", "Computation of Salary", JOptionPane.QUESTION_MESSAGE);
int intNumber=Integer.parseInt(group);
if (type == 1) {
if (hours <= 40) {
wages = rate*hours;
else (hours >40)
wages = rate*1.5*hours; }
}
else if (type == 2) {
wages = rate*hours + 200.0;
}
else if (type ==0) {
System.exit(0);
// input Employee Name
String name = JOptionPane.showInputDialog(null, "Enter employee
name", "Computation of Salary", JOptionPane.QUESTION_MESSAGE);
// convert string to
// input
// input Number of hours worked
String doublehours = JOptionPane.showInputDialog(null, "Enter Number
of hours worked", "Computation of Salary",
JOptionPane.QUESTION_MESSAGE);
// convert string into double
double doubleHours = Double.parseDouble(hours);
// input hourly rate
String doublerate = JOptionPane.showInputDialog(null, "Enter Number
of hours worked", "Computation of Salary",
JOptionPane.QUESTION_MESSAGE);
// convert string into double
double doubleRate = Double.parseDouble(rate);
// Display results
String output = "The employee is" +name+ "nOf employee type" +type+
"nGross pay" +Gross pay+ "nIncome Tax withheld" +IncomeTaxWithheld+
"nNet Pay" +NetPay;
JoptionPane.showMessageDialog(null,output,"Summary of the computation
of the salary of the employee just entered",
JOptionPane.INFORMATION_MESSAGE);
}
}
Much help is appreciated...thanx a lot
|
|
|
| Back to top |
|
 |
projnaza Guest
|
Posted: Sun Feb 20, 2005 11:43 pm Post subject: Re: Need help on java project pls |
|
|
"Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
| Quote: | "projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote in message
news:1108869902.799881.311300 (AT) z14g2000cwz (DOT) googlegroups.com...
I'm a total greenhorn in this language and my lecturer threw
me an assignment to do recently. It seems like I have to
create an interactive Salary Computation program that
calculates the gross salary, net salary for a set of employees
(a loop is needed for this function) and prints the results.
There are 2 types of Employees : 1 and 2.
Type 1 employees calculate their gross pay as regular pay
for hours up to 40 and one and a half time the regular rate
for hours over 40. Type 2 employees calculate their gross
pay as (rate*hours+200.0)
Taxes are calculated as 20% if the gross pay is less than
or equal to Euro$500.0 and 30% if the gross is over
$500.0. Net pay is gross pay minus taxes.
The program is to run in a loop. Each time it asks for the
following:
1.Type of employee: 1 or 2
2.Employee Name
3.Hours worked/week: any positive number
4.Hourly rate: any positive number
The program then computes and prints out the employee
type, name, gross pay, income tax withheld and the net pay.
the loop is terminated by typing 0 in place of a valid
employee type when no more employees are to be
processed.
the program then prints a summary including the total
number of employees processed, average gross salary of
an employee and the average net salary of an employee.
SNIP
Based on the code you've posted it appears that you:
* Are not required to package program functionailty into
separate methods
* Use the 'JOptionPane.showXXXDialog' for performing
interactive I/O
Assuming this is the case, then you appear to be on the right track, though
you've incorrectly declared a couple of variables, and did not encase your
code within a loop.
Below you will find a tidied up [executable, but incomplete] version of your
code which rectifies these problems. It should be enough to enable you to
continue with your project.
I hope this helps.
Anthony Borla
// -----------------------------------------
import javax.swing.JOptionPane;
public class Salary
{
public static void main(String args[])
{
// Commence loop ...
do
{
// input employee type
String typeStr = JOptionPane.showInputDialog(null,
"Enter employee type (1 or 2 only - 0 to exit)",
"Computation of Salary", JOptionPane.QUESTION_MESSAGE);
int type = Integer.parseInt(typeStr);
// Check whether to exit program
if (type == 0)
break;
// input Employee Name
String name = JOptionPane.showInputDialog(null,
"Enter employee name",
"Computation of Salary", JOptionPane.QUESTION_MESSAGE);
// convert string to
// input
// input Number of hours worked
String hoursStr = JOptionPane.showInputDialog(null,
"Enter Number of hours worked",
"Computation of Salary", JOptionPane.QUESTION_MESSAGE);
// convert string into double
double hours = Double.parseDouble(hoursStr);
// input hourly rate
String rateStr = JOptionPane.showInputDialog(null,
"Enter Rate", "Computation of Salary",
JOptionPane.QUESTION_MESSAGE);
// convert string into double
double rate = Double.parseDouble(rateStr);
double grossPay = 0.0;
if (type == 1)
{
if (hours <= 40)
grossPay = rate * hours;
else
grossPay = rate * 1.5 * hours;
}
else if (type == 2)
{
grossPay = rate * hours + 200.0;
}
else
{
// Invalid type ... display error message or something ..
;
}
// Compute net pay ...
double incomeTaxWithheld = 0.0;
double netPay = 0.0;
// Display results
String output = "The employee is" + name + "nOf employee type"
+ type + "nGross pay" + grossPay + "nIncome Tax withheld"
+ incomeTaxWithheld + "nNet Pay" + netPay;
JOptionPane.showMessageDialog(null,output,
"Summary of the computation of the salary of the employee",
JOptionPane.INFORMATION_MESSAGE);
} while (true);
System.exit(0);
}
}
|
Thanks for the help. One qn though: how do I write the program codes
that calculates the summary of the wages of the number of employees
entered? The other half of the qn asks for:
the program then prints a summary including the total
| Quote: | number of employees processed, average gross salary of
an employee and the average net salary of an employee.
|
|
|
| Back to top |
|
 |
projnaza Guest
|
Posted: Sun Feb 20, 2005 11:47 pm Post subject: Re: Need help on java project pls |
|
|
Hmm no, at the time when this assignment was given out, my lecturer
hadn't talked about inheritance
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Mon Feb 21, 2005 2:14 am Post subject: Re: Need help on java project pls |
|
|
"projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote
| Quote: | "Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
"projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote in message
news:1108869902.799881.311300 (AT) z14g2000cwz (DOT) googlegroups.com...
SNIP
Thanks for the help. One qn though: how do I write the
program codes that calculates the summary of the wages
of the number of employees entered? The other half of the
qn asks for:
the program then prints a summary including the total
number of employees processed, average gross salary of
an employee and the average net salary of an employee.
|
Sorry, you're on your own for the rest of the project since it is, after
all, *your* project, and the main reason for completing it [aside from
helping to pass your course !] is for you to learn Java.
The only hints I'll give:
* If processing things a number of times in the loop then
you need additional variables that will be updated
with totals, and counts etc as you repeat the loop
* The collected totals and counts may be used to compute
things like averages etc *outside the loop*, near the end
of the program, at which time they would also be displayed
I'm sure this is *more* than enough for you to proceed. The best of luck to
you !
Cheers,
Anthony Borla
|
|
| Back to top |
|
 |
projnaza Guest
|
Posted: Tue Feb 22, 2005 2:56 am Post subject: Re: Need help on java project pls |
|
|
"Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
| Quote: | "projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote in message
news:bc94c729.0502201543.2f9d26f3 (AT) posting (DOT) google.com...
"Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote in message
news:<nkYRd.168337$K7.83568 (AT) news-server (DOT) bigpond.net.au>...
"projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote in message
news:1108869902.799881.311300 (AT) z14g2000cwz (DOT) googlegroups.com...
SNIP
Thanks for the help. One qn though: how do I write the
program codes that calculates the summary of the wages
of the number of employees entered? The other half of the
qn asks for:
the program then prints a summary including the total
number of employees processed, average gross salary of
an employee and the average net salary of an employee.
Sorry, you're on your own for the rest of the project since it is, after
all, *your* project, and the main reason for completing it [aside from
helping to pass your course !] is for you to learn Java.
The only hints I'll give:
* If processing things a number of times in the loop then
you need additional variables that will be updated
with totals, and counts etc as you repeat the loop
* The collected totals and counts may be used to compute
things like averages etc *outside the loop*, near the end
of the program, at which time they would also be displayed
I'm sure this is *more* than enough for you to proceed. The best of luck to
you !
Cheers,
Anthony Borla
|
Last few qns: I tried it out but here are the errors
the compiler said 'while' was expected in the part after int type and
identifiers are expected...how do i debug these problems? thanks
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Tue Feb 22, 2005 3:22 am Post subject: Re: Need help on java project pls |
|
|
"projnaza" <amelia_it_ji (AT) yahoo (DOT) co.uk> wrote
| Quote: | "Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
SNIP
Thanks for the help. One qn though: how do I write the
program codes that calculates the summary of the wages
of the number of employees entered? The other half of the
qn asks for:
the program then prints a summary including the total
number of employees processed, average gross salary of
an employee and the average net salary of an employee.
SNIP
Last few qns: I tried it out but here are the errors
the compiler said 'while' was expected in the part after
int type and identifiers are expected...how do i debug
these problems? thanks
|
It sounds like you may not have correctly cut'n'pasted the code I sent - it
compiled cleanly, and executed perfectly on my system. Try to do so again,
and ensure that all the braces match, that is, for every:
{
there is a corresponding:
}
As for debugging problems all I can suggest is that you first read through
your code trying to understand it, and pinpoint the problem area. Things
like placing a few 'System.out.println' calls may help in determining
whether values are being set as expected.
It really boils down to spending time with your code and trying different
things to see what effect they have.
I hope this helps.
Anthony Borla
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|