Results 1 to 6 of 6
  1. #1
    jomobro is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Printing a rectangle with a hollow centre problem

    Hi guys
    I'm both new to Java and this forum, so apologies if I do anything wrong here

    I'm working through the Java Just in Time textbook by John Latham, by recommendation
    So far, it's been a very good guide and the set problems quite fun to solve
    However, I'm stuck on this problem: 6.4.2, page 28 http://studentnet.cs.manchester.ac.u...-with-labs.pdf

    So here's my code as it is right now:
    Java Code:
    public class PrintHoledRectangle
    {
       public static void main(String[] args)
       {
         int width = Integer.parseInt(args[0]) / 2 * 2 + 1;
         int height = Integer.parseInt(args[1]) / 2 * 2 + 1;
         int centre = width * height / 2 + 1;
         int index = 0;
         
           for (int row = 1; row <= height; row = row++)
           {
             for (int column = 1; column <= width; column = column++)
             {
               index = index++;
               if (index != centre)
                 System.out.print("[_]");
               else
                 System.out.print("   ");
             }
               
             System.out.println();
           }
       }
    }
    The width of the rectangle to be printed is given by the first argument, the height by the second
    The centre tile ("[_]") of the rectangle (to be printed as " ") is given by the formula: ((width*height)/2)+1, assuming you count each tile left-to-right, top-to-bottom
    My thinking is that the programme keeps a running count (seen as 'index') so that, when the centre tile is to be printed, it outputs " ", otherwise "[_]", hence the if-else statement

    Currently, it prints "[_]" to infinity

    Could anyone please point me in the right direction? Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Printing a rectangle with a hollow centre problem

    What does the code print when it is executed? Wrap the output in code tags to preserve the interior spaces.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Printing a rectangle with a hollow centre problem

    He, this is a good one. Lets start with a little debugging tip to help you narrow down the source of problems yourself. Try printing out row and column in the loops and see what you get. The reason why it loops infinitely should then become clear, and then there will be second puzzle to solve.

    Java Code:
       public static void main(String[] args)
       {
         int width = Integer.parseInt(args[0]) / 2 * 2 + 1;
         int height = Integer.parseInt(args[1]) / 2 * 2 + 1;
    	 
         int centre = width * height / 2 + 1;
         int index = 0;
          
           for (int row = 1; row <= height; row = row++)
           {
             for (int column = 1; column <= width; column = column++)
             {
    		 System.out.println("R: " + row + ", C:" + column);
             }
           }
       }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jomobro is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Re: Printing a rectangle with a hollow centre problem

    Quote Originally Posted by Norm View Post
    What does the code print when it is executed? Wrap the output in code tags to preserve the interior spaces.
    Quote Originally Posted by gimbal2 View Post
    He, this is a good one. Lets start with a little debugging tip to help you narrow down the source of problems yourself. Try printing out row and column in the loops and see what you get. The reason why it loops infinitely should then become clear, and then there will be second puzzle to solve.

    Java Code:
       public static void main(String[] args)
       {
         int width = Integer.parseInt(args[0]) / 2 * 2 + 1;
         int height = Integer.parseInt(args[1]) / 2 * 2 + 1;
    	 
         int centre = width * height / 2 + 1;
         int index = 0;
          
           for (int row = 1; row <= height; row = row++)
           {
             for (int column = 1; column <= width; column = column++)
             {
    		 System.out.println("R: " + row + ", C:" + column);
             }
           }
       }
    Wahay, got it!
    Seems I didn't understand how to use the "x++" operator

    Thanks muchly for the help, one last thing:

    I'm not sure I fully understand how my misuse of the operator messed up the programme so
    "x=x++" translates to "x=x=x+1", right? So is it that java causes that to be read as "x=x", due to the dominance of a term closer to the start of the line, and disregards the "=x+1" bit?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Printing a rectangle with a hollow centre problem

    Good job figuring it out. The 'problem' here is that you are using the 'post-increment operator' which requires you to know -exactly- how that thing behaves - as you have already found out, it works in mysterious ways that often lead to wrong assumptions. The code isn't really translated to what you say, its more that the ++ operation you are performing is simply 'lost in translation'. There are some good reads to be found about it on the net about that if you google 'java post increment operator'. Stackoverflow is a good source of answers:

    post increment operator java - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jomobro is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Re: Printing a rectangle with a hollow centre problem

    Sweet! Thanks very much for all the help :)

Similar Threads

  1. Help with printing hollow squares?
    By AZjava_newbie in forum New To Java
    Replies: 2
    Last Post: 10-22-2012, 05:14 AM
  2. Hollow square n x n logic issue - help!
    By xcaldk74 in forum New To Java
    Replies: 2
    Last Post: 04-04-2012, 08:48 PM
  3. Rotating Rectangle about its Centre
    By Lance in forum Java 2D
    Replies: 6
    Last Post: 02-28-2011, 11:28 PM
  4. problem with adding circle and rectangle
    By nicnicnic in forum Java 2D
    Replies: 3
    Last Post: 11-16-2009, 06:13 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
  •