Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Homework assignment (beginner)

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework assignment (beginner)

    I'm taking a course and I'm in about my 3rd week, totally new to this otherwise.

    The assignment was as follows: Including a do-while statement, create a program that asks for user input for an integer, then prints a diamond to the console based on that integer. For example, if the integer is 5, it should be 5 lines into the middle.

         *
        * *
       *   *
      *     *
     *       *
      *     *
       *   *
        * *
         *

    Without further delay, here is what I've come up with for the first half of the diamond only. I'm aware it's so far from optimal that it's probably hilarious, but I've been struggling with this for a long time now and want to finish this if it can work.

    The comments should explain my methodology--and lastly, I greatly appreciate any guidance.

    Oh, I should say my result looks like this... the spaces aren't happening at the beginning.

    *
    * *
    *   *
    *     *
    *       *

     
    import javax.swing.JOptionPane;
     
    public class doWhile
    {
     
        public static void main(String[] args)
    {
    	String symbol = "*", spacer = " ", gap = " ";
     
    	String uinput = JOptionPane.showInputDialog("Enter an integer.");
    	int size = Integer.parseInt(uinput);
    	int spaceCount = size;
    	int gapCount = 1;
     
    // This first 'for' statement places the first asterisk. I do this separately because
    // it makes it easier later on when there is a 'gap' in between each asterisk, which
    // has to go up by 2 in the loops.
     
    	for (int a = 1; spaceCount == 0; spaceCount--);
    	{
    		System.out.print(spacer); 		// Prints a space until the count is 0.
    	}
    	System.out.print(symbol); 			// Prints the first asterisk in the line.
     
    	spaceCount = size;  				// Resets the spaceCount to be the size again.
     
    	for (int b = 1; spaceCount == 0; spaceCount--);
    	{
    			System.out.println(spacer); // Prints a space until the count is 0.
    	}
    	size--; 							// Size gets reduced as the 1st diamond line is complete.
     
    	spaceCount = size;					// spaceCount becomes new size.
     
     
    		do
    			{
     
    				// Same methodology occurs for the first half of the diamond.
    				// gapCount is introduced to go up by 2 with each line, as this
    				// is what happens internally in a diamond shape.
     
    				gapCount = gapCount + 2;
     
    				for (int c = 1; spaceCount == 0; spaceCount--); // Spaces decrease as gap size increases
    				{
    					System.out.print(spacer);  // Prints spacer, repeats until spaceCount is 0.
    				}
     
    				spaceCount = size; // Resets spaceCount to the size.
     
    				System.out.print(symbol); // Prints 1st asterisk in line.
     
    				for (int d = 1; gapCount > 1; gapCount--)
    					{
    						gap = gap + ("  "); // Gap becomes single space, plus 2 additional spaces.
     
    					}
     
    				System.out.print(gap); // Prints the gap between asterisks
     
    				System.out.print(symbol); // Prints 2nd asterisk in line
     
    				for (int e = 1; spaceCount == 0; spaceCount--); // Spaces decrease as gap size increases
    				{
    				System.out.println(spacer);  // Prints spacer, repeats until spaceCount is 0.
    				}
     
    				size--; // Size is reduced by one more line.
     
    				spaceCount = size; // Spacecount becomes new size.
     
     
    			} while (size > 0); // Whole process runs until size no longer has a value.
     
    System.exit(0);
    		}
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework assignment (beginner)

    	for (int a = 1; spaceCount == 0; spaceCount--);
    No ending ; here. It ends the WHOLE if statement
    What is the variable a used for?

    How does the condition part of the if statement control the looping? (spaceCount == 0)

    Also look at your use of print vs println.

    Other ideas to make debugging easier. Instead of print blanks, print a character:
    spacer = "S", gap = "G";

    gap = gap + ("gg");
    Then you can see better what is happening.
    Last edited by Norm; May 30th, 2011 at 09:21 AM.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework assignment (beginner)

    Thanks for the help!

    The print vs println statements were done intentionally because I only want it to go down to the next line after finishing the final spacer in each line. That spacer might be unnecessary since you don't need to have a space after the 2 asterisks have been printed, but for whatever reason I included it. So then it goes down a line, followed by several print commands, followed by a println again to go down. Did I do this incorrectly?

    I will take your advice for entering a value to debug with and see if that sparks something.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework assignment (beginner)

    I've been trying to debug it all morning with your suggestions and while they're helping, I'm still stuck. So I tried to narrow it down using just my initial line to print x number of spaces, an asterisk, and x number of spaces again.

    Can anyone take a look at this and let me know why this outputs a single asterisk with no spaces (spaces are represented by 's', gaps are 'g' but don't come into play yet).

    for (int a = 1; spaceCount < 1; spaceCount--) <-- my problem may be here, but I'm not sure why. The initialization is just to get it going, so I thought a = 1 would be fine. I checked and spaceCount does = the user's input, so if I test with the number 5 it does equal 5 immediately before this 'for' loop runs. So in theory spaceCount should be decrementing until it becomes 0, and each time it should be printing an 's'--why is this not so?


        public static void main(String[] args)
    		{
    			String symbol = "*", spacer = "s", gap = "g";
     
    			String uinput = JOptionPane.showInputDialog("Enter an integer.");
    			int size = Integer.parseInt(uinput);
    			int spaceCount = size;
    			int gapCount = 1;
     
    			JOptionPane.showMessageDialog(null, spaceCount);
     
    		// This first 'for' statement places the first asterisk. I do this separately because
    		// it makes it easier later on when there is a 'gap' in between each asterisk, which
    		// has to go up by 2 in the loops.
     
    			for (int a = 1; spaceCount < 1; spaceCount--)
    			{
    				System.out.print(spacer); 		// Prints a space until the count is 0.
    			}
     
    			System.out.print(symbol); 			// Prints the first asterisk in the line.
     
    			spaceCount = size;  				// Resets the spaceCount to be the size again.
     
    			for (int a = 1; spaceCount < 1; spaceCount--)
    				{
    					System.out.print(spacer); 		// Prints a space until the count is 0.
    			}

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework assignment (beginner)

    for (int a = 1; spaceCount < 1; spaceCount--)

    Look at the condition for this if statement. If true the loop goes around again.
    What is the value of spaceCount when you enter the for () statement
    I think you want to go around the loop while spaceCount is > 0

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework assignment (beginner)

    I see what you're saying--when I read an explanation about the termination point of a for statement, I interpreted it so that the loop will continue until the middle boolean occurs, which is why I had it set to < 1, so that it would continue subtracting from spaceCount until spaceCount was 0. I see how it works now, and fortunately the first half of my diamond now completes with those adjustments. Should be able to do the same with some other changes and get the bottom half going.

    Thanks so much for this!

Similar Threads

  1. Array Homework assignment
    By lich0802 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 27th, 2011, 08:17 PM
  2. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  3. Beginner needs help with simple java assignment.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 07:53 PM
  4. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM
  5. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM