Storing object references in an array

C

conmulligan

Hi all,

I'm new to Java so bear with me.

I want to store a bunch of object references in an array.

Here's what I have:

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

// payroll class
// ------------
public class Payroll {

public static void main(String args[]) {

Employee employee[];
String output = "";

// instatiate employee classes
Boss boss = new Boss("John", "Smith", 800.0);
CommissionWorker commissionWorker = new CommissionWorker("Sue",
"Jones", 400.0, 3.0, 150);
PieceWorker pieceWorker = new PieceWorker("Bob", "Lewis", 2.5, 200);
HourlyWorker hourlyWorker = new HourlyWorker("Karen", "Price",
13.75, 40);

DecimalFormat precision2 = new DecimalFormat("0.00");

// add boss earnings to output
employee[0] = boss;
output += employee[0].toString() + " earned $" +
precision2.format(employee[0].earnings()) + "\n" +
boss.toString() + " earned $" + precision2.format(boss.earnings()) +
"\n";

// add commission worker earnings to output
employee[1] = commissionWorker;
output += employee[1].toString() + " earned $" +
precision2.format(employee[1].earnings()) + "\n" +
commissionWorker.toString() + " earned $" +
precision2.format(commissionWorker.earnings()) + "\n";

// add piece worker earnings to output
employee[2] = pieceWorker;
output += employee[2].toString() + " earned $" +
precision2.format(employee[2].earnings()) + "\n" +
pieceWorker.toString() + " earned $" +
precision2.format(pieceWorker.earnings()) + "\n";

// add hourly worker earnings to output
employee[3] = hourlyWorker;
output += employee[3].toString() + " earned $" +
precision2.format(employee[3].earnings()) + "\n" +
hourlyWorker.toString() + " earned $" +
precision2.format(hourlyWorker.earnings()) + "\n";

// display the contents of output
JOptionPane.showMessageDialog(null, output, "Payroll",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}


With this code I get the following error:

variable employee might not have been initialized
employee[0] = boss

The Employee class is abstract so I cannot instatiate it.

Any ideas?
 
D

derek

public static void main(String args[]) {

Employee[] employee = new Employee[3];
String output = "";
 
B

Brian

Employee employee[];
variable employee might not have been initialized
employee[0] = boss

The Employee class is abstract so I cannot instatiate it.

Any ideas?

You have to write: Employee[] employee;



A better way to do this is by using a list(Here is an example using an
ArrayList):

import java.util.*; --> need this import


ArrayList<Employee> employee = new ArrayList<Employeee>();

//then you can add an Employee-objet to the list:
employee.add(new Boss("John", "Smith" , 800.0);
employee.add(new PieceWorker("Bob", "Lewis", 2.5, 200);
employee.add(new HourlyWorker("Karen", "Price",13.75, 40);

//then you can iterate through the list:

output="";
for(Employee e: employee){
output += e.toString() + " earned $" + precision2.format(e.earnings())
+ "\n"
}

this should cover most of your code :)

Hope it was any help

/Brian
 
T

Thomas Fritsch

conmulligan wrote:
[...]
Employee employee[];
String output = "";

// instatiate employee classes
Boss boss = new Boss("John", "Smith", 800.0);
CommissionWorker commissionWorker = new CommissionWorker("Sue",
"Jones", 400.0, 3.0, 150);
PieceWorker pieceWorker = new PieceWorker("Bob", "Lewis", 2.5, 200);
HourlyWorker hourlyWorker = new HourlyWorker("Karen", "Price",
13.75, 40);

DecimalFormat precision2 = new DecimalFormat("0.00");

// add boss earnings to output
employee[0] = boss; [...]

With this code I get the following error:

variable employee might not have been initialized
employee[0] = boss
You have to initialize the array variable employee, for example by
employee = new Employee[4];
before using employee[0], employee[1], for the first time.
The Employee class is abstract so I cannot instatiate it.
Because class Employee is abstract, you cannot create an Employee
*object* as such, by
new Employee();
But it does *not* forbid to create an *array* of Employees, by
new Employee[4];
 
R

Roedy Green

The Employee class is abstract so I cannot instatiate it.

There are two stages of initialisation for an array,

allocating an array of null pointers the right length.

Setting those pointers to point to actual objects.

The first you can do even if the class of the things you want to point
to eventually is abstract. The second will require some real classes.

See http://mindprod.com/jgloss/array.html for a fuller explanation.
 
L

Lew

conmulligan said:
Boss boss = new Boss("John", "Smith", 800.0);
CommissionWorker commissionWorker = new CommissionWorker("Sue",
"Jones", 400.0, 3.0, 150);
PieceWorker pieceWorker = new PieceWorker("Bob", "Lewis", 2.5, 200);
HourlyWorker hourlyWorker = new HourlyWorker("Karen", "Price",
13.75, 40);

You don't show the definitions of these classes. Are they all subclasses of
Employee?

Under no circumstances should you ever represent monetary amounts with double
or float values. Initialize those BigInteger or BigDecimal values with Strings.
 
J

Joshua Cranmer

conmulligan said:
With this code I get the following error:

variable employee might not have been initialized
employee[0] = boss

I think that the error might be more obvious if it was changed to:
variable employee was not initialized
employee[0] = boss

Thoughts?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top