• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Debugging? Program : Array Holding Many Objects  RSS feed

 
Samuel Weston
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay,I created a program that demonstrated an array holding a number of objects, each from different classes.
the printout I got for the first program ended up being gibberish. when I made the program a second time, I got the correct result.

I've been trying to pinpoint the anomaly but I can't seem to find it. So ill try breaking down what I did and maybe you guys could help me out?

Goal:
The purpose of the program was to have a single Class (we'll call the Class 'Animal') to pass the parameters of a previously made method. This method would, amongst other things, call on an array.
Further, I would then make subclasses of the Class Animal, and then use them as well to pass the method parameters, thus demonstrating subclasses can be used in place of the super class.

3 of these classes were made purely to as a "place setter". the Classes called "Cat" and "Dog" respectively were created to be subclasses of the Class Animal (with no other code inside them aside from "extends Animal)
the 3rd "place setter" called "ClasstoHoldObjects" was created to be the array class (ClasstoHoldObjects itself has no code in it aside from the class name)

these are the ones we want

Animal class (which holds the coding meant to be used)
Here is the Animal Class




and then I made a Class to execute everything



the print out was this

next, im putting THIS line in the array... june_6.Dog@a0dcd9


(and the same by the Cat object)

Notes: I recognize june6 as the package name.

I didn't include the 3 other classes nor did I include the correctly executed version of this program to save you guys some time.
if you want them please mention it

This is the first time I used java code brackets for a post -I hope it came out alright.

finally, I erased extra lines as well as comments and simplified class names to make the code more readable. If you want the original, or some class names are a bit off- I could give you the original with all its comments and longer named classes

thanks for your help
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Weston wrote:the printout I got for the first program ended up being gibberish.
june_6.Dog@a0dcd9

That's not gibberish. It's the implementation of the toString method in the Object class. See this for an explanantion of what the string represents.
If you want output more relevant to your classes you need to override the toString method in your classes.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you don't need the ClasstoHoldObjects class. If Cat and Dog are subclasses of Animal then you can declare your array as
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with arrays of multiple types. If you want to pass multiple arguments of specific types you can create a class to encapsulate them, but each field will be strongly typed. The fields should be private with getters as appropriate.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!