How to Store two different class objects in Same array. - new to Java ..Need Help

Thread Solved
Reply

Join Date: Sep 2008
Posts: 11
Reputation: Kamal_Java is an unknown quantity at this point 
Solved Threads: 0
Kamal_Java Kamal_Java is offline Offline
Newbie Poster

How to Store two different class objects in Same array. - new to Java ..Need Help

 
0
  #1
Sep 25th, 2008
Hi ... [ Need ur suggestion NOT code .... ]

This is one part of my assignment .. I need your help on how to do this . Below is my short description of my problem ...

I have two classes Named Passenger class and container class .

Passenger class has its own variables like passenger name ( String ) , ID ( int ), and Injury Level (An enum class which has priority like 1 , 2 , 3 and 4 ).

Employee class has its own variable like containerID ( int ) , and ConatinerType ( this can be one of the three types URGENT , IMPORTANT , or GENERAL - these are from enum class )

I will be taking this passenger details[ has 100 of passenger details ] and container details [ 100 of container ] from a file and should write to an Array . Here comes my question.

1. How do i store the Passenger and Container class object in an single array.
[ Note : There must be only one array , not a separate array for passenger and a seperate array for containers.]

Hope i have given clear description of the problem ...
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to Store two different class objects in Same array. - new to Java ..Need Help

 
0
  #2
Sep 25th, 2008
one suggestion: try first, this looks like a basic question to test if you understand Object Oriented programming or not

to give you an idea how to solve this quickly: all classes can be stored in an array of their superclass (the class they inherit from) and all classes in Java, no matter who wrote them and or it is mentioned in the code itself, inherits from the object "Object"

this is just an idea, remember that 'an array of objects' does not have to be 'Object[] row = new Object[int x];'
it can occur in different ways. just make sure that when youtry to get the info out of it, you check the kind of object before you try to store it into a new (local) variable
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: Kamal_Java is an unknown quantity at this point 
Solved Threads: 0
Kamal_Java Kamal_Java is offline Offline
Newbie Poster

Re: How to Store two different class objects in Same array. - new to Java ..Need Help

 
0
  #3
Sep 25th, 2008
remember that an array of objects' does not have to be 'Object[] row = new Object[int x];
what does it mean ......
Should i be doing Object[] obj = new Object[10];
obj[0] = new Passenger( );
obj[1] = new Container( );
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to Store two different class objects in Same array. - new to Java ..Need Help

 
0
  #4
Sep 25th, 2008
it's a possibillity.. or maybe thinking about a Collection, if you do not know how many elements you'll have to put in the array
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC