Changing Array variables from different classes
Hey guys/gals,
I'm having terrible trouble:mad: with my code and I would be most grateful if somebody could point me in the right direction.
First off let me explain my situation. I have a assignment due in Monday morning and I cant make any progress..
The proposal from my lecturer was to create a program with a superclass called employee, a subclass called production worker that has 2 child classes, Team lead and ordinary production worker. I have to be able to input how many workers are working for the team lead and then he will get a bonus based on the "productivity" of the employees he has under him. I was told to do this using arrays so my code is minimal but also so I can add additional TL's when my program is working. I also have to declare the "bonus" as abstract in the employee class so that I can add a supervisor to the mix later on.
I hope I have explained this so that it makes sense to somebody.. below is my code any any help would be wonderful!
Code Java:
//------------------- Employee class------------------------
public abstract class Employee implements Displayable
{
private double bonus;
private int employeeID;
public String name;
private int startDate;
private double pay;
private static int count;
public abstract double calculateBonus();// NOTE: no body for this method
public double getBonus()
{
calculateBonus();
return bonus;
}
public void setBonus(double bonus)
{
this.bonus = bonus;
}
////////////////////Constructor/////////////////////////////////
public Employee()
{
employeeID = 1 + count; // start student IDs from 1
count++; //incrementing for each new studentinStudentID;
}
public Employee(String inName)
{
employeeID = 1 + count; // start student IDs from 1
count++; //incrementing for each new studentinStudentID;
name = inName;
}
//----------ID---------------
public void setEmployeeID(int ID)
{
employeeID = ID;
}
public int getEmployeeID()
{
return employeeID;
}
//-------------Name------------
public void setName(String Name)
{
name = Name;
}
public String getName()
{
return name;
}
//---------Start Date----
public void setStartDate(int DOS)
{
startDate = DOS;
}
public int getStartDate()
{
return startDate;
}
//------------PAY------------
public void setPay(double pay)
{
this.pay = pay;
}
public double getPay()
{
return pay;
}
public void display()
{
System.out.println("Name is " +name);
System.out.println("ID is " +employeeID );
System.out.println("the start of work Date " + startDate);
}
}
Code Java:
//------------------production worker--------------------------
public abstract class ProductionWorker extends Employee
{
public int totalPay;
public double hoursWorked;
public int unitsProduced;
public int rate;
public ProductionWorker()
{
super();
}
public ProductionWorker(String Name)
{
this.hoursWorked = hoursWorked;
}
public void sethoursWorked(double hourWorked)
{
this.hoursWorked = hoursWorked;
}
public double gethoursWorked()
{
return hoursWorked;
}
public void setUnitsProduced(int unitsProduced)
{
this.unitsProduced = unitsProduced;
}
public double getUnitsProduced()
{
return unitsProduced;
}
public void totalPay()
{
setPay(hoursWorked * rate);
}
public double gettotalPay()
{
return totalPay;
}
public void display()
{
super.display();
System.out.println("The total income of the day: " +gettotalPay());
}
}
Code Java:
//---------------------Ordinary production---------------------------------
public abstract class OrdinaryProduction extends ProductionWorker
{
private double productivity;
private double wages;
public OrdinaryProduction()
{
super();
}
//////////////////////////////////////////////////////////////////
// Modifier methods - used to set values for instance variables
public void setUnitsProduced(int UP)
{
unitsProduced = UP;
}
public double getUnitsProduced()
{
return unitsProduced;
}
public void setProductivity(int Productivity)
{
productivity = Productivity;
}
public double getProductivity()
{
return productivity;
}
/////////////////////////////////////////////////////////////////////
// Accessor methods - used to retrieve values for instance variables
public void calcWages() // Sub-class implements abstract method from the ProductionWorker class
{
setWages(hoursWorked * 12);
}
public double getWages()
{
return wages;
}
////////////////////////////////////////////////////////////////
// Method to determine the productivity - this is called a helper method
// A helper method is one that is invoked within a class but is
// not visible outside of the class
private void calculateProductivity()
{
if( unitsProduced >=4 )
productivity = 'A';
else if( unitsProduced >= 3)
productivity = 'B';
else if( unitsProduced >= 2)
productivity = 'C';
else if( unitsProduced >= 1)
productivity = 'D';
else
productivity = 'E';
}
////////////////////////////////////////////////////////////////
// Method to display information
public void display()
{
System.out.println("Weekly wages :" + wages);
System.out.println("Weekly wages :" + productivity);
}
}
Code Java:
//----------------------Team lead----------------------
public class TeamLead extends OrdinaryProduction
{
private int unitsProduced;
private int productivity;
private int workers;
private double wages;
private OrdinaryProduction[amount];
public TeamLead()
{}
///////////////////////////////////////
//Set array to read in production workers
setOpwArray(OrdinaryProduction[] op){
//OrdinaryProductionWorkerArray = opwArray;
}
public int getOpwArray(OrdinaryProduction[] op)
{
return op;
}
//////////////////////////////////////////////////////////////////
// Modifier methods - used to set values for instance variables
public void sethoursWorked(int inHoursWorked)
{
hoursWorked = inHoursWorked;
}
public void setWages(double inWages)
{
wages = inWages;
}
public void setWorkers(int inWorkers)
{
workers = inWorkers;
}
public void setWages(int inWages)
{
wages = inWages;
}
/////////////////////////////////////////////////////////////////////
// Accessor methods - used to retrieve values for instance variables
public int getHoursWorked()
{
return hoursWorked;
}
public int getWorkers()
{
return workers;
}
public int getProductivity()
{
return productivity;
}
public void calcBonus()
{
if (productivity = A)
bonus = 700;
else if( productivity = B)
bonus = 500;
else if( unitsProduced = C)
bonus = 200;
else if( unitsProduced = D)
bonus = 100;
else
bonus = 0;
}
public void calcWages() // Sub-class implements abstract method from the Employee class
{
setWages(hoursWorked * 13.80);
}
}
Code Java:
//----------------------------Employee info-------------------------
class EmployeeInformation
{
public static void main (String [] args ){
Scanner input = new Scanner(System.in);
TeamLead TL1 = new TeamLead();
System.out.print("How many Production workers working for TL1 ?");
int amount = input.nextInt();
OrdinaryProduction[] opwArray = new OrdinaryProduction[amount];
for (int i = 0; i < opwArray.length; i++){
opwArray[i] = new OrdinaryProduction();
System.out.print("Enter name for production worker?");
opwArray[i].setName(input.nextLine());
System.out.print("Enter Start date for production worker?");
opwArray[i].setStartDate(input.nextInt());
System.out.print("Enter hours worked for production worker?");
opwArray[i].sethoursWorked(input.nextInt());
System.out.print("Enter units produced for production worker?");
opwArray[i].setunitsProduced(input.nextInt());
System.out.print("Enter productivity for production worker?");
opwArray[i].setproductivity(input.nextInt());
}
}
}
Re: Changing Array variables from different classes
...what's your question?
When posting code, it should be in the form of an SSCCE (that's a link, but in other words, as short as possible). Don't forget the code tags.
Re: Changing Array variables from different classes
Moved to Collections and Generics - Java Programming Forums
As Kevin says, what is the problem exactly? Please tell us where you are stuck.
Re: Changing Array variables from different classes
Quote:
Originally Posted by
KevinWorkman
...what's your question?
When posting code, it should be in the form of an
SSCCE (that's a link, but in other words, as short as possible). Don't forget the code tags.
Oh ok, my question is what is the problem with my array code? I am missing something somewhere to read in production workers but i dont know what it is. I keep getting errors.
tnx
Re: Changing Array variables from different classes
Quote:
Originally Posted by
smellyhole85
Oh ok, my question is what is the problem with my array code? I am missing something somewhere to read in production workers but i dont know what it is. I keep getting errors.
tnx
What errors? What do you expect your code to do? What does it do instead? Be specific. Post an SSCCE that demonstrates the problems, and make sure to point out any line numbers referenced in the Exception's stack trace so that we don't have to count.
Re: Changing Array variables from different classes
private OrdinaryProduction[amount];
You shouldn't have the size of the array in your declarations before the constructor.
1.) It's not initialized yet
2.) Even if it were, it would refer to the value at index amount, not the whole array
Re: Changing Array variables from different classes
private OrdinaryProduction[] op;
public OrdinaryProduction[] get OPArray(int amount)
{
op = new OrdinaryProduction[amount];
// it's still empty at this point you know but is of size amount
return op;
}