473,624 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do i calculate a total for all employees pay?

1 New Member
Hi all,
i seem to have gotten stuck on this coursework, i am unsure as to how to implement a method in the main class to calculate the sum of all employees monthly salaries.
Everything else works on the program it does output the employees details and their monthly salaries. I just need to add them up and output a "Total Payroll: xxxxxx.x " in this format. It sounds simple and it probably is simple, i think i can't see the woods for trees on this at the moment and just get confused, any help will be greatly appreciated.


Expand|Select|Wrap|Line Numbers
  1.  
  2.  /*Class Main.java
  3.  *
  4.  */
  5. import Employees.Employee;
  6. import Employees.StaffList;
  7. import Employees.FullTimeEmployees;
  8. import Employees.PartTimeEmployees;
  9. import Employees.CommisionedEmployees;
  10.  
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author 
  16.  */
  17. public class Main
  18. {
  19.  
  20.     /**
  21.      *
  22.      * @param args
  23.      */
  24.     public static void main(String[] args)
  25.     {
  26.       Employee[]  staff = new Employee[9];
  27.  
  28.       staff[0] =  new FullTimeEmployees("STUART Charles",1002);
  29.       staff[1] =  new PartTimeEmployees("MARPLE Jane",1005,10.5);
  30.       staff[2] =  new CommisionedEmployees("SEYMOUR Thomas",1006,6500.0);
  31.       staff[3] =  new FullTimeEmployees("AYDELJI Paul",1012);
  32.       staff[4] =  new PartTimeEmployees("BROCK Sarah",1001,30.0);
  33.       staff[5] =  new CommisionedEmployees("ALHAMBRA Alia",1003,3450.0);
  34.       staff[6] =  new PartTimeEmployees("WHARTON Edith",1104,20.0);
  35.       staff[7] =  new FullTimeEmployees("TURNER George",1111);
  36.       staff[8] =  new CommisionedEmployees("BERNERS-LEE Tim",1014,7800.0);
  37.  
  38.       printStaffList( staff);
  39.  
  40.  
  41.     }
  42.  
  43.       static void printStaffList(Employee [] s)
  44.        {
  45.          for ( int i =0; i < 9; i++)
  46.            {
  47.            s[i].calculatePay() ;   //  THIS LINES COMPUTES PAY
  48.                System.out.println( s[i].toString());
  49.            }
  50.        }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.      private Main()
  57.      {
  58.  
  59.      }
  60.  }
  61.  
  62.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*class Employee
  3.  */
  4.  
  5. package Employees;
  6.  
  7.  
  8. /**
  9.  *  @author 
  10.  */
  11.  
  12. public abstract class Employee
  13. {
  14.     protected String name;    //Fields-containers that hold a value
  15.     protected double hrsWorked;
  16.     protected int empID;
  17.     protected static int lastEmpID = 1000;
  18.     protected double sales;
  19.     protected double hours;
  20.     protected double pay;
  21.     private double TotalPayD;
  22.  
  23.  
  24.     /**
  25.      *
  26.      */
  27.     public Employee()
  28.     {
  29.      name = "";
  30.      hrsWorked = 0.0;
  31.      empID = ++lastEmpID ;
  32.      sales = 0.0;
  33.      hours = 40.00;
  34.      pay = 0.0;
  35.      TotalPayD = 0.0;
  36.  
  37.     }
  38.  
  39.     public Employee(String nm, double hrs, double p)
  40.    {
  41.      name =nm;
  42.      hours=hrs;
  43.      pay= p;
  44.      empID = ++lastEmpID ;
  45.    }
  46.  
  47.     public abstract void calculatePay();
  48.  
  49.     public double setHoursWorked( double hoursWorked)
  50.     {
  51.       return hoursWorked;
  52.     }
  53.  
  54.  
  55.      public double getHoursWorked( double hoursWorked)
  56.     {
  57.      return hoursWorked;
  58.     }
  59.  
  60.  
  61.  
  62.     @Override
  63.  
  64.     public String toString ()
  65.     {
  66.       return (" \n Name:  " + name +
  67.               " \n WorksID: " + empID +
  68.               " \n Sales: " + sales +
  69.               " \n Hours: " + hours +
  70.               " \n Pay:  " + pay +"");
  71.     }
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
Expand|Select|Wrap|Line Numbers
  1. /*class Employee
  2.  */
  3.  
  4. package Employees;
  5.  
  6.  
  7. /**
  8.  *  @author 
  9.  */
  10.  
  11. *class FullTimeEmployees
  12.  *
  13.  */
  14. package Employees;
  15.  
  16. /**
  17.  * @author 
  18.  */
  19.  
  20. public class FullTimeEmployees extends Employee
  21. {
  22.  
  23.     double hrsWorked = 40.0;  //  ALL Full-Time staff work for 40 hours
  24.  
  25.     /**
  26.      * 
  27.      * @param nm
  28.      * @param id 
  29.      */
  30.     public FullTimeEmployees(String nm, int id)
  31.     {
  32.         name = nm ;
  33.         hours = hrsWorked ;  // i.e. hours = 40.0 
  34.     empID = id ;
  35.     }
  36.  
  37.  
  38.  
  39.     @Override
  40.     public void calculatePay()
  41.     {
  42.         pay = hrsWorked * 2000.0 / 40.0 ;
  43.     }
  44.  
  45. }
  46.  
  47.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*class PartTimeEmployees
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package Employees;
  8.  
  9. /**
  10.  *
  11.  * @author 
  12.  */
  13. public class PartTimeEmployees extends Employee
  14. {
  15.  
  16.  
  17.     /**
  18.      *
  19.      * @param nm
  20.      * @param id
  21.      * @param hr
  22.      */
  23.     public PartTimeEmployees(String nm, int id, double hr)
  24.     {
  25.        name = nm ;
  26.        empID = id ;
  27.        hours = hr ;
  28.     }
  29.  
  30.     @Override
  31.     public void calculatePay()
  32.     {
  33.        pay = hours * 2000.0 / 40.0 ;
  34.     }
  35.  
  36. }
  37.  
  38.  
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. /*class CommisionedEmployees
  4.  */
  5.  
  6. package Employees;
  7.  
  8. /**
  9.  *
  10.  * @author 
  11.  */
  12. public class CommisionedEmployees extends Employee
  13. {
  14.     private static double commRate = 10.0 ;
  15.     private static double flatAmount = 1200.0 ;
  16.  
  17.     public CommisionedEmployees(String nm, int id, double s)
  18.     {
  19.        name = nm ;
  20.        empID = id ;
  21.        sales = s ;
  22.     }
  23.  
  24.     @Override
  25.     public void calculatePay()
  26.     {
  27.         pay = flatAmount + sales * (commRate / 100.0) ;
  28.     }
  29.  
  30. }
  31.  
  32.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*class StaffList
  3.  *
  4.  */
  5.  
  6. package Employees;
  7.  
  8. import java.util.Collections;
  9.  
  10.  
  11.  
  12.  
  13. public class StaffList 
  14. {
  15.  
  16.  
  17.     public StaffList(String string)
  18.     {
  19.  
  20.     }
  21.  
  22.  
  23.     public void setStaffList()
  24.      {
  25.  
  26.      }
  27.  
  28.     public void getStaffList()
  29.      {
  30.  
  31.      }
  32.  
  33.  
  34.     public void calculatePay()
  35.     {
  36.  
  37.     }
  38.  
  39.     public void printStaffList()
  40.     {
  41.       collections.sort();
  42.     }
  43.      //Collections.sort();
  44.  
  45. }
  46.  
  47.  
Jan 6 '10 #1
3 8407
Frinavale
9,735 Recognized Expert Moderator Expert
I'm not entirely sure what happens to class level variables that have no scope modifiers (like public or private) in Java.

If the "pay" variable is public (accessible to the calling code), then for each employee use the pay variable to tally up your sum.

If the "pay" variable is not accessible to the calling code (is private), then either write a method that exposes the pay variable to the calling code or have the "calculateP ay" method return the pay so that you can use it retrieve the pay for each employee and tally up your sum.

-Frinny
Jan 6 '10 #2
RedSon
5,000 Recognized Expert Expert
I think it defaults to "protected" . In any event you should have get() and set() unless you plan on deriving other classes from these classes.
Jan 6 '10 #3
pbrockway2
151 Recognized Expert New Member
The default rights allow access from within the same package (like protected does) but do not allow access from subclasses in other packages. It is sometimes known as "package-private". See the table in Sun's Tutorial in the chapter Controlling Access to Members of a Class.

As others have said provide a method by which the pay for an employee is made available: probably make calculatePay() return a double. Then the printStaffList( ) can use the standard idiom for accumulating a result:

Expand|Select|Wrap|Line Numbers
  1. double sum = 0;
  2. for(/*etc*/)
  3. {
  4.     // other stuff like printing names etc
  5.     sum += ...; // get the pay and add it
  6. }
  7. // at this point sum will be the total pay
  8.  
Post again if you get stuck.
Jan 6 '10 #4

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

Similar topics

1
9981
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a number. That number will appear in a seperate box at the bottom. So basically whatever you choose has a corresponding number associated with it (except for the text input, which you enter whatever number) and those numbers are added and produced in...
53
5688
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
7
2821
by: rick | last post by:
Can anyone help, I am try to create a simple form using a table, where a user can fill out quanty and price and have a total automatically calculated and inserted in another field. I stuck trying to figure out how expand this script to recalculate when rows are added or removed. My code so far. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
7
4325
by: Jurek | last post by:
I have 10+ experience in C/C++ - mostly automation and graphics. I have never written any business apps though. Recently I've been asked to write a simple report that would calculate sales commission. The report needs to be generated from within a C/C++ app. I don't want to mess it up so I thought maybe someone from this group could give me some advice or point to me to a place that'll provide some background on the issue. Here is a...
4
6363
by: Rich_C | last post by:
I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost (cost). Users can dynamically add rows to the table so I don't know how many rows might need to be calculated. I need to calculate the total (qty * cost) and put that number in a table cell (or read only input box). I also need to sum the...
1
4125
by: jaswmil | last post by:
I have a query (see SQL below) that essentially grabs a list of all employees terminated during a specific period. What I am needing to do is to be able to take this number and then divide it by the CURRENT count of active employees. The problem that I cannot figure out, is how do I do this by department. Of course all of our departments want to be able to see their own specific turnover rate. I can figure out how to do this on a...
3
11789
by: gator6688 | last post by:
I have to write a program that asks for a cost-per-item, number of items purchased, and a discount rate. Then it should calculate the total cost, tax due, and amount due. I have to use the formulas: total cost = number of items * cost-per-item total cost (discounted) = total cost - (discount rate * total cost) tax due = total cost * TAXRATE amount due = total cost + tax due I am going to put what I have so far. Any suggestions would be...
4
2771
by: trogenone | last post by:
Hi i have already asked what to use for calculating an age and that works fine. The problem is it dosnt leave a value in the desired table. Tables: Employees & Holidays I asked for a formula that would calculate time served with the company and to give a number in years. This part works except the value isnt shown in the table of employees it just stays blank? What can i do to solve this as the second part of the equasion is for the years...
4
11200
by: shilpareddy2787 | last post by:
Hello, I have some total values, I want to calculate percenatge of these Total Values. I want to divide the total with No. Of working Days Excluding Saturdays and Sundays in a given period. How to calculate the Total Number of working Days in a given period . Let us say If i give the period as 08/01/2008 to 08/15/2008, I want total number of working days as 11. Please help me
0
8238
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7164
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.