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

Thread: Please help! Nested while loops and asterisk triangles!

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

    Default Please help! Nested while loops and asterisk triangles!

    Write program which prints the following four triangle patterns with four different user
    defined methods. Test your program by calling each of these four methods from the main method. Use different parameter values to test your methods. Assume the parameter of each method is an integer >= 5 and <= 25.
    NOTE: Each of your methods is required to use two nested while loops to print its triangle pattern. Write your user defined methods in the same class as your main method and before your main method.

    firstPattern(5); (call to firstPattern with parameter 5 produces this right triangle
    with height 5 and base)
    *
    **
    ***
    ****
    *****

    secondPattern(5);
    *****
    ****
    ***
    **
    *

    thirdPattern(5);
    *
    **
    ***
    ****
    *****



    fourthPattern(5);
    *****
    ****
    ***
    **
    *


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

    Default Re: Please help! Nested while loops and asterisk triangles!

    Please post what you've done with any and all errors clearly - we are not a code service so just posting a homework assignment will get you nowhere. Further, search the forums - this homework assignment has been posted time and again on the forums. Lastly, scroll down to the bottom of the page and you will find a list of Similar Threads, many of which will provide you guidance.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help! Nested while loops and asterisk triangles!

    Quote Originally Posted by copeg View Post
    Please post what you've done with any and all errors clearly - we are not a code service so just posting a homework assignment will get you nowhere. Further, search the forums - this homework assignment has been posted time and again on the forums. Lastly, scroll down to the bottom of the page and you will find a list of Similar Threads, many of which will provide you guidance.
    Hey I am new I have the same assignment and got stuck in it to but i tried searching like you said, but i can't seem to find anyone else who posted it. Can you point me in the right direction to the others that have posted this assignment so i can find out where to begin?

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help! Nested while loops and asterisk triangles!

    here is what i have just need help converting it. I personally like for loops better than while

    import java.io.*;
     
    public class NewClass {
     
        public static void main(String[] args) {
     
    for (int i = 1; i <= 5; i++)
        {
        	for (int k=i;k<5;k++)
                {
                    System.out.print(" ");
                }  
     
            for (int j = i; j > 0; j--)
                {
                    System.out.print("*");
                }
    		          	System.out.println(" ");
                }
     
     
            System.out.println("");
     
    int f=1;
     
     
     
     
         for (int i = 5; i>=1; i--)
            {
                for (int j = i; j >=1; j--) 
                    {
                        System.out.print("*");
                    }
     
                        System.out.println(" ");
     
                for (int k=0;k<f;k++)
    		        {
    			         System.out.print(" ");
    	           	} 
    f++;
            }
     
        }
    }

    not sure if its right i just typed it and tried my best but i no its incomplete compared the assignment. Can you guys tell me what i am doing wrong?

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

    Default Re: Please help! Nested while loops and asterisk triangles!

    import java.util.Scanner;
    public class Lab10a
    {
    public static void firstPattern()
    {
    int parameter;
    Scanner scan=new Scanner(System.in);
    System.out.print("Enter parameter number:");
    parameter=scan.nextInt();
    int row=1;
    while(row<=parameter)
    {
    int star=1;
    while(star<=row)
    {
    System.out.print("*");
    star++;
    }
    System.out.println();
    row++;
    }
    }



    this is what I have so far and it works right, i just dont know how to do the other shapes??

    also, how do I make it work so that instead of asking for the parameter in each method it will ask once and use that parameter in all of the methods?

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Please help! Nested while loops and asterisk triangles!

    *
    **
    ***
    ****
    I assume is what you have since it is the easiest. It prints 1 star upto N stars.
    ****
    ***
    **
    *
    Is the opposite. It prints N stars down to 1 star.
    ****
     ***
      **
       *
     
       *
      **
     ***
    ****
    These two patterns are similar to the first two except you now have to print M spaces before the stars.

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help! Nested while loops and asterisk triangles!

    how about in nested for loop?how to convert that?

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help! Nested while loops and asterisk triangles!

    Quote Originally Posted by Zerro900 View Post
    here is what i have just need help converting it. I personally like for loops better than while

    import java.io.*;
     
    public class NewClass {
     
        public static void main(String[] args) {
     
    for (int i = 1; i <= 5; i++)
        {
        	for (int k=i;k<5;k++)
                {
                    System.out.print(" ");
                }  
     
            for (int j = i; j > 0; j--)
                {
                    System.out.print("*");
                }
    		          	System.out.println(" ");
                }
     
     
            System.out.println("");
     
    int f=1;
     
     
     
     
         for (int i = 5; i>=1; i--)
            {
                for (int j = i; j >=1; j--) 
                    {
                        System.out.print("*");
                    }
     
                        System.out.println(" ");
     
                for (int k=0;k<f;k++)
    		        {
    			         System.out.print(" ");
    	           	} 
    f++;
            }
     
        }
    }

    not sure if its right i just typed it and tried my best but i no its incomplete compared the assignment. Can you guys tell me what i am doing wrong?
    convert that in nested for loop?

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Please help! Nested while loops and asterisk triangles!

    The code does use nested for loops. What are you asking? And why have you revived a 5 month old thread?
    Improving the world one idiot at a time!

  11. #11
    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: Please help! Nested while loops and asterisk triangles!

    Quote Originally Posted by Junky View Post
    The code does use nested for loops. What are you asking? And why have you revived a 5 month old thread?
    Because school just started, so we're going to see a massive inflow of homework dumps. Batten down the hatches!
    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!

  12. #12
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help! Nested while loops and asterisk triangles!

    Check this !!!!!!!!!!!!!!!!!!!!!!!!!

    (spoonfeeding removed by KevinWorkman)
    Last edited by KevinWorkman; September 15th, 2011 at 11:50 AM. Reason: removed spoonfeeding

  13. #13
    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: Please help! Nested while loops and asterisk triangles!

    Quote Originally Posted by AJAXx195 View Post
    Check this !!!!!!!!!!!!!!!!!!!!!!!!!
    Please read this: http://www.javaprogrammingforums.com...n-feeding.html
    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!

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  2. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM
  3. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM
  4. asterisk forming diamond
    By cutee_eyeh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 13th, 2010, 08:53 PM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM