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. -
-
/*Class Main.java
-
*
-
*/
-
import Employees.Employee;
-
import Employees.StaffList;
-
import Employees.FullTimeEmployees;
-
import Employees.PartTimeEmployees;
-
import Employees.CommisionedEmployees;
-
-
-
-
/**
-
*
-
* @author
-
*/
-
public class Main
-
{
-
-
/**
-
*
-
* @param args
-
*/
-
public static void main(String[] args)
-
{
-
Employee[] staff = new Employee[9];
-
-
staff[0] = new FullTimeEmployees("STUART Charles",1002);
-
staff[1] = new PartTimeEmployees("MARPLE Jane",1005,10.5);
-
staff[2] = new CommisionedEmployees("SEYMOUR Thomas",1006,6500.0);
-
staff[3] = new FullTimeEmployees("AYDELJI Paul",1012);
-
staff[4] = new PartTimeEmployees("BROCK Sarah",1001,30.0);
-
staff[5] = new CommisionedEmployees("ALHAMBRA Alia",1003,3450.0);
-
staff[6] = new PartTimeEmployees("WHARTON Edith",1104,20.0);
-
staff[7] = new FullTimeEmployees("TURNER George",1111);
-
staff[8] = new CommisionedEmployees("BERNERS-LEE Tim",1014,7800.0);
-
-
printStaffList( staff);
-
-
-
}
-
-
static void printStaffList(Employee [] s)
-
{
-
for ( int i =0; i < 9; i++)
-
{
-
s[i].calculatePay() ; // THIS LINES COMPUTES PAY
-
System.out.println( s[i].toString());
-
}
-
}
-
-
-
-
-
-
private Main()
-
{
-
-
}
-
}
-
-
-
-
/*class Employee
-
*/
-
-
package Employees;
-
-
-
/**
-
* @author
-
*/
-
-
public abstract class Employee
-
{
-
protected String name; //Fields-containers that hold a value
-
protected double hrsWorked;
-
protected int empID;
-
protected static int lastEmpID = 1000;
-
protected double sales;
-
protected double hours;
-
protected double pay;
-
private double TotalPayD;
-
-
-
/**
-
*
-
*/
-
public Employee()
-
{
-
name = "";
-
hrsWorked = 0.0;
-
empID = ++lastEmpID ;
-
sales = 0.0;
-
hours = 40.00;
-
pay = 0.0;
-
TotalPayD = 0.0;
-
-
}
-
-
public Employee(String nm, double hrs, double p)
-
{
-
name =nm;
-
hours=hrs;
-
pay= p;
-
empID = ++lastEmpID ;
-
}
-
-
public abstract void calculatePay();
-
-
public double setHoursWorked( double hoursWorked)
-
{
-
return hoursWorked;
-
}
-
-
-
public double getHoursWorked( double hoursWorked)
-
{
-
return hoursWorked;
-
}
-
-
-
-
@Override
-
-
public String toString ()
-
{
-
return (" \n Name: " + name +
-
" \n WorksID: " + empID +
-
" \n Sales: " + sales +
-
" \n Hours: " + hours +
-
" \n Pay: " + pay +"");
-
}
-
-
}
-
-
-
-
-
/*class Employee
-
*/
-
-
package Employees;
-
-
-
/**
-
* @author
-
*/
-
-
*class FullTimeEmployees
-
*
-
*/
-
package Employees;
-
-
/**
-
* @author
-
*/
-
-
public class FullTimeEmployees extends Employee
-
{
-
-
double hrsWorked = 40.0; // ALL Full-Time staff work for 40 hours
-
-
/**
-
*
-
* @param nm
-
* @param id
-
*/
-
public FullTimeEmployees(String nm, int id)
-
{
-
name = nm ;
-
hours = hrsWorked ; // i.e. hours = 40.0
-
empID = id ;
-
}
-
-
-
-
@Override
-
public void calculatePay()
-
{
-
pay = hrsWorked * 2000.0 / 40.0 ;
-
}
-
-
}
-
-
-
-
/*class PartTimeEmployees
-
* To change this template, choose Tools | Templates
-
* and open the template in the editor.
-
*/
-
-
package Employees;
-
-
/**
-
*
-
* @author
-
*/
-
public class PartTimeEmployees extends Employee
-
{
-
-
-
/**
-
*
-
* @param nm
-
* @param id
-
* @param hr
-
*/
-
public PartTimeEmployees(String nm, int id, double hr)
-
{
-
name = nm ;
-
empID = id ;
-
hours = hr ;
-
}
-
-
@Override
-
public void calculatePay()
-
{
-
pay = hours * 2000.0 / 40.0 ;
-
}
-
-
}
-
-
-
-
-
/*class CommisionedEmployees
-
*/
-
-
package Employees;
-
-
/**
-
*
-
* @author
-
*/
-
public class CommisionedEmployees extends Employee
-
{
-
private static double commRate = 10.0 ;
-
private static double flatAmount = 1200.0 ;
-
-
public CommisionedEmployees(String nm, int id, double s)
-
{
-
name = nm ;
-
empID = id ;
-
sales = s ;
-
}
-
-
@Override
-
public void calculatePay()
-
{
-
pay = flatAmount + sales * (commRate / 100.0) ;
-
}
-
-
}
-
-
-
-
/*class StaffList
-
*
-
*/
-
-
package Employees;
-
-
import java.util.Collections;
-
-
-
-
-
public class StaffList
-
{
-
-
-
public StaffList(String string)
-
{
-
-
}
-
-
-
public void setStaffList()
-
{
-
-
}
-
-
public void getStaffList()
-
{
-
-
}
-
-
-
public void calculatePay()
-
{
-
-
}
-
-
public void printStaffList()
-
{
-
collections.sort();
-
}
-
//Collections.sort();
-
-
}
-
-
3 8391 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 "calculatePay" method return the pay so that you can use it retrieve the pay for each employee and tally up your sum.
-Frinny
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.
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: -
double sum = 0;
-
for(/*etc*/)
-
{
-
// other stuff like printing names etc
-
sum += ...; // get the pay and add it
-
}
-
// at this point sum will be the total pay
-
Post again if you get stuck.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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.
...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |