Kochan 2.0 Exercise 8-7 Possible with NSLog?

Discussion in 'Mac Programming' started by mdeh, Jan 16, 2009.

  1. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    Any idea as to how to tackle this using NSLog?

    "
    7:
    Write a method for the Rectangle class called draw that draws a rectangle using dashes and vertical bar characters. The following code sequence


    Rectangle *myRect = [[Rectangle alloc] init];
    [myRect setWidth: 10 andHeight: 3];
    [myRect draw];
    [myRect free];
    "
    Would look something like this.
    ----------
    | |
    | |
    | |
    ----------

    (Cannot get the formatting correct, but I think you get the idea)

    Version 1.0 used printf, but in keeping with the Authors guidelines, printf is not used. NSLog prints each character on a new line.....I think :)
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    NSLog adds a newline at the end of each invocation. Just build a NSMutableString with each line or the whole shape and print it with one NSLog. If that's beyond the current progress in the book, I'm sure there's something similar you can do with what's been covered so far.

    -Lee
     
  3. TotalLuck macrumors newbie

    TotalLuck

    Joined:
    Jan 3, 2009
    Location:
    Moreno Vallley
    #3
    i couldn't figure it our either.

    I had to use printf to get-r-done. If you found a way to use NSLog please help me out.

    Code:
    -(void) draw
    {
    	int count1 = width;
    	int count2 = height;
    	
    	while (count1 > 0)
    		{
    			printf ("_");
    			--count1;
    		}
    	
    	printf("\n");
    
    	while (count2 > 0)
    		{
    			count1 = width;
    			printf("|");
    			while (count1 > 1)
    			{
    				printf (" ");
    				--count1;
    			}
    			printf("|");
    			--count2;
    			printf("\n");
    		}
    	
    	count1 = width;
    		
    	while (count1 > 0)
    		{
    			printf ("_");
    			--count1;
    		}
    	
    }
    
    thanks
     
  4. skochan macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #4
    Oops! You're right! In the first edition I assumed printf would be used (NSLog isn't taught by that point yet). I hadn't thought about how to use NSLog to do it in the second edition with what's been learned to that point.

    I can get the sides of the rectangle with one NSLog call like this:

    Code:
    NSLog (@"|%*c|\n", width - 2, ' ');
    but even that uses a formatting feature I didn't teach in the text (variable-width format specified by the corresponding argument to NSLog/printf). And I don't know of a way to do the top and bottom of the rectangle with a single NSLog call, given what you've learned. [There is a way based on an assumption of the maximum width of a rectangle--not very clean at all, so I'm not going to even share it! :)]

    Thanks for pointing this out and for trying to solve it! I'll make note of this on my errata page.

    Cheers,

    Steve Kochan
     

Share This Page