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 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
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 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...
|
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
|
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">
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |