473,480 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Salary calculation for employee: How to get the correct output ?

7 New Member
Expand|Select|Wrap|Line Numbers
  1. public class Employee{
  2.       String name;
  3.       double salary;
  4.  
  5.        // This is the constructor of the class Employee
  6.        public Employee(String EmpName)
  7.        {
  8.           name = EmpName;
  9.        }
  10.  
  11.        // Calculate weekly pay
  12.        public double weeklyPay(double hrsWorked, double hlyPay)
  13.        {
  14.           double total; 
  15.           if (hrsWorked <= 40)
  16.              total = hrsWorked * hlyPay;
  17.           else
  18.              total = (40 * hlyPay) + ((hrsWorked - 40)*1.5*hlyPay);
  19.           return total; 
  20.        }
  21.  
  22.        //Monthly salary is calculated by multiplying weeklyPay with 4
  23.        public void monthlyPay(double weeklyPay)
  24.        {
  25.            salary = weeklyPay * 4;
  26.        }
  27.  
  28.        /* Print the Employee details */
  29.        public void printEmployee()
  30.        {
  31.            System.out.println(name + "' salary for a month is " + salary);
  32.        }
  33.     }
  34.  
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class EmployeeTest{
  3.  
  4.    public static void main(String args[]){
  5.  
  6.        // Invoking methods for each object created
  7.       final double hourlyPay = 52.50;
  8.       double hWorked, wPay; 
  9.       Scanner input = new Scanner(System.in);
  10.       System.out.print("Please enter the number of hours work: ");
  11.       hWorked = input.nextDouble();
  12.  
  13.       /* Create employee objects using constructor */
  14.       Employee emp = new Employee ("Mike");
  15.  
  16.       /* invoke weeklyPay() method */
  17.       emp.weeklyPay(hourlyPay, hWorked);
  18.  
  19.       /* invoke monthlyPay() method*/
  20.       emp.monthlyPay(weeklyPay);
  21.  
  22.       /* invoke printEmployee() method*/
  23.       emp.printEmployee();
  24.    }
  25. }
  26.  
Output:
Please enter the number of hours work: 44
Ahmad salary is 0.0

(My output should be 9660.0)

Any help and response will be appreciate.

Regards!
Nov 18 '13 #1
6 9577
r035198x
13,262 MVP
You are not using the result of the weeklyPay method in your calculation of the monthly pay.
Did you instead mean

Expand|Select|Wrap|Line Numbers
  1. double weeklyPay = emp.weeklyPay();
then
Expand|Select|Wrap|Line Numbers
  1. emp.monthlyPay(weeklyPay);
?
Also, your weeklyPay method in the Employee class takes two arguments of type double but you are calling it without passing any parameters? Are you sure you posted the code that you ran?
Nov 19 '13 #2
raitoHiong
7 New Member
Very Sorry, i am still a newbie in Java. Is it correct if i write like this to use the result?
Expand|Select|Wrap|Line Numbers
  1. emp.weeklyPay(hWorked, hourlyPay);
The result of weeklyPay multiplying by 4 write like this is it correct ?
Expand|Select|Wrap|Line Numbers
  1.  //Monthly salary is calculated by multiplying weeklyPay with 4
  2.        public void monthlyPay(double weeklyPay)
  3.        {
  4.            salary = weeklyPay * 4;
  5.        }
i am confuse to invoke monthlyPay() method
Nov 19 '13 #3
r035198x
13,262 MVP
You are not assigning the value that was calculated in the weeklyPay method to any variable. So it is just being lost. Assign it to a variable with

Expand|Select|Wrap|Line Numbers
  1. double weeklyPay = emp.weeklyPay(hWorked, hourlyPay);
Then use that value when calling the monthlyPay method.
Nov 19 '13 #4
raitoHiong
7 New Member
Thanks for the help...i get it arldy tq...
Nov 19 '13 #5
rashmiahuja84
3 New Member
Expand|Select|Wrap|Line Numbers
  1. class Employee{
  2.       String name;
  3.       double salary;
  4.  
  5.        // This is the constructor of the class Employee
  6.        public Employee(String EmpName)
  7.        {
  8.           name = EmpName;
  9.        }
  10.  
  11.        // Calculate weekly pay
  12.        public double weeklyPay(double hrsWorked, double hlyPay)
  13.        {
  14.           double total; 
  15.           if (hrsWorked <= 40)
  16.              total = hrsWorked * hlyPay;
  17.           else
  18.              total = (40 * hlyPay) + ((hrsWorked - 40)*1.5*hlyPay);
  19.           return total; 
  20.        }
  21.  
  22.        //Monthly salary is calculated by multiplying weeklyPay with 4
  23.        public void monthlyPay(double weeklyPay)
  24.        {
  25.            salary = weeklyPay * 4;
  26.        }
  27.  
  28.        /* Print the Employee details */
  29.        public void printEmployee()
  30.        {
  31.            System.out.println(name + "' salary for a month is " + salary);
  32.        }
  33.     }
  34.  
Apr 4 '14 #6
rashmiahuja84
3 New Member
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class EmployeeTest{
  3.  
  4.    public static void main(String args[]){
  5.  
  6.        // Invoking methods for each object created
  7.       final double hourlyPay = 52.50;
  8.       double hWorked, wPay; 
  9.       double weeklyPay=0.0;
  10.       Scanner input = new Scanner(System.in);
  11.       System.out.print("Please enter the number of hours work: ");
  12.       hWorked = input.nextDouble();
  13.  
  14.       /* Create employee objects using constructor */
  15.       Employee emp = new Employee ("Mike");
  16.  
  17.       /* invoke weeklyPay() method */
  18.       weeklyPay=  emp.weeklyPay(hWorked,hourlyPay);
  19.  
  20.       /* invoke monthlyPay() method*/
  21.       emp.monthlyPay(weeklyPay);
  22.  
  23.       /* invoke printEmployee() method*/
  24.       emp.printEmployee();
  25.    }
  26. }
  27.  
  28.  
Apr 4 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4300
by: hnzhang | last post by:
int main() { char *test = "hello"; std::cout << *test << std::endl; return 0; } the result of out put is h; only the first character. But what I want
3
2088
by: pv1 | last post by:
Hello, I am somewhat new to Access and vba, but am trying to complete a request that has been made of me. Access version 2002 WindowsXP. At this point these text boxes are bound to the table...
5
8551
by: james121285 | last post by:
This is to calculate an employees tax and pension. The salary is input from the keyboard. The NI contribution is calculated as 6% of the gross salary. The pension contribution is calculated as 2%...
13
2179
by: Carramba | last post by:
Hi! I have written some peace of code, but I wonder if it's legal for ansi-c I have no problem to compiling it, but since I'm inexperience and the output is not correct I have doubts. Thank you...
2
2358
by: pks83 | last post by:
In MSSQL I am using datetime as the column type and trying to execute the below mentioned query select time from tickets where ticket_id = '1'; when i execute the query on sql manager window it...
4
1903
rajiv07
by: rajiv07 | last post by:
I have a script to calculate the warranty the period of one year.I add 365 days of current date.I got the correct output for the year 2007 but the 2008 year calculation getting incorrect value.please...
0
1218
by: Masterstill | last post by:
I've faced some problems while designing a page. the below codes look perfect on 800 * 600 browser but higher resolution is not supported. I've tried using % instead of px. but still i could not...
3
1613
by: rwgwiccan | last post by:
Have a program that is to take a persons name, hours worked, rate of pay, fed and state taxes, and combine them to get a net pay. However, most of the calculations will be done in a separate class...
4
1665
by: raitoHiong | last post by:
i want to get correct output: public class Temperature { private String myScale; //valid values are "F" or "C" private double myDegrees; double celsius; //default constructor
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.