Results 1 to 3 of 3
  1. #1
    Jarsss is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Method that displays a rectangle

    First post here in the forums. Hello all! Currently in my first programming class, Java. Got some homework thats giving me some issues.


    ....Your task is to write a method that displays a rectangle with a given width and height. For example, if the width is 4 and the height is 2, print

    [][][][]
    [][][][]

    Don't print anything if the width or height is larger than 25.....


    This is what i have so far, i honestly lost right now. I know what i have to do. Which is First read the length, print "[]" time how big the length is. The repeat that for the total height. How exactly do i get there tho?

    import java.util.Scanner;

    public class DrawRects
    {
    /**
    A method to display a rectangle of dimension <tt>height</tt>
    by <tt>width</tt>. If the width or height are > 25, don't
    display anything.
    @param width, the width of the rectangle to be drawn
    @param height, the height of the rectangle to be drawn
    */


    /**
    The main method reads the width and height and invokes your method.
    */
    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);
    int w = in.nextInt();
    int h = in.nextInt();
    displayRectangle(w, h);
    }

    public static void displayRectangle(int w, int h)
    {
    String square = "[]";

    if( w > 25 && h > 25 )
    {
    // java will not compute if values are greater than 25
    }

    else
    {
    int value = 0;
    for(int i = w; i < value; i++)
    for(int n = h; n < value; n++)
    System.out.print("[]");
    }

    }
    }

  2. #2
    Jarsss is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Method that displays a rectangle

    I did figure it out. Will post so if anyone else needs.


    import java.util.Scanner;

    public class DrawRect {

    public static void main( String args[] ) {
    Scanner in = new Scanner(System.in);
    int side, width;
    System.out.print("Please enter a width: ");
    width = in.nextInt();
    System.out.print("Please enter a height: ");
    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.print("Values to big.");
    }
    System.out.println();
    }




    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Method that displays a rectangle

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help me displays an image
    By dinhtrieu08t4 in forum AWT / Swing
    Replies: 4
    Last Post: 10-07-2011, 05:58 PM
  2. button displays image
    By hopelessnoob in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2011, 04:44 PM
  3. Replies: 6
    Last Post: 12-01-2010, 12:29 AM
  4. Touch Displays
    By Ciwan in forum New To Java
    Replies: 4
    Last Post: 10-21-2010, 10:47 AM
  5. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •