Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:10:41+00:00 2026-05-27T09:10:41+00:00

I have two abstract classes called Robot and GeometricElement . Each one of them

  • 0

I have two abstract classes called Robot and GeometricElement.
Each one of them has subclasses.
I need to build a matrix of NxM that holds either a Robot or a GeometricElement, so
I’ve written another class and called it Item, and then Robot and GeometricElement
inherit from that class.

Here is the code:

public class Item {
    private Dot currentLocation;
    private boolean taken;

    public Item(Dot location) {
        int x = location.getXcomponent();
        int y = location.getYcomponent();
        currentLocation = new Dot(x,y);
        taken = false;
    }

    // more code 
}

public abstract class GeometricElement extends Item {

    private float area; 

    public GeometricElement(Dot location) {
        super(location);
    }
}


public abstract class Robot extends Item { 
    private float basketArea;

    /*  Constructor */

    public Robot(Dot location, float basketNewArea) {
        super(location);
        basketArea = basketNewArea;
    }

    // some more code 
}

The class that takes care of storing the Items is called Ground :

public class Ground {

    private Item[][] board;
    private Queue elementQueue;

    /*  Constructor */

    public Ground(int row,int col) {
        board = new Item[row][col];
        this.elementQueue = new Queue();
    }

    // more code 

    public void addElementsToRobot() {
        while (this.elementQueue.doesQueueHasItems()) {
            GeometricElement element = elementQueue.popElementFromQuque();
            int x = element.getXlocation();
            int y = element.getYlocation();
            if (this.board[x][y].isTaken()) {
                if (board[x][y] instanceof Robot) {
                    // add geometric element to the basket
                }
            }
        }
    }
}

As mentioned above, I need to store a Robot or a GeometricElement in the board. The problem starts when I try to read from the matrix (the ‘board’ matrix in ‘Ground’): I can’t seem to find a way to tell if I have in a cell either a Robot or a GeometricElement, without using instanceof.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:10:42+00:00Added an answer on May 27, 2026 at 9:10 am

    I don’t know your goals of course, but from the top of my head:

    I would personally refactor this using Composite pattern, so that you can access all elements in a uniform fashion. And as for determining without instance of, you can have an abstract method on your Item class telling each other apart. Or you could have public property doing that. Or you could come with better design.

    It really depends on what you are trying to achieve. Instanceof may be actually a solution to your problem but generally, it points to deeper design issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation
I have two abstract classes which will hold references to each other.How / can
I have an abstract base class called LibItem, and two inherited classes called Book
I have two abstract classes, lets call them Car 's and Wheel 's. I
I have two one-to-one relations here between a class called MailAccount and the classes
Consider the following example. I have an interface MyInterface, and then two abstract classes
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
I have an abstract entity called Block which contains two attributes: column and order
I have a very simple scenario, using NHibernate: one abstract base class animal; two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.