Results 1 to 4 of 4
  1. #1
    anotsu is offline Member
    Join Date
    Feb 2008
    Posts
    10
    Rep Power
    0

    Default change the square to triangle java

    hello i have to change the code of a program that draw a square with stars to a triangle square , can someone help me out please ?

    here is the code into 2 files :

    first files --->

    import java.util.Scanner;

    public class Hollow
    {
    // draw a hollow box surrounded by stars
    public void drawHollowBox()
    {
    Scanner input = new Scanner( System.in );

    int stars; // number of stars on a side
    int column; // the current column of the square being printed
    int row = 1; // the current row of the square being printed

    // prompt and read the length of the side from the user
    System.out.print( "Enter length of side:" );
    stars = input.nextInt();

    if ( stars < 1 )
    {
    stars = 1;
    System.out.println( "Invalid Input\nUsing default value 1" );
    } // end if
    else if ( stars > 20 )
    {
    stars = 20;
    System.out.println( "Invalid Input\nUsing default value 20" );
    } // end else if

    // repeat for as many rows as the user entered
    while ( row <= stars )
    {
    column = 1;

    // and for as many columns as rows
    while ( column <= stars )
    {
    if ( row == 1 )
    System.out.print( "*" );
    else if ( row == stars )
    System.out.print( "*" );
    else if ( column == 1 )
    System.out.print( "*" );
    else if ( column == stars )
    System.out.print( "*" );
    else
    System.out.print( " " );

    column++;
    } // end inner while loop

    System.out.println();
    row++;
    } // end outer while loop
    } // end method main
    } // end class Hollow



    second file ----->

    public class HollowTest
    {
    public static void main( String args[] )
    {
    Hollow application = new Hollow();
    application.drawHollowBox();
    } // end main
    } // end class HollowTest



    thank you very much

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    You didn't really explain the specifics of the triangle, so this first example here is a left aligned triangle. I formatted it my way since you didn't properly format the post - please use code tags when posting code. There's many ways to do it... and I fancy for loops in case you didn't notice. :)


    Replace your two while loops with either of the below snippets of code:
    Java Code:
      ...
        for (i = 0; i < stars; i++) {
          for (j = 0; j <= i; j++) {
            System.out.print("*");
          }
          System.out.println();
        }
      ...
    Since your square was hollow, here's a hollow triangle - also left aligned:
    Java Code:
      ...
        for (i = 0; i <= stars; i++) {
          for (j = 0; j <= i; j++) {        
            if (i == stars) {
              for (int x = 0; x<= stars; x++)
                System.out.print("*");
              break;
            } else if (i > 1) {
              System.out.print("*");                    
              for (int k = 0; k < i - 1; k++)
                System.out.print(" ");
              System.out.print("*");      
              break;                               
            } else 
              System.out.print("*");
          }      
          System.out.println();
        }
      ...
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    anotsu is offline Member
    Join Date
    Feb 2008
    Posts
    10
    Rep Power
    0

    Default

    thank you pal !!!! u rock

  4. #4
    iamsandip is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default

    i am getting exception like as - java.util.zip.DataFormatException: invalid bit length repeat
    when passing xml as string into this program xml size is 20k

    public static void main(String[] args)
    {
    String in;
    try
    {
    in=args[0];
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
    in=defaultString;
    }
    String comp;
    System.out.println("Source: "+in);
    comp=ZipUtils.Compress(in);
    System.out.println("Display Compressed: "+comp);
    in=ZipUtils.Decompress(comp);
    System.out.println("Display Decompressed: "+in);
    }
    }

    // Method calling Deflater and inflater as per the requirement
    public static class ZipUtils {

    public static String byteOnly(String trg)
    {
    try
    {
    byte[] buf;
    int l;
    Deflater comp=new Deflater();
    comp.setInput(trg.getBytes());
    comp.finish();

    buf=new byte[8192];
    l=comp.deflate(buf);
    Inflater decomp=new Inflater();
    decomp.setInput(buf,0,l);

    buf=new byte[300000];
    l=decomp.inflate(buf);
    return new String(buf);
    }
    catch(Exception e)
    {
    System.out.println(e);
    return "";
    }


    }


    // Into Compress Method trg passed as an argument and it capturing defaultString passed data

    public static String Compress(String trg)
    {
    try
    {
    byte[] buf;
    int l;
    Deflater comp=new Deflater();
    comp.setInput(trg.getBytes());
    comp.finish();


    buf=new byte[8192];
    l=comp.deflate(buf);

    for(int i=0;i<buf.length;++i)
    {
    // System.out.write(buf[i]);
    }
    System.out.print("\n");

    return new String(buf,0,l);
    }
    catch(Exception e)
    {
    System.out.println(e);
    return "";
    }
    }

    //Decompress Method trg passed as an argument and it capturing compress data
    public static String Decompress(String trg)
    {
    try
    {
    byte[] buf;
    int l;
    Inflater comp=new Inflater();
    comp.setInput(trg.getBytes());
    comp.finished();

    buf=new byte[300000];
    l=comp.inflate(buf);

    for(int i=0;i<buf.length;++i)
    {
    // System.out.write(buf[i]);
    }
    System.out.print("\n");

    return new String(buf,0,l);
    }
    catch(java.util.zip.DataFormatException ex)
    {
    System.out.println(ex);
    return "";
    }
    }
    }

    }

Similar Threads

  1. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 03:08 PM
  2. Replies: 0
    Last Post: 04-12-2008, 09:39 PM
  3. assignement change the java screensaver
    By anotsu in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:28 AM
  4. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 12:23 PM
  5. How to change the default Java
    By integrator in forum New To Java
    Replies: 4
    Last Post: 02-01-2008, 04:19 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
  •