-1

I'm creating a simulation for one of my classes and I'm running into some trouble. Looking for some direction here + any tips.

I have two classes so far and I'm looking to implement some other things. At the moment, I want to be able to access the points (x and y coordinates) of my objects that are in the array stored in my other class.

mmuso
  • 11
  • 3
  • 1
    You mean `ecosystem[i][j].method();`? – Andrew Li May 03 '17 at 20:01
  • Kind of! I want to call getCarnivoreColumn() on the instances of Carnivores that are created in initialize() in Ecosystem. – mmuso May 03 '17 at 20:06
  • I already told you how... – Andrew Li May 03 '17 at 20:07
  • Hmmm... sorry if I'm being dense. I tried doing that inside of my Ecosystem class and I was not able to call on getCarnivoreColumn(). – mmuso May 03 '17 at 20:11
  • Oh, I forgot. You must cast it to the appropriate type in the instanceof if statement. – Andrew Li May 03 '17 at 20:15
  • Ahh, I tried that as well and I still am not able to access the method from my Carnivore class on the ecosystem. I'm doing the following: (Carnivore)ecosystem[i][j].getCarnivoreColumn(); – mmuso May 03 '17 at 20:18
  • 1
    No. that casts the return value. You have to cast the object. `((Carnivore) ecosystem[i][j]).getCarnivoreColumn();` – Andrew Li May 03 '17 at 20:19
  • Thank you for your help! That works! Do you think there's a simpler way to populate my ecosystem where I wouldn't need to cast the object's type to another type or is there not enough information for you to judge based off of what I've given? – mmuso May 03 '17 at 20:22
  • Actually, for your array, you have an Animal array instead of an Ecosystem array right? Because an array of type Ecosystem 1. Makes no sense logically 2. shouldn't work... for polymorphism to work you need a common parent, which is Animal not Ecosystem. – Andrew Li May 03 '17 at 20:27
  • 1
    `instanceof` and downcasts are CODE SMELL. Why not have the method `getColumn`, declared in a superinterface and implemented in the subclasses? Java's an object-oriented language; you might as well try object-oriented programming, eh? – Lew Bloch May 03 '17 at 20:33
  • My ecosystem is comprised of Animals (which branches off into Carnivores and Herbivores) and Plants, so I chose Ecosystem because I've made it so that they all extend Ecosystem. @LewBloch I agree and I'm doing some reading on that since you pointed it out. I'm not seeing how to avoid using instanceof in my particular case – mmuso May 03 '17 at 21:20
  • If `Animals` had a method `getColumn` method that its subclasses overrode, then the caller could use it through an `Animal` reference. You eliminate the `instanceof` for those subclasses. If the method were pushed up one more level, you could use an `Ecosystem` reference and eliminate another layer of `instanceof`. Your object model is wrong. If `Carnivore` _is-an_ `Animal` but the supertype has not even one behavior for subtypes, especially a shared behavior!, then either you have a useless supertype or bad implementations thereof, or both. – Lew Bloch May 03 '17 at 23:37

2 Answers2

0
  1. You can't store a new instance of each of those animal types in an Ecosystem array since those animal types are not Ecosystems. To do that, you'd need to make those other classes extend Ecosystem.
  2. I'd suggest you make Ecosystem an interface and implement it from the other classes. Define the getRow() and getColumn() in the interface and override it in the sub class, that way each array element can access it and would return their specific implementation.
  3. Doing public static void printCarnivore() is wrong. You can't have such static method in an inner class.
yusuf.oguntola
  • 492
  • 3
  • 10
-1

This should work.

if (ecosystem[i][j] instanceof Carnivore) {
    Carnivore.printCarnivore();
    System.out.print(" | ");
    int x = (Carnivore) ecosystem[i][j].getCarnivoreRow();
    int y = (Carnivore) ecosystem[i][j].getCarnivoreColumn();
    System.out.println("Coordinates: (" + x + ", " + y + ")" )
}
Mohammad C
  • 1,321
  • 1
  • 8
  • 12