Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Storing object references in an array

Thread view: 
conmulligan - 19 Sep 2007 16:32 GMT
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?
derek - 19 Sep 2007 16:47 GMT
public static void main(String args[]) {

        Employee[] employee = new Employee[3];
        String output = "";
Brian - 19 Sep 2007 17:00 GMT
>        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
Thomas Fritsch - 19 Sep 2007 17:08 GMT
[...]
>     Employee employee[];
>     String output = "";
[quoted text clipped - 11 lines]
>     // 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];

Signature

Thomas

Roedy Green - 19 Sep 2007 20:31 GMT
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.

Signature

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

Lew - 19 Sep 2007 23:34 GMT
>         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.

Signature

Lew

Joshua Cranmer - 19 Sep 2007 23:54 GMT
> 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?

Signature

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

conmulligan - 20 Sep 2007 10:39 GMT
Thanks everyone, got it.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.