473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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

✓ answered by r035198x

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.

6 9570
r035198x
13,262 8TB
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
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 8TB
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
Thanks for the help...i get it arldy tq...
Nov 19 '13 #5
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
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
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
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
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
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
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
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
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
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
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.