Hi.... im just trying out some new programs using inheritance... can sumone pleeassee teme how to use array of objects in java??? ive donw it in c++ but i guess its no tthe same here in java...can anyone pleeasee help me out..?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi.... im just trying out some new programs using inheritance... can sumone pleeassee teme how to use array of objects in java??? ive donw it in c++ but i guess its no tthe same here in java...can anyone pleeasee help me out..?
hi...im just running over some programs in java inheritance... and i so desperatly need to use array of objects for acesing some of my subclasses.....ive done this in c++ ,....well i guess its not the same here in java... can someone help me out ..??????
you can use an array list:
ArrayList<object> arr= new ArrayList<object>();
or using a regular array:
object[] arr= new object[size];
and then for an array its pretty much the same as C++ and for an arraylist, you use .add to add objects, .get to get objects, .set to set an index of the list to an objects .delete to delete an object at some point, .contains to check if an object is in the list and so on and so forth
Can you post the code here?
ummm... actually wat I meant was not ecaxtly this.... its like.... I access a class repeatedly using an object..and I want to store each access to the class in an array of the object tht I created for the class.... is this possible in java..??? is my question too complicated..?
Update the array during each access...
import java.util.Scanner;
class A
{
double bp,da,hra,gp;
int pf,ca;
void professor()
{
bp=37000;
da=0.4*37000;
hra=10000;
pf=1000;
ca=1000;
gp=bp+hra+da+ca;
System.out.println(+gp);
}
void associate()
{
bp=37000;
da=0.4*37000;
hra=8000;
pf=1000;
ca=1000;
gp=bp+hra+da+ca;
}
void assistant()
{
bp=30000;
da=0.4*30000;
hra=8000;
pf=800;
ca=800;
gp=bp+hra+da+ca;
}
}
class Emp extends A
{
public static void main(String args[])
{
int n,i;
String name;
String desg;
String s1="professor";
Emp[] obj= new Emp[4];
Scanner sc = new Scanner(System.in);
for(i=1;i<3;i++)
{
System.out.println("\n Enter the desg : ");
desg=sc.nextLine();
if(desg.equals(s1))
{
obj[i].professor();
}
}
}
}
this is my coding..... at the line where ive used "obj[I].professor" its shows me a java.lang.nullpointer error.... can sumone please teme wat is the other way for doing this??
You've never initialized the objects in the array. Please use code tags when posting code and include the full stack trace.
and how do I do tht??
this is my coding.......
import java.util.Scanner;
class A
{
double bp,da,hra,gp;
int pf,ca;
void professor()
{
bp=37000;
da=0.4*37000;
hra=10000;
pf=1000;
ca=1000;
gp=bp+hra+da+ca;
System.out.println(+gp);
}
void associate()
{
bp=37000;
da=0.4*37000;
hra=8000;
pf=1000;
ca=1000;
gp=bp+hra+da+ca;
}
void assistant()
{
bp=30000;
da=0.4*30000;
hra=8000;
pf=800;
ca=800;
gp=bp+hra+da+ca;
}
}
class Emp extends A
{
public static void main(String args[])
{
int n,i;
String name;
String desg;
String s1="professor";
Emp[] obj= new Emp[4];
Scanner sc = new Scanner(System.in);
for(i=1;i<3;i++)
{
System.out.println("\n Enter the desg : ");
desg=sc.nextLine();
if(desg.equals(s1))
{
obj[i].professor();
}
}
}
}
and it shows me an java.lang.nullpointer error...
--- Update ---
how do I update the array..?
NullPointerException means, that you're trying to do something with null. Hence, check up every object in main() to be not null. Please, wrap your code in the [code=Java]/*...*/[//code] - it's really difficult to read.
and how do I use code tags and full stack trace?
By reading the big announcement at the top of this forum. The full stack trace ist the complete error message you get at runtime.
do I have to add tht before and after my code to convert it to a presentable form?
yes. [code=Java] before, [//code] (with 1 slash) after
Fanta Hubert (May 27th, 2013)
import java.util.Scanner; class A { double bp,da,hra,gp; int pf,ca; void professor() { bp=37000; da=0.4*37000; hra=10000; pf=1000; ca=1000; gp=bp+hra+da+ca; System.out.println(+gp); } void associate() { bp=37000; da=0.4*37000; hra=8000; pf=1000; ca=1000; gp=bp+hra+da+ca; System.out.println(+gp); } void assistant() { bp=30000; da=0.4*30000; hra=8000; pf=800; ca=800; gp=bp+hra+da+ca; System.out.println(+gp); } void display() { System.out.println(+gp); } } class Emp extends A { public static void main(String args[]) { int n,i; String name; String desg; String s1="professor"; String s2="associate"; A obj=new A(); Scanner sc = new Scanner(System.in); for(i=2;i<4;i++) { System.out.println("\n Enter the desg : "); desg=sc.nextLine(); if(desg.equals(s1)) { obj.professor(); } else if(desg.equals(s2)) { obj.associate(); } } for(i=1;i<3;i++) { obj.display(); } } }
--- Update ---
now this is my code..... well after accessing function professor and associate... I want to print both their gross pays ...well my output displays the gross pay of the associate itself two times.. which means tht the object has only the last acces In it... how to change this..?
So do you want some kind of memory in your object to remember the results of all your accesses and computations?
EXACTLY...!!!! exactly........any idea how to do tht..??
I used to have an object array for it in c++... well im zero abt it in java....please help me out..
The best way is to use collection, a List for example.
Many useful tips can be found on the Announcements page, including help on the use of code tags.
Always include the full text of the error message with the code and questions when you post. This is for speed of service on your behalf, and a huge time saver on the other end. The message will give clues to the location and a variable name. Figure out why that variable does not have a valid value when it is being accessed.
Are you asking someone to tell you what the other way is? If so please use full words. Storage is cheap these days, those extra letters are okay, please include them.
...The other way to do what exactly?
--- Update ---
Please do not double post your question.
Threads merged