1

Any ideas how to write Java program in a class named Window that produces the preceding figure as output. I have to use nested for loops to print the repeated parts of the figure. I've tried lots of times, no success :(

Write a Java program in a class named Window that produces the preceding figure as output. Use nested for loops to print the repeated parts of the figure. Once you get it to work, add one class constant to your program so that the size of the figure can be changed simply by changing that constant's value. The example output shown is at a constant size of 3, but if you change the constant, the figure should grow larger and wider proportionally.

+===+===+
|   |   |
|   |   |
|   |   |
+===+===+
|   |   |
|   |   |
|   |   |
+===+===+

OK i've got this, but still need to get rid of 3 bottom lines - any idea?

    for (int i = 1; i <= 3; i++) {
        for (int plus = 1; plus <= 2; plus++) {
            System.out.print("+");
        for (int shave = 1; shave <= 3; shave++) {
                System.out.print("=");
            }
            }
        System.out.print("+");
            System.out.println();
    for (int time = 1; time <= 3; time++) {
         for (int kav = 1; kav <= 3; kav++) {
                 System.out.print("|");
             for (int rev = 1; rev <= 3; rev++) {
                 System.out.print(" ");
             }
             }
         System.out.println();
        }
    }

}

alya
  • 27
  • 1
  • 6

7 Answers7

3

I think this is what you are Looking for:

        final int BLOCK_SIZE = 2;
        for(int i=0; i<1; i++){
            System.out.print("+===+");
            for(int j=0; j<1; j++){
                System.out.println("===+");
                for(int k=0; k<BLOCK_SIZE; k++){
                    System.out.println("|   |   |\n|   |   |\n|   |   |");
                    for(int l=0; l<1; l++){
                        System.out.println("+===+===+");
                    }
                } System.out.println();
            }
        }
Johnydep
  • 6,027
  • 20
  • 57
  • 74
1

I was going through random questions and I found no one actually answered correctly by correcting the code you posted. So here I post my code with nested for loops and it satisfies all your criteria, giving the correct output.

int BLOCK_SIZE = 4;
for (int i=1; i<BLOCK_SIZE; i++){
    for(int j=1; j<BLOCK_SIZE; j++){
        System.out.print("+===");
    }
    System.out.println("+");
    for(int k=0; k<3; k++){
        for(int j=1; j<BLOCK_SIZE; j++){
            System.out.print("|   ");
        }
        System.out.println("|");
    }
}
for(int j=1; j<BLOCK_SIZE; j++){
        System.out.print("+===");
    }
System.out.println("+");

Another approach using some if statements

  • Here I make use of the fact that every (4n+1) line has '+' replacing '|' and '=' replacing 'whitespace'

    int BLOCK_SIZE = 5;
    int length = BLOCK_SIZE*4-4;
    char one,two;
    
    for(int i=0; i<=length; i++){
        if(i%4!=0){one='|';two=' ';}
        else{one='+';two='=';}
        for(int j=0; j<=length; j++){
            if(j%4==0) {
            System.out.print(one);
            }
            else {
            System.out.print(two);
            }
        }
        System.out.println(); 
    }
    

Granted it's a homework problem, but we can have fun cracking them even if they serve no real life purpose. Such is the happiness that can be gotten from coding! :)

banjoSas
  • 184
  • 1
  • 10
1

Took sometime to achieve your expected result, see if it works for you?

public class Homework
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
            for (int line = 1; line <= 3; line ++)
            {

                NEWLINE:

                    for (int plus = 1; plus <= 3; plus++)
                    {
                        System.out.print("+");
                        if (plus == 3) 
                        {
                            for (int k = 1; k <= 3; k++)
                            {
                                if (line == 3)
                                {
                                    break NEWLINE;
                                }
                                System.out.println("");
                                System.out.print(" |         |          |");
                                if (k == 3)
                                {
                                    System.out.println();
                                    break NEWLINE;
                                }
                            }
                        }

                        for (int eq = 1; eq <= 6; eq++)
                        {
                            if (eq % 4 == 0)
                            {                           
                                break;
                            }
                            System.out.print("=");
                        }

                    }
            }
    }
}
Jasonw
  • 5,054
  • 7
  • 43
  • 48
  • nice use of the break statement there – mbatchkarov Feb 05 '12 at 09:36
  • Thank you Jasonw, but I need to use only nested for loops – alya Feb 05 '12 at 18:51
  • In general it works, but how can I change constant height if I have this line: System.out.println("+===+===+");? – alya Feb 05 '12 at 23:06
  • @alya that could probably be your homework as many posted their suggested here that give you many idea how to achieve your expected result in many ways. But probably Johnydep answer come the closest to your requirement. – Jasonw Feb 06 '12 at 02:18
0

This is simplest way.

public class Window {
    public static final int size = 3;

    public static void main(String[] args) {
        for (int x=0; x<2; x++) {
            for (int y = 0; y<2; y++){
                System.out.print("+");
                for (int z=0;z<size; z++) {
                    System.out.print("=");
                }
            }
            System.out.println("+");
            for (int i = 0; i<size; i ++) {
                for (int j = 0; j<2; j++) {
                    System.out.print("|");
                    for (int k = 0; k<size; k++) {
                        System.out.print(" ");
                    }
                }
                System.out.println("|");
            }
        }
         for (int y = 0; y<2; y++){
                System.out.print("+");
                for (int z=0;z<size; z++) {
                    System.out.print("=");
                }
            }
        System.out.println("+");
    }
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Noy
  • 1
0

Here is one way

    int rows = 3;
    int cols = 4;
    String output1 = "";
    String output2 = "";
    for(int j = 0; j < cols; j++)
    {
        output1 += "|   ";
    }
    output1 += "|";
    for(int j = 0; j < cols; j++)
    {
        output2 += "+===";
    }
    output2 += "+";
    for(int i = 0; i < rows*4; i++)
    {
        System.out.println((i % 4 != 0)?output1:output2);
    }
    System.out.println(output2);
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
  • I wrote it "I have to use nested for loops to print the repeated parts of the figure" – alya Feb 04 '12 at 23:16
  • You're going to need to use a conditional statement for this to distinguish between which type of row to display. I updated the code. Its better and uses only 1 `if` statement. – Nick Rolando Feb 04 '12 at 23:31
  • 1
    Really stupid homework: you can replace any if by a for (example, `if(i % 4 !=0)` is the same as `for(int cnt = 0; cnt < 1 && i % 4 != 0; cnt++)`... so just replace the ifs with this pattern ;) – s106mo Feb 04 '12 at 23:37
  • thanks, it looks good, but I need to use 4 for loops, it should be really easy, but I can't figure out it yet :( – alya Feb 05 '12 at 00:12
0

Could be refactored to be more pretty, but will work:

static String[][] array = { { "x", "="}, { "|", " "}};

public static void main(String[] args) {
    for(int y = 0; y < 9; y++) {
        for(int x = 0; x < 9; x++) {
            System.out.print(getSign(y, x));
        }
        System.out.print("\n");
    }
}

private static String getSign(int y, int x) {   
    int modY = y % 4;
    int modX = x % 4;
    return array[getPos(modY)][getPos(modX)];
}

private static int getPos(int mod) {
    return (mod & 1) | ((mod & 2) >> 1);
}

s106mo
  • 1,243
  • 2
  • 14
  • 20
-1
public class Window{
    public static final int size=3;
    public static void main(String[] args){
       for (int p = 1; p <= 2; p++) {
            for (int i = 1; i <=2; i++) {
                System.out.print("+");
                for (int j = 1; j <= size; j++) {
                    System.out.print("=");
                }
            }
                System.out.print("+");
                System.out.println();
            for (int k = 1; k <= size; k++) {
                for (int i = 1; i <= 3; i++) {
                    System.out.print("|");
                    for (int j = 1; j <= size; j++) {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
        for (int i = 1; i <= 2; i++) {
            System.out.print("+");
            for (int j = 1; j <= size; j++) {
                System.out.print("=");
            }
        }
        System.out.print("+");
    }
}
deepak
  • 1
  • 1
    What's this? Probably, you should explain why did you answer 3 y.o. question. Why do you think your answer is better than already posted answers? – default locale Feb 16 '15 at 04:27