Search code examples
javaarrayscons

Java – Working with 2 dimensional array and constructor (Beginner)


This post is going to be really long, but I would be so glad if the community can help me.

This is a question from a former class test. I have to expect a similar one this time and I'm currently stuck. I'm new to Java and its concepts. The result of the test should look like this:

The result

I'm facing multiple problems at once. I will try to be as specific as I can. First I will explain the task, which has given conditions that can't be changed.

The task Create an array field with 2 dimensions. The field should work with various sizes. Besides the size, the array field should contain the following pattern(see picture).

1.) Create an constructor that creates a rectangle array field from the parameter passed in, with to extra conditions.

1.1) If the parameter is less than 5, the field should be forced to appear as a 5/5 field.

1.2) If the paramter is more than 5 it should always appear as an n/n array.

public class Pattern {
 char[][] field;

public Pattern(int n) {
  // Here goes my code for creating the field
}

2.) Write a method that fills the constructor.

public void fillArray() {
  // Here goes my code.
}

My approach so far

public Pattern(int n) {
field = new char[n][n];              
int i, j;

if (field.length < 5) {
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= 5; j++) {
            // ? what to do with the array here?
        }
        System.out.println();
    }
}


public void fillArray() {
// no fu**ing idea... 
} 


public static void main (String[]args){
    Pattern cross = new Pattern(2);  
    cross.fillArray();
    System.out.println(cross);
}

Questions

1.) What value should be returned in the constructor?

2.) How can I access the object in the method and set a for loop, that gets the predefined field size? Should I use this ?

I'm sorry for my bad understanding on how to pass array information and execute these correctly.


Solution

  • You are putting what should be in fillArray in the constructor. The constructor should just be this:

    public Pattern(int n) {
        if (n < 5) {
            field = new char[5][5];
        } else {
            field = new char[n][n];
        }
    }
    

    And the two for loops should be in the fillArray method:

    public void fillArray() {
        // you should loop until field.length and field[x].length
        for (int x = 0; x < field.length; x++) {
            for (int y = 0; y < field[x].length; y++) {
                // ...
            }
        }
    }
    

    Try to write your own logic of how to fill in the array.


    If you are stuck, here is my solution:

    for (int x = 0 ; x < field.length ; x++) {
        for (int y = 0 ; y < field[x].length ; y++) {
            /*
            The '1' area can be represented by these inequalities:
            x - y < 0
            x + y < array.length - 1
            The '2' area can be represented by these inequalities:
            x - y < 0
            x + y > array.length - 1
            The '3' area can be represented by these inequalities:
            x - y > 0
            x + y > array.length - 1
            The '4' area can be represented by these inequalities:
            x - y > 0
            x + y < array.length - 1
            Everywhere that does not satisfy any of the inequalities are *s
             */
            int xMinusY = x - y;
            int xPlusY = x + y;
            if (xMinusY < 0 && xPlusY < field.length - 1) {
                field[x][y] = '1';
            } else if (xMinusY < 0 && xPlusY > field.length - 1) {
                field[x][y] = '2';
            } else if (xMinusY > 0 && xPlusY > field.length - 1) {
                field[x][y] = '3';
            } else if (xMinusY > 0 && xPlusY < field.length - 1) {
                field[x][y] = '4';
            } else {
                field[x][y] = '*';
            }
        }
    }
    
    // ...
    
    // printing the array out:
    
    for (int x = 0 ; x < field.length ; x++) {
        for (int y = 0 ; y < field.length ; y++) {
            System.out.print(field[x][y]);
        }
        System.out.println();
    }