Storing object references in an array - Java

This is a discussion on Storing object references in an array - Java ; 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 ...

+ Reply to Thread
Results 1 to 8 of 8

Storing object references in an array

  1. Default Storing object references in an array

    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?


  2. Default Storing object references in an array

    public static void main(String args[]) {

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

  3. Default Re: Storing object references in an array

    On Wed, 19 Sep 2007 15:32:28 -0000, conmulligan
    <conmulligan@gmail.com> wrote:


    > 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




  4. Default Re: Storing object references in an array

    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];

    --
    Thomas

  5. Default Re: Storing object references in an array

    On Wed, 19 Sep 2007 15:32:28 -0000, conmulligan
    <conmulligan@gmail.com> wrote, quoted or indirectly quoted someone who
    said :

    >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.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com

  6. Default Re: Storing object references in an array

    conmulligan wrote:
    > 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.

    --
    Lew

  7. Default Re: Storing object references in an array

    conmulligan wrote:
    > 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?

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth

  8. Default Re: Storing object references in an array

    Thanks everyone, got it.


+ Reply to Thread

Similar Threads

  1. Get length of returned array without storing?
    By Application Development in forum Perl
    Replies: 13
    Last Post: 11-06-2007, 12:49 PM
  2. Storing and reading different types to/from an array
    By Application Development in forum CSharp
    Replies: 10
    Last Post: 08-17-2007, 09:30 AM
  3. Object References as a member of a bigger Object
    By Application Development in forum Perl
    Replies: 4
    Last Post: 08-06-2007, 05:44 AM
  4. storing string array into HDF
    By Application Development in forum Idl-pvwave
    Replies: 0
    Last Post: 07-11-2007, 12:48 AM
  5. Storing an Array in a Hash
    By Application Development in forum Perl
    Replies: 1
    Last Post: 12-28-2003, 03:41 PM