a *little* bit of coding help needed (java)

Discussion in 'OT Technology' started by Tk, Nov 19, 2003.

  1. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,086
    Likes Received:
    285
    Location:
    nw iowa
    im finishing up a java lab, and basically we had to write a program that takes input and draws shapes according to the users selections. you can draw a square, triangle, diamond, or rectangle, and the user can say which character to use to draw it. ive got the rectangle, square, and triangle done... but the diamond is the hard part.

    square (4x4):
    ####
    ####
    ####
    ####

    Code:
     
    	private void drawRectangle(int rLength, int rHeight, String rChar)
    	{
    		int currentLine = 0, currentChar = 1;
    		do
    		{
    			do
    			{
    				System.out.print(rChar);
    				if(currentChar == rLength)
    				{
    					System.out.print("\n");
    					currentChar = 1;
    				}
    			}while(currentChar < rLength);
     
    			currentLine++;
    		}while(currentLine < rHeight);
    	}
    
    triangle (width of 5):
    #
    ##
    ###
    ####
    #####

    Code:
    	private void drawTriangle(int tHeight, String tChar)
    	{
    		int tLine = 1, tCharNum = 1;
     
    		while(tLine <= tHeight)
    		{
    			while(tCharNum <= tLine)
    			{
    				System.out.print(tChar);
    			}
    			System.out.print("\n");
    			tLine++;
    			tCharNum = 1;
    		}		 
    	}
    
    Diamond (width of 5 (must be an odd #, as well)):

    ive been thinking about it for awhile and i dont even know where to really start on this one... just looking for a bit of help :o
     
  2. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    You need to put some spaces before the rows (except the middle one). Let's take your example of 5 for the width. Your middle row is 2 (starting from zero) so we know that row doesn't need any spaces added to it since it will be the longest row. Row 1 will need one space added and row 0 will need two spaces. Also, Row 3 will need one space and row 4 will need two again. To get the desired results, you can subtract the row index from the middle row to figure out the number of padding spaces (use the absolute value to get rid of negative numbers). For example, middle row number (2) - current row (0) = pad spaces (2). The calc will work for all of the rows. Give that a try and your program should be complete.
     

Share This Page