1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Simple Java Quest

Discussion in 'Mac Programming' started by MBP123, Nov 12, 2007.

  1. macrumors regular

    #1
    To print out this output

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

    right now i have the top row and the left aligned stars, but cant get the right aligned ones.

    Code:
    int HorizLine, VertLine;
            
            for (HorizLine=1; HorizLine<=1; HorizLine++)
    	   {
    		for (VertLine=1; VertLine<=10; VertLine++)
    				{
    				System.out.print("*");
    				}
    			System.out.print("");
    			}//row of solid asteriks
            
            for (HorizLine=1; HorizLine<=4; HorizLine++)
               {
                    for (VertLine=1; VertLine<=1; VertLine++)
                                    {
                                    System.out.print("\n");
                             System.out.print ("*");//left aligned asteriks
    
    
    could someone please steer me in the right direction as to how to get them to be in line with the other side? thanks
     
  2. macrumors regular

    #2
    well the image didnt display properly in the forum....but its supposed to the one row of stars in line with the first asterisk, and the other line of asterisks in line with the last asterisk of the top row.
     
  3. Moderator

    robbieduncan

    Staff Member

    #3
    Well, if you can assume fixed width fonts (seems the only way doing what you are doing) I would simply print one "*" then 8 " " then another "*".
     
  4. macrumors regular

    #4
    no its not fixed because the user enters the width by inputting a digit earlier in the program. so it can be anything between 5 and 20.

    so its more like

    Code:
    for (HorizLine=1; HorizLine<=1; HorizLine++)
    	   {
    		for (VertLine=1; VertLine<=width; VertLine++)
    				{
    				System.out.print("*");
    				}
    			System.out.print("");
     
  5. Moderator

    robbieduncan

    Staff Member

    #5
    OK. If I assume your width is stored in an int called width:

    Code:
    
    int width;
    
    <get user to input and validate storing in width>
    
    int i,j;
    
    for (i=0;i<width;i++)
    {
    System.out.print("*");
    }
    System.out.print ("\n");
    
    for (j=0;j<4;j+)
    {
    System.out.print("*");
    for (i=0;i<(width-2);i++)
    {
    System.out.print(" ");
    }
    System.out.print("*\n");
    }
    
    
     
  6. macrumors 68020

    Gelfin

    #6
    I'm assuming this is a homework assignment, so I'm not going to provide an answer, but here are a couple of hints:

    First, learn how to indent correctly. If your instructor isn't knocking at least five points off for that, he should.

    Second, if you recall last time you posted a question like this, I replied with an unconscionably smartassed solution that did the bulk of the work on one line. That solution was terrible because it was unreadable and unmaintainable, but the general approach was valid, and since this is a much simpler problem, you'd do well to consider it:

    You are writing out a square of characters with known height and width, one coordinate at a time. Treat that as a separate problem. Do your nested loops, and inside them call a method whose job it is to figure out whether or not a star should be written at a given X and Y coordinate. Think about the conditions that make a position a "star" position instead of a "space" position. If your method returns true, write a star. If it returns false, write a space.

    In your current problem, the conditions are very simple -- far more so than in that checkerboard+diamond problem. A position should contain a star if it is in the top row, the leftmost column, or the rightmost column (assuming I've understood your assignment correctly). You can easily define a test method for those conditions in a few lines of code.

    If you want to really impress your instructor, make a general class that writes out a matrix of stars (define your test method to always return true) then each time he assigns you one of these problems reuse the general class by deriving a subclass that overrides your test method with one that returns the correct values for the assignment.

    I mean, don't be surprised if he thereafter gives you an assignment that breaks your base class by asking for a matrix with more than two kinds of character in it. Compensating for that possibility upfront is left as an exercise you may easily be forgiven for not wanting to undertake just now.
     

Share This Page