3

As you'll see, I'm fairly new to object oriented programming. I'm trying to teach myself, but am stuck on this and can't figure out what to do.

I'm trying to write some code to layout a rectangular grid into rows and columns. Think "laying out small squares onto a large rectangle". The user will input how many of the "small squares" they have. My goal is to map this integer into the best layout of rows and column.

The user can input any integer from 20 through 100. For each of the 81 different possible entries, I have determined the best way to layout these small squares in rows and columns. What I need to do now is to get these 81 different layouts into my program then identify and return the one that applies to the user's input.

Since there are only 81 values and they range through consecutive integers, I think that an array is the best idea (rather than a map, hashmap, vector, etc.). Here's what I have thus far. It's a bit of a mess, but I think you'll get the idea of what I'm trying to do. Can anyone help me? I can't seem to return the row and column values that I need.

Thank you!

public static void getLayout (int numSquares) {

    int rows;
    int columns;        

    Layouts myLayout = new Layouts();

    rows = myLayout[numSquares].r; //this is where it fails
    columns = myLayout[numSquares].c;
}

class RowCol<R, C> {

    /* Class Variables */
    public final R r;
    public final C c;

    public RowCol(R r, C c) {
        this.r = r;
        this.c = c;
    }

    public R getRow() {return r;}
    public C getCol() {return c;}
}

class Layouts {

    RowCol[] myLayouts;

    public Layouts() {

        /* Set the numbers of Rows and Columns for each possible
         * number of squares requested.
         */
            myLayouts[20] = new RowCol(5, 4);   // 20 Problems
            myLayouts[21] = new RowCol(7, 3);   // 21 Problems
            myLayouts[22] = new RowCol(5, 4);   // 22 Problems
            myLayouts[23] = new RowCol(5, 4);   // 23 Problems
            myLayouts[24] = new RowCol(6, 4);   // 24 Problems
            myLayouts[25] = new RowCol(5, 5);   // 25 Problems
            myLayouts[26] = new RowCol(6, 4);   // 26 Problems
            myLayouts[27] = new RowCol(6, 4);   // 27 Problems
            myLayouts[28] = new RowCol(7, 4);   // 28 Problems
            //etc...

Edit: Applying the responses below, I needed to intantiate an object in the Layouts class, which I did. Then, I modified the getLayout class to isolate the returned RowCol value. Here is that class updated. My issue at this point is that I can't convert the row and column into integers so that I can use them as such.

public static void getLayout(int numSquares) {

    Layouts myLayout = new Layouts();
    RowCol myRC = myLayout.getLayout(numProbs);
    int rows = Integer.parseInt(myRC.getRow());
    int cols = Integer.parseInt(myRC.getCol());
}

The error is:

no suitable method found for parseInt(Object) method Integer.parseInt(String) is not applicable (actual argument Object cannot be converted to String by method invocation conversion) method Integer.parseInt(String,int) is not applicable (actual and formal argument lists differ in length)

EDIT: Solved. Thanks everybody!

5
  • 1
    Can you post the error you are getting? Commented Aug 16, 2012 at 2:43
  • 3
    Did you initialize your array? RowCol[] myLayouts = new RowCol[81]; Commented Aug 16, 2012 at 2:45
  • Thank you both. DaoWen, that was the problem. I'm still having trouble getting the returned object separated into their two independent values - a row and a column (both integers). I've added a line to my original post to show my code at this point. Can anyone see why I can't get back to an integer here? Thanks again! Commented Aug 16, 2012 at 3:22
  • DaoWen, please post this as an answer and I'll mark as accepted. Thanks again. Commented Aug 16, 2012 at 3:31
  • I re-posted my comment as an answer below, and I'll look at what you said about the return value. Commented Aug 16, 2012 at 3:36

4 Answers 4

3

I can see a quick problem there. Layouts class does not support index based access in any way. So following code will throw an error:

rows = myLayout[numSquares].r;

You might want to create a method in Layouts class that returns its private array myLayouts which further can be used with an index.

And, as DaoWen 7 pointed out, that array needs to be initialized somewhere.

I am writing this from my phone so cannot type much :-P

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

1 Comment

Thank you. I noticed this later, as well.
2

When you mix arrays and generics, strange things happen because of type erasure.

I would probably not make RowCol a generic class, and simply use int for the field types.

3 Comments

That is true! I am someone who only recently jumped back onto Java (from C#). Definition of generics is world apart and I hard time accepting that Java allowd type erasure. Why java, Why??
@Abhinav - because the alternative would have been worse - two groups of mutually incompatible collection classes ... potentially propagating through all APIs that depend on them.
Thank, Taymon. That finished it off. I had tried to do this using int, int earlier, but couldn't make it work that way. Now it works. Thank you very much!
1

Did you initialize your array?

RowCol[] myLayouts = new RowCol[81];

If you don't initialize the array then the myLayouts reference is null and you'll get a NullPointerException when you try to assign to or read from any of the indices.

Update:

The problem you're experiencing with parseInt is stemming from the fact that you're using raw-typed generics in your array. You have type parameters in the declaration of the RowCol class (class RowCol<R, C> has type parameters R and C), but you're not specifying types when you declare instances of RowCol, so they're just defaulting to the raw type which assumes everything is an Object. This should fix it:

@SuppressWarnings("unchecked")
RowCol<Integer,Integer>[] myLayouts = new RowCol[81];
// . . .
myLayouts[20] = new RowCol<Integer,Integer>(5, 4);   // 20 Problems
myLayouts[21] = new RowCol<Integer,Integer>(7, 3);   // 21 Problems
// . . .

The above will generate a warning due to limitations Java puts on generics and arrays, but the @SuppressWarnings annotation should take care of it. You could also avoid this problem altogether by using an ArrayList (which has full generic support) instead of an Array.

3 Comments

new RowCol<Integer,Integer>[81]; is illegal in java 'Arrays holding elements whose type is concrete parameterized type is illegal in java'
Amit Deshpande - I forgot about the stupid limitations Java puts on arrays. I guess I've been using Scala too much.
Thanks. As I mentioned previously, I changed my RowCol class to be generic. as in "class RowCol {...". That took care of my typing problem with converting the objects R and C back to integers at the very end.
0

Please do below

public static void getLayout (int numSquares) {

    int rows;
    int columns;        
    //Create object here currently if i create it then it will be my class specific
    Layouts myLayout = new Layouts();

    rows =myLayout.myLayouts[numSquares].r; //this is where it fails
    columns = myLayout.myLayouts[numSquares].c;
}

class RowCol<R, C> {

    /* Class Variables */
    public final R r;
    public final C c;

    public RowCol(R r, C c) {
        this.r = r;
        this.c = c;
    }

    public R getRow() {return r;}
    public C getCol() {return c;}
}

class Layouts {

    RowCol<Integer, Integer>[] myLayouts = new RowCol[81];

    public Layouts() {

        /* Set the numbers of Rows and Columns for each possible
         * number of squares requested.
         */
            myLayouts[20] = new RowCol<Integer, Integer>(5, 4);   // 20 Problems
            myLayouts[21] = new RowCol<Integer, Integer>(7, 3);   // 21 Problems
            myLayouts[22] = new RowCol<Integer, Integer>(5, 4);   // 22 Problems
            myLayouts[23] = new RowCol<Integer, Integer>(5, 4);   // 23 Problems
            myLayouts[24] = new RowCol<Integer, Integer>(6, 4);   // 24 Problems
            myLayouts[25] = new RowCol<Integer, Integer>(5, 5);   // 25 Problems
            myLayouts[26] = new RowCol<Integer, Integer>(6, 4);   // 26 Problems
            myLayouts[27] = new RowCol<Integer, Integer>(6, 4);   // 27 Problems
            myLayouts[28] = new RowCol<Integer, Integer>(7, 4);   // 28 Problems
            //etc...
    }
}

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.