A method to display a Rectangle.

Page 1 of 1

1 Replies - 405 Views - Last Post: 29 November 2012 - 05:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302112&amp;s=946d8374beceade3a5faf5d1c8c5007d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 MalpaSzalona

Reputation: 0
• Posts: 9
• Joined: 16-November 12

A method to display a Rectangle.

Posted 29 November 2012 - 05:09 PM

I'm a little stuck if someone can help, I'm supposed to write a method to display a rectangle. I'm pretty close to getting it right, I just need some guidance on what to modify in my code to make it pass all the parameters.

The first parameter passes and the second fails and returns this....
test2.in
2*4
pass;*Actual:*[][][][]
****Expected:*[][]
pass;*Actual:*[][][][]
****Expected:*[][]
fail;*Actual:*
****Expected:*[][]
fail;*Actual:*
****Expected:*[][]

The third test parameters fails also and returns this...

Input*test3.in
4*2
pass;*Actual:*[][]
****Expected:*[][][][]
pass;*Actual:*[][]
****Expected:*[][][][]
fail;*Actual:*[][]
****Expected:*
fail;*Actual:*[][]
****Expected:*

This is the rest of the parameters that it fails...

Input*test4.in
25*1
pass;*Actual:*[]
****Expected:*[][][][][][][][][][][][][][][][][][][][][][][][][]
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
fail;*Actual:*[]
****Expected:*
Input*test5.in
3*26
pass********:****
Input*test6.in
26*3
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*
fail;*Actual:*[][][]
****Expected:*

This is the code I used....
```import java.util.Scanner;

public class DrawRects
{

public static void main( String args[] ) {
Scanner in = new Scanner(System.in);
int side, width;

width = in.nextInt();
side = in.nextInt();
int height = side;

displayRectangle(side, width, height);
}
public static void displayRectangle(int side, int height, int width)
{
if( side <= 25 && side > 0)
{
while(height >= 1)
{
width = 1;
while(width <= side)
{
System.out.print("[]");
width++;
while(height >=2 && height <= side-1 && width <= side-1)
{
System.out.print("[]");
width++;
}
}
--height;
System.out.println();
}
}
else
System.out.println();
}
}

```

Is This A Good Question/Topic? 0

Replies To: A method to display a Rectangle.

#2 pbl

• There is nothing you can't do with a JTable

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

Re: A method to display a Rectangle.

Posted 29 November 2012 - 05:20 PM

seems to me a lot complicated for nothing... what about

```     void drawRect(int width, int height) {
for(int row = 0; row < height; ++row) {
for(int col = 0; col < width; ++col) {
System.out.print("[]");
}
System.out.println();
}
}

```