3 Replies - 1974 Views - Last Post: 11 December 2007 - 07:23 AM Rate Topic: -----

#1 hellojosef   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 07

Help using for-loops and methods

Post icon  Posted 10 December 2007 - 06:01 PM

I'm trying to write a program in which the user inputs a character and an integer (we'll say n). I want the output to be a hollow square of a size (n x n) comprised of the character followed by a hollow rectangle (n x 2n), also made up of the user's chosen character. The integer obviously must be greater than zero, and in the event an integer <= 0 is entered, then the prompt must be reissued. I'm trying to use methods and for-loops to handle most of the code, but I'm having a heck of time sorting it all out. Here's what I have so far.

   import java.util.Scanner;

	public class Methods
   {
	   public static void main(String[] args)
	  {
			giveInstructions();

			Scanner kbd = new Scanner(System.in);

			final String getSize = "Enter the size (>0): ";

			final String getCharacter = "Enter the character: ";

			System.out.print(getSize);

			int size = kbd.nextInt();

			kbd.nextLine();

			System.out.print(getCharacter);

			char ch = (kbd.next()).charAt(0);

			drawSquare(size, ch);
			
	  }
		 
		 
		 public static void giveInstructions()
		{
			System.out.println("Enter an integer and a character, " +
									 "and get two figures drawn");
			newLine();
			newLine();
									 
		}
		 
		 

//		 public static int getSize(Scanner kbd)
//		{
//			final String getSize = "Enter the size (>0): ";
//			System.out.print(getSize);
//			int size = kbd.nextInt();
//			kbd.nextLine();
//			return n;
//		}
//		 
//		 
//		 public static char getCharacter(Scanner kbd)
//		{
//			final String getCharacter = "Enter the character: ";
//			System.out.print(getCharacter);
//			char shape = (kbd.next()).charAt(0);
//			return ch; 					
//		}
	 
		 public static void drawSquare(int size, char ch)
		{			
			for(int i=1; i<=size; i++)
		  {
				  drawSolidLine(size, ch);
				drawHollowLine(size, ch);
		  }
		}
		 
		 
		 public static void drawSolidLine(int size, char ch)
		{
			for(int i=1; i<=size; i++)
		  {
				  System.out.print(ch);
		  }	
		}
		 
		 
		 public static void drawHollowLine(int size, char ch)
		{
			for(int i=1; i<=(size - i); i++)
		  {
				  System.out.print(ch);		
		  }
		}
		 
		 
		 public static void newLine()
		{
			System.out.println();
		}
	

   } 





I know that the drawSquare method needs to call the drawSolidLine and drawHollowLine methods to make the hollow shapes, but the logistics of that combined with the for-loops have me stumped. I'm grateful for any insights with that matter.

The code that is documented is my attempt to handle the int and char inputs via methods (thus far unsuccessfully). A hint with that would be great as well. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Help using for-loops and methods

#2 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7233
  • View blog
  • Posts: 15,071
  • Joined: 16-October 07

Re: Help using for-loops and methods

Posted 10 December 2007 - 07:53 PM

Ok, here's the hint.

The following code will print a basic solid square:
for(int y=0; y<outerHeight; y++) {
	for(int x=0; x<outerWidth; x++) {
		System.out.print('X');
	}
	System.out.println();
}



The trick is not draw lines, that's a red herring. The trick is deciding to draw a space instead.
for(int y=0; y<outerHeight; y++) {
	for(int x=0; x<outerWidth; x++) {
		if (isInHollowArea(x, y)) {
			System.out.print(' ');
		} else {
			System.out.print('X');
		}
	}
	System.out.println();
}



Good luck.
Was This Post Helpful? 0
  • +
  • -

#3 hellojosef   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 07

Re: Help using for-loops and methods

Posted 10 December 2007 - 10:13 PM

Ok. First of all, thank you. I'm definitely further along than I was. Unfortunately, the hollow concept is still eluding me. I don't understand the if-else conditionals and especially the "isInHollowArea(x, y)" method. I gather I'd have to create that method, but I'm not sure what I'd put in it. Here's where I am now:

		 public static void drawSquare(int size, char ch)
		{			
			
			
			for(int i=1; i<=size; i++)
		  {
				for(int j=1; j<=size; j++)
			  {	
				drawHollowLine(size, ch);
				}	
		   newLine();
		  }
			drawSolidLine(size, ch);
			newLine();
			newLine();
			
			
			for(int i=1; i<=size*2; i++)
		  {
				for(int j=1; j<=size; j++)
			  {	
				drawHollowLine(size, ch);
				}	
		   newLine();
		  }
			drawSolidLine(size, ch);
			newLine();
		}
		 
		 
		 public static void drawSolidLine(int size, char ch)
		{
			for(int i=1; i<=size; i++)
		  {
				  System.out.print(ch);
		  }	
		}
		 
		 public static void drawHollowLine(int size, char ch)
		{
			System.out.print(ch);
		}
		 

				 public static void newLine()
		{
			System.out.println();
		}
	

   }




Am I on the right track? I feel like I just need to figure out some relationship between "i" and "size" to create a for-loop within the drawHollowLine method, but I don't know where to begin. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,015
  • Joined: 11-December 07

Re: Help using for-loops and methods

Posted 11 December 2007 - 07:23 AM

Assuming the character is *...

In a solid line you have a number of * equal to size.
In a hollow line you need the same number of characters but only 2 are *. The rest are spaces. This means the number of spaces is (size - 2). You can use that to determine how long your for loop should last.

You can apply the same logic to the lines: 2 solid lines and (size - 2) hollow lines.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1