1 Replies - 197 Views - Last Post: 29 November 2012 - 05:20 PM Rate Topic: -----

#1 MalpaSzalona  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 8316
  • View blog
  • Posts: 31,836
  • 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();
        }
     }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1