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 7 of 7

Thread: Bigger triangle made up of multiple ones?

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Bigger triangle made up of multiple ones?

    Hey everyone! I have an assigment due next Monday, and in this assignment I must create an ASCII triangle in Java. I've managed to do that, but for extra credit I have to do something like this:



    Here is the code I have so far:

    /**********************************************************************
    * Program: Triangles                                                  
    * Author:                                                             
    *                                                                     
    * Programming Assignment #2                                           
    * Date:                                                               
    *                                                                     
    * This program prompts the user to input the length of the side of an 
    * equilateral triangle that is larger than 2. An equilateral triangle 
    * will then be created using the ASCII character "*" (ASCII graphics) 
    *                                                                     
    * Input:                                                              
    * Side of the equilateral triangle                                    
    *                                                                     
    * Output:                                                             
    * An equilateral triangle made in ASCII graphics                      
    **********************************************************************/
     
    import java.io.*;
    import java.util.*;
     
    public class Triangle
    {    
      public static void main(String[] args) 
      {	
     
      //Variable declarations - i is the main LCV (Loop Control Variable)
      int triangleside = 0, triangleheight = 0, i, j, side;
      String intriangleside = null, option = null;
      boolean Start = true;
     
      //Prepares input
      Console console = System.console();
     
      //Welcomes the user to the program
      System.out.println("Welcome to the ASCII triangle drawing program!");
     
      //Loop that controls whether the main part of the program should start or not
      while(Start==true)
      {
       try 
       { 
        intriangleside = console.readLine("Enter the length of the triangle's side: ");
        triangleside = Integer.parseInt(intriangleside);
        side = triangleside;
     
        //Tells the user that a number greater than or equal to two is required if one is input
        if (triangleside<2)
        {
           System.out.println("Sorry, please choose a number greater than or equal to 2.");
        }	
     
        //Draws the triangle if the value is 2 or above
        else
        {
     
          System.out.println("Here is your triangle: ");
          System.out.println();
     
          //Loop that controls how many lines are made
          for(i=triangleside; i>0; i--)
          {
     
            //Loop that controls how many spaces are in each line
            for(triangleheight=i; triangleheight<side; triangleheight++)
            {	
              System.out.print(" ");
            }
     
            //Loop that controls how many stars are in each line
            for(triangleside=0; triangleside<i; triangleside++)
            {
          	  System.out.print("* ");
            }
     
            System.out.println();	 
          }
        }
     
        Start=false;
        option = console.readLine("Would you like to make another triangle? Please type either 'Yes' or 'No': ");
     
        if (option.equals("Yes"))
        {
          Start=true;
        }	
     
        else if (option.equals("No"))
        {
          System.out.println("Goodbye!");
          System.exit(0);
        }
       }
     
       /*Finds if an inappropriate response (Ex: Typing in 1 when prompted for a yes or no answer) is given
        *and closes the program if one isn't*/
       catch(Throwable e)
       {
         System.out.println("Please type in an appropriate response next time. Goodbye!");
         System.exit(0);	
       }		     	
      } 
     }
    }

    Apparently I have to make a larger triangle out of the smaller ones, but I'm not quite sure how to approach it. Any help would be greatly appreciated!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bigger triangle made up of multiple ones?

    Well, how would you do this "by hand" without a computer?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Bigger triangle made up of multiple ones?

    I would draw a bigger triangle out of smaller ones.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bigger triangle made up of multiple ones?

    Quote Originally Posted by Kimimaru View Post
    I would draw a bigger triangle out of smaller ones.
    Funny, but obviously not what I meant.

    One of the challenges that programmers face is breaking up things that seem obvious to us (like "draw a bigger triangle out of smaller ones") into steps that a computer can understand. HOW would you draw that bigger triangle out of smaller ones?

    If you can't figure that out, pretend you have a friend who has no idea how to draw a triangle, let alone a big tiangle out of smaller ones. Write out the exact, specific steps he should follow to accomplish this goal, and you'll have an algorithm that should be pretty easy to translate to code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Bigger triangle made up of multiple ones?

    Okay, thanks! Writing the pseudo code helped me quite a bit, but I still haven't got it so far. I'm VERY close, but I'm definitely overlooking something. Here's what I got so far:

    for (j=0;j<side; j++)
      {	
           for (triangleheight=triangleside; triangleheight>j; triangleheight--)
           {	
     
             for (i=0; i < triangleside; i++)
             {
         	 System.out.print(" *");  
              }
     
            }
       	   System.out.println();
            for (triangleheight=0; triangleheight<=j; triangleheight++)
            {
               System.out.print(" ");
             }	  
       }

    As you can see, all I really need to do is space the lower lines out at certain values. After that, I just need to add an outer loop and the program can be considered done. Can anyone tell me how I can go about doing this? The extra credit is due on Monday, so I'd really appreciate it if I got an answer soon. Thanks!

    Also, my code looks unorganized because of how the forum formats it. I'll see if I can fix it up to make it more readable.
    Last edited by Kimimaru; January 29th, 2011 at 07:34 PM.

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

    Default Re: Bigger triangle made up of multiple ones?

    Hell,

    I'm just new in java. I tried to make what it is shown in the image. For the output make sure your font is monospaced so the height and width is the same... Thanks...

    ....edited
    Last edited by copeg; January 30th, 2011 at 11:15 AM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Bigger triangle made up of multiple ones?

    Quote Originally Posted by nine05 View Post
    Hell,

    I'm just new in java. I tried to make what it is shown in the image. For the output make sure your font is monospaced so the height and width is the same... Thanks...

    ....edited
    nine05, thanks for contributing and welcome to the forums. While I believe your intentions were good, we'd like to ask that you do not hand over solutions...as stated in the Announcements 'Providing homework solutions in full or in part is frowned upon, and contributions that are considered as such will be subject to moderation (editing and/or deletion)'. I have edited your post as a result.

Similar Threads

  1. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM
  2. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  3. Bigger and Smaller
    By dylgod in forum Loops & Control Statements
    Replies: 4
    Last Post: July 28th, 2010, 09:18 AM
  4. Databases in Java made Easy with JPersist
    By Lord.Quackstar in forum JDBC and Database Tutorials
    Replies: 1
    Last Post: June 14th, 2010, 06:38 AM
  5. Replies: 0
    Last Post: May 25th, 2010, 08:19 AM