0

The program below (thanks to Sundial) computes the area of a rectangle

public class ComputeTheArea {

public static int areaOfTheRectangle (char[][] table, char ch) {
    int[] first = new int[2];
    int[] last = new int[2];

    for (int i=0; i<3; i++) { 
        for (int j=0; j<4; j++) {
               if(grid[i][j]==ch) {
                  first[0] = i;
                  first[1] = j;
               }
        }
    }

    for (int i=2; i>=0; i--) { 
        for (int j=3; j>=0; j--) { 
               if(grid[i][j]==ch) {
                  last[0] = i;
                  last[1] = j;
               }                    
        }
    }

    int answer = ((Math.max(first[0]+1,last[0]+1) - Math.min(first[0]+1,last[0]+1)) *
                  (Math.max(first[1]+1,last[1]+1) - Math.min(first[1]+1,last[1]+1)));

    return answer;
}

However, when it is run, it outputs the wrong answer. I know there is something wrong with the for loop. I'm new in Java and I need your help for me to fix the method. Please and thank you very much!

EDIT: I edited the code to conform to Michael's answer.

2
  • What wrong answer this gives ? Commented Jul 15, 2012 at 11:21
  • Hello @BhavikAmbani! When the user input a, it will return 6. if b, c, d, it will return 0. Commented Jul 15, 2012 at 11:24

2 Answers 2

1

First of all, you don't search all the elements in the matrix with your first loop.
Secondly, you don't break when you found a match.
Also, this approach is a bit flawed. For example, see this matrix:

a b c b 
a _ c d 
x z b a 

Here you wouldn't know which b to stop at at the first row to get the entire b square.

If you instead just loop through the entire matrix once and saves the maximum and minimum (first and last) x and y coordinates, the area can be calculated very easily. See this code:

public static int charArea (char[][] grid, char ch) {
    int[] first = new int[] {100, 100};
    int[] last = new int[] {-1, -1};

    for (int i=0; i<3; i++) { 
        for (int j=0; j<4; j++) {
               if(grid[i][j]==ch) {
                  first[0] = Math.min(i, first[0]);
                  first[1] = Math.min(j, first[1]);
                  last[0] = Math.max(i, last[0]);
                  last[1] = Math.max(j, last[1]);
               }
        }
    }

    int answer = (last[0] - first[0] + 1) * (last[1] - first[1] + 1);

    return answer;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you again, Keppil! I really appreciate your help! Thank you!! Just a question, how do I say "if a variable does not exist in an array, return 0 as the answer" in Java?
You can use Arrays.asList(array).contains(char). In this case though I would just use a boolean found that I would set to true in the for loop when a match is found, and return 0 at the end if found==false.
Thanks! Done as told. But it says that found cannot be resolved to a variable but I did declare `boolean found = true' in the for loop.
You have to declare it as boolean found = false; before the for loops and then set it to true inside the loop, otherwise the variable will only exist inside the loop and the compiler won't find it at the end of the method.
Oh I see. I thought in Java, I can declare variables as I go style haha. But thanks to you, I just realized that it will just exist inside the loop. Thanks much for helping me, Keppil! More power! :)
0

The for loops should probably break once they find the character.

The first for loop sets j=i. That should probably be j=0 instead.

I don't think the length calculation is correct. A 1 should be added to both terms. I.e. the length of something with first=0 and last=3 should be last+1-first=4, not 3 as it would be now.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.