Need help with rectangle program!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Carsomyr
    New Member
    • Oct 2007
    • 2

    #1

    Need help with rectangle program!

    I'm assigned to create a rectangle with inputs of height and width using the drawLine method and loops using asterisks(*).

    I've done this without methods on a triangle, but I can't figure this one out.

    Please help! thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Can we please see your code (not methods) dealing with triangles?

    kind regards,

    Jos

    Comment

    • Carsomyr
      New Member
      • Oct 2007
      • 2

      #3
      Actually, I have the code for a square program that does similar to the same thing.

      Rectangle is more confusing because I need to get 2 inputs, height and width, and determine the rectangle from there, which I'm stuck at, if someone can check out the square code and figure something out for a rectangle. Here it is..


      [CODE=java]import java.util.Scann er;

      /*************** *************** *************** *
      * *
      * Prints a square of size *
      * given by the user. *
      * Uses the drawLine() method. *
      * *
      *************** *************** *************** */

      public class Square {

      public static void main(String[] args) {

      int squareSize;

      do {

      squareSize = getSquareSize() ;
      drawSquare(squa reSize);
      System.out.prin tln();

      } while (squareSize >= 0);

      }

      /*
      * Prompts user to input number
      * returns input as size of square to print
      */
      public static int getSquareSize() {

      int squareInput;
      Scanner myScanner = new Scanner(System. in);
      System.out.prin t("Enter square size (negative to exit): ");
      squareInput = myScanner.nextI nt();

      return squareInput;

      }

      /*
      * Prints a square of given size
      */
      public static void drawSquare(int size) {

      for (int i=1; i<=size; i++) {

      drawLine(size);
      System.out.prin tln();

      }

      }

      /*
      * Prints a line of given length
      */
      public static void drawLine(int length) {

      for (int i=1; i<=length; i++)
      System.out.prin t("*");

      }

      }[/CODE]
      Last edited by Ganon11; Oct 5 '07, 02:19 AM. Reason: Please use the [CODE] tags provided.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by Carsomyr
        I'm assigned to create a rectangle with inputs of height and width using the drawLine method and loops using asterisks(*).

        I've done this without methods on a triangle, but I can't figure this one out.

        Please help! thanks
        Have a look at this code ...........

        [code=java]
        int length = 100, height = 200;
        for(int i=0;i<length;i+ +)
        System.out.prin tln("*");
        for(int i=1;i<height-1;i++){
        System.out.prin tln("*");
        for(int j=1;j<length-1;j++)
        System.out.prin tln(" ");
        System.out.prin tln("*");
        }
        for(int i=0;i<length;i+ +)
        System.out.prin tln("*");
        [/code]

        Enjoy this code.
        Good Luck !

        Debasis Jana

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          Have a look at this code ...........

          <snip>

          Enjoy this code.
          Good Luck !

          Debasis Jana
          Don't spoonfeed code.

          kind regards,

          Jos

          Comment

          Working...