0

I'm new to programming in Java and previously only programmed in Python. I have to write a class that doesn't include a constructor but instead has a method that "configures" the class object. So as I understand it, the class object is created as an object with no content and then its content is set by the configure method. An array is passed into the configure method, but when I try to set the class object equal to the array it says it's a type mismatch - I either need to change the class object to an array or the array to a class object, which from what I understand is undefined at that point. What is the class object? How can I make it an array without a constructor? I'm so confused and there seems to be little reference material for this situation online.

// UPDATE //

Here's the configure method per your requests.

I know you don't want English, but it's hard to explain just with a few lines of code - it's part of a larger program that finds the most efficient path across a map. There's a GUI Interface that operates through MapState that executes methods from BasicMapState and MapSolver that executes methods from BasicMapSolver. givenState is an array representing all the map tiles, with a 1 in the tile where the solver is currently located.

I would ideally like to store the givenState array and a parent move (what direction the solver chose to get to the givenState) as attributes of the MapState class object, but I'm lost. Please help.

public class BasicMapState implements MapState {
@Override
public void configureState(int[] givenState) {
    MapState current = givenState
}
6
  • 5
    Please post code, it's better than English. Commented Sep 15, 2014 at 6:39
  • Your code will tell us what exactly the issue. Commented Sep 15, 2014 at 6:39
  • 1
    Sounds like you're trying to code a Factory? Commented Sep 15, 2014 at 6:49
  • @MarounMaroun Updated with code. Please help, thank you! Commented Sep 15, 2014 at 7:01
  • @RuchiraGayanRanaweera Updated with code - please take a look! Commented Sep 15, 2014 at 7:02

2 Answers 2

1

First of all, if you do not write a constructor, an empty constructor is autogenerated by the compiler. If you do not want the constructor to be called, make it privte like this

private BasicMapState() { }

If you do not want to make an object from this Class, you can work with static variables, otherwise you have to call the constructor from any public method.

To come to your problem: As I understand you try to make the givenState Array equals the interface? This makes no sense. If you want to set something like this, you have to declare a variable first which you can set. Interfaces are in most cases only for declaring method names. The interface is an inteerface and not Array. To explain your error, that and array is not compatible with Object. If I remember correctly, Object is the basic root class from which ALL Classes and interfaces inherit.

I hope that helps a bit, if not let me know :)

John

Sign up to request clarification or add additional context in comments.

Comments

0

mu~maybe

public class BasicMapState implements MapState {
private int[] givenState;
@Override
public void configureState(int[] givenState) {
    this.givenState = givenState;
}

then maybe customize a compare method like

public boolean myCompare(BasicMapState bms){
    return Arrays.equals(givenState, bms.getGivenState());
}

or just override the equals method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.