• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create this in java

 
james maina
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*
**
***
****
*****
****
***
**
*
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James. Welcome to the Ranch!

It's a much more effective learning process if you manage this yourself (with help if necessary) than if someone hands out the answer. So...what have you tried so far? Where are you having problems?
 
james maina
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just started created few programs but when i came to this one i tried to do this
[code=java]class Patterns{
public static void main(String[] args){
System.out.println("*");
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
}
}
 
Rose Ellis
Greenhorn
Posts: 16
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you could take a manual approach and write 9 print statements. That could be your start. Or you could try making it a little bit more automatic and use a for loop with a couple of if statements to determine whether you're increasing the number of stars or reducing them and print them out. Give it a shot and let us know if you get stuck
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happened? Did you get any errors?

Now I've formatted it I can see a couple of problems. One with the name of the class (you can't have * as part of a class or variable name). And have a closer look at line 2 - does that look like the start of other main methods you've seen?
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Program comes likes this
public class Patterns {
public static void main(String[] args) {
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
}
}
 
james maina
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was great....but now how do i use the for loop to make it automatic?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?

We really don't hand out answers here. Make an effort, write some code, post it, tell us what it did, tell us what you want it to do...

Simply saying "Please do this for me" really won't get you very far around these parts.

 
Rose Ellis
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you some pseudocode

loop until it reaches the number of lines you need to print out
in the loop
concatenate the "*" to a String variable (has to be initialised outside the loop) if the loop counter is less than the longest line number,
otherwise use a substring starting from position 1 (that would remove one "*" from your string)
print the resulting string

done
 
james maina
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Rose, that is idea I needed....
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than catenating to a String, append a * to a StringBuilder, or remove a * from it. That will give better performance.
 
james maina
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Patterns {
public static void main(String[] args){
for(int i=1; i<10; i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println(" ");
}
}
}


guys I managed to do this but the part that is supposed to make them reverse the other way has proved to be a bit hectic.
 
james maina
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class pattern3{
public static void main(String [] args){
for(int i=1; i<=12; i++){
if(i>1 && i<11){
for(int j=7; j<12-Math.abs(i-6); j++)
System.out.print("*");

}
System.out.println();
}
}
}
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james maina wrote:guys I managed to do this but the part that is supposed to make them reverse the other way has proved to be a bit hectic.

Ah, so now you're changing the rules, eh?

Have another look at Campbell's suggestion, and then look at the API for java.lang.StringBuilder. I think you may find exactly what you're looking for, but you MUST look.

Winston
 
Junilu Lacar
Bartender
Pie
Posts: 7275
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is kind of mind-boggling, isn't it? Come back to it in a day or two and try to figure out what it's doing. Or have someone else read it and figure out what it's doing. Not nice.
It's easier to solve if you break down the problem into smaller parts:
If you strive for clarity, the solution comes a lot quicker and it turns out a lot nicer.
 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Soni,

First, welcome to the Ranch. We're glad you decided to stop by.

Second, we generally frown upon providing complete solutions. We want to teach people how to program, not do their homework for them. Solutions will generally be deleted. However, since this doesn't actually do what the OP asked for, I'll go ahead and leave it. This is what I got for the output:

C:\slop>java Arrow
*
**
***
****
*****
******
*******
********
*********
******************************************************
 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Ashwin Soni,

First, welcome to the Ranch. We're glad you decided to stop by.

Second, we generally frown upon providing complete solutions. We want to teach people how to program, not do their homework for them. Solutions will generally be deleted. However, since this doesn't actually do what the OP asked for, I'll go ahead and leave it. This is what I got for the output:

C:\slop>java Arrow
*
**
***
****
*****
******
*******
********
*********
******************************************************


Yes, Actually I understood by reading all replies in the post that we are not doing their homework.
That is why I left following code at 99.99% .
However, In code you can understand Loop is right but braces are at wrong position therefore it happened.
 
james maina
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys it was a great experience to learn how to create that code with for loop and an if statement.......I had posted the progress of my work, and still working on other methods and I will post them to show the progress..... I appreciate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic