• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Understanding  RSS feed

 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if someone could explain to me how arrays work when you are taking information from a few different classes. Let's say we have 5 class Address, Name, School, UTA, and Texas. So UTA and Texas are children of School. I need to build two different arrays that go in class SchoolTest with two variables coming from UTA (say number of students and number of prof.), an address coming from class Address, a name come from class Name, another address coming for class Address and finally a course number coming from class School. *Note, I just made this example up so if it does not make sense at all, just let me know. It may be coming out different than what I am thinking in my head

Say Address returns an address:


Name returns:


School returns:


UTA returns:


Texas returns:



So for example, would my test code be something like this:



I am so sorry this is such a long post, I thought that all of the returns and code would help describe what I am trying to do and what I am a bit sketchy on. Building the array from different classes is my main problem. So if anyone can help me with any advice at all, I would be grateful!! Again, if there are any problems reading what I was trying to say, just let me know and I will be more than happy to try to explain a little better!! Thanks so much!!

[ UD: edited for better readability ]
[ January 28, 2006: Message edited by: Ulf Dittmer ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's clear enough what the code does, but what's not clear to me is what exactly you are asking. You said it's the building of an array out of different classes, but you seem to have handled that by subclassing, so that in actuality the array is full of objects of the same (super-)type. Does the code not compile, or not do what you expect it to do?
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I use Eclipse SDK and it is showing there is an error in my array. So it will not let me compile. So basically, I was wondering if anyone saw a problem with my array or there was a better way of writing it.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, post the error message, and we'll take a stab at it. In the meantime, I'll move this thread to the "Java in General (intermediate)" forum, as this isn't really about applets. Please find the thread there and continue the discussion.
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is the error I am getting right now:

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These errors have nothing to do with arrays. Rather they are telling you that the classes Texas and UTA do not have constructors that take the parameter lists that you are trying to pass. So what constructors do these two classes have? Perhaps you have declared an appropriate constructor in the super class School. However, constructors are not inherited so you will need to write constructors in both subclasses as well. This is just a guess, so I suggest you post some code snippets that illustrate the constructors in the School, Texas, and UTA classes.

Layne
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok first of all, thanks for the replies and help with this little problem. Here are the constructors in the School, UTA and Texas classes. Hopefully this is what you need, let me know if not. Thanks so much!!

UTA Class:


Texas Class:


School Class:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Layne guessed, your problem is your constructors do not take any arguments, therefore trying to pass any type of argument will cause a compiler error. Either you need to write constructors that take parameters, or instatiate each object with the default constructors, and set the fields using the setter methods of the class.
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last post I added was just a portion of those classes, just to show my constructors that Layne asked me to post...I can post all of the code for each if that would be helpful. That way you could see what I am trying to return.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is not what with what gets returned; that makes no difference here. Garrett is right on the mark: either you need constructors, or setter methods to get the actual values for the various parameters into your objects.
[ January 29, 2006: Message edited by: Ulf Dittmer ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type of the methods doesn't matter. If these are your sole constructors then you can't do what youre trying to do. For instance:

If that is your only constructor then the only way you can instantiate a UTA object is by calling its only constructor just as written.

If you want to call the constructor the way you are doing it in your SchoolTest class, then you need to write a constructor that take the parameters you are trying to pass in. For instance for your UTA class it might look like:

In short, you can only call methods and constructors exactly the way they are defined in your class. Anything else will cause a compiler error. If this is for a course you are taking, I suggest you reread the portion of your text that talks about declaring constructors and methods.
[ January 29, 2006: Message edited by: Garrett Rowe ]
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I see what you are saying. Below are how the UTA and Texas Classes look now after looking at all the notes you gave me. Am I on the right track now? Hopefully I am getting closer to getting that array to work. Sorry if this is getting frustrating, I am frustrating myself


[ January 29, 2006: Message edited by: Holly Leery ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are seriously getting there. One problem that you will encounter if you do it like this:


Is that you cant call setStudents(int), or setProfessors(int) before the constructor has exited. I know its kind of confusing but while you're inside the constructor, an object of the class has not been constructed yet, therefore you can't call any non-static methods of this class from inside the constructor. However you can access class level variables. Therefore this would be legal:

Also if you are only going to hold references to the students and professors in the subclass and let the superclass handle the name, address, and courseID, there is no need to have variables or methods for them in the subclass since they will never be used. (I really hope you understood that sentence because I can't think of a better way to say it ).

And lastly remember if you are going to pass parameters to the superclass constructor, you'll have to write a superclass constructor that accepts parameters.

You're on your way.

Garrett
[ January 29, 2006: Message edited by: Garrett Rowe ]
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that you cant call setStudents(int), or setProfessors(int) before the constructor has exited. I know its kind of confusing but while you're inside the constructor, an object of the class has not been constructed yet, therefore you can't call any non-static methods of this class from inside the constructor.


But this code runs well and prints 10 (in Jdk 1.5)
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by karthi keyan:

You're absolutely right. I stand corrected. I wonder where I got the idea that you couldnt?
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I changed the subclass UTA, it is below. Does that look better than before? It compiles but then my test class doesn't. If my subclasses are compiling and same with my superclass, could there be something wrong with the array? Maybe I didn't reference something right in my superclass. Here is the superclass (School) and one of the subclasses, UTA, which is pretty much the same as the other subclass, Texas:





Thanks again for all of the tips and help...it really is helping me understand the concepts and such.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your test class now needs to call the setters specifically. What does it look like after you changed it to do so?
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by call the setters specifically? You mean in the array? I am a bit fuzzy on the array part. Here is the test code I am using..which I see is obviously wrong.


[ January 30, 2006: Message edited by: Holly Leery ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem has nothing to do with the array.

You define a constructor with the following parameters:



but your test code is using a constructor like



which you don't have. So you need to either add a constructor like that, or change your test code to work with the ones you have.
[ January 31, 2006: Message edited by: Ulf Dittmer ]
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I finally got what you all were saying..lol!! It just clicked. I was sitting here looking at it and all of the sudden it just clicked in my head!! I got the applet running, thanks to all of the help and advice!! It was so small, but I just wasn't seeing it at all!! Thank you all so much for all of the great tips, I learned a lot from this already. Thanks again I appreciate everything!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!