Solved

write a method called printTriangle. How to start?

Posted on 2008-10-20
9
220 Views
Last Modified: 2013-11-23
(Objects first with java, A practical Introduction using Blue J, Third Ed, David J. Barnes & Michael Kolling)

Hi

I have to write a method called printTriangle.

example:  printTriangle(4);

output should look like this:

*
**
***
****

and I don't know how to start. Can you please give me an advice?
0
Comment
Question by:bt1942
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22764318
0
 

Author Comment

by:bt1942
ID: 22764354
chaituu// that seems very complicated, I just need to write a simple method soething like

               
public void printTriangle()

	{

	    int n = 4;

	    String s = "*";

	    for(int row = 1; row<= n; row++)

	    {

	        System.out.println(s);

	      

	       }

	   }

}

Open in new window

0
 
LVL 6

Accepted Solution

by:
javaexperto earned 200 total points
ID: 22764396
the first thing you need is one loop to print "*" and other loop to print a new line. Your first loop will get up to your parameter and the second will enter the first time one,  the second two, etc. The loops must be nested, here the method:
public void printTriangle(int stars) {

    for(int i = 0; i < stars; i++) {

        for(int j = 0; j <= i; j++)  {

	    System.out.print("*");	

	}

	    System.out.println("");

    }

}

Open in new window

0
 
LVL 3

Assisted Solution

by:siranm
siranm earned 100 total points
ID: 22764405
here you go....

public void printTriangle()

        {

            int n = 4;

            String s = "*";

            for(int row = 1; row<= n; row++)

            {
 

for(int stars=1; stars<=row;start++){

                System.out.println(s); //1st row->1star, 2nd row->2stars,...                    

}

           }

}

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Expert Comment

by:siranm
ID: 22764410
yep, javaexperto, i missed that detail on the new line ; )
0
 
LVL 3

Assisted Solution

by:glenjr
glenjr earned 100 total points
ID: 22764419
bt1942,

you are on the right track. right now your program prints out:

*
*
*
*

In order to adjust it so that it prints out the way you want you need to the following line below your system.out...:

s+= "*";


Also, make sure your {} match and that you have a main method in there some where.

Let me know if you have questions.

glenjr
0
 
LVL 3

Expert Comment

by:glenjr
ID: 22764424
looks like i wasnt the only one working on it, i wasnt fast enough :)
0
 
LVL 20

Assisted Solution

by:chaitu chaitu
chaitu chaitu earned 100 total points
ID: 22764430
public static void main(String[] args) {

             int n = 4;
           String s = "*";
           String s1 = "";
       for(int row = 1; row<= n; row++)
       {

for(int stars=1; stars<=row;stars++){
      s1+=s;
           System.out.println(s1); .                    
}
      }
       }
0
 

Author Closing Comment

by:bt1942
ID: 31508140
Thanks for your help!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now