Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

pyramid of numbers  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If 4023 I pass then I should see output like

      0
     2 2
   3  3 3
4  4 4  4
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there is no relationship between the numbers and how many times they are displayed?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:No

Of course there is. It's just not "display a digit as many times as its value."
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First digit(i.e least digit) one time in first row
second digit(i.e next least digit) two times in two row
third digit(i.e next least digit) one time in three row

etc
 
Carey Brown
Bartender
Posts: 2710
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I normally don't post answers but I feel this thread has gone on way too long.

 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will try. thank you
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below code produced below output


Input number: 5319
   5
  3 3
1 1 1
9 9 9 9




If i want below output as ascending order from first to second row to third row etc as below how to modify the code


Input number: 5319
   1
  3 3
5 5 5
9 9 9 9
 
Carey Brown
Bartender
Posts: 2710
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try copying the code correctly.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try copying the code correctly.


can you please elaborate on this? I was not clear
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
If i want below output as ascending order from first to second row to third row etc as below how to modify the code


Well, obviously, you need to sort the input first.  Since this is likely a homework assignment, or following up a homework assignment for learning purposes -- what have you done so far to modify it?

Henry
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is likely a homework assignment, or following up a homework assignment


this is not. you assumption is wrong
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
Since this is likely a homework assignment, or following up a homework assignment


this is not. you assumption is wrong


The impetus of the assumption is that you are doing this to learn.  Hence, my question of asking what have you done so far.

Now surely, this is not wrong?

Henry
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try with collection class and let you know about sorting as well
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not post something which you wrote yourself that actually works. Never mind how well it works, just something that works. If you have something which works, however badly, you can improve it.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not post something which you wrote yourself that actually works.


i posted more than 50 comments with whatever i can. I do not know something that actually works. If i know would not have posted this question here. can you please provide solution. I am almost about to give up on this challenge
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i finally got my solution of ascending pyramid.
how to improve/modify my code. please advise
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:. . . how to improve/modify my code. please advise
Start by indenting it correctly.
Remove all those commented‑out lines.
Remove all those blank lines.
Change the main method to its ideal length: one statement.
Try some object‑oriented programming: you shou‍ld be able to write
new DiamondPrinter(12).print();
and get a diamond printed. (Or maybe only a pyramid).
What do the loops on lines 42‑49 mean? Why are you using <= rather than <?

Is that enough to get you started.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will fix them. why eclipse wont remove blank lines when i say control A then Control Shift F to format?
 
Knute Snortum
Sheriff
Posts: 3842
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will, you just have to tell it what to do.  The default is that multiple existing blank lines collapse to one.  You can change that in:

Windows -> Preferences -> Code -> Formatter

Edit the active profile or create a new one, then click on the Blank Lines tab.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't remove all the blank lines. A blank line to separate one method from the next or different logical sections of a block of code can improve readability. You don't want to have your code all mashed up into one big continuous blob with no visual cues as to which lines of code belong together.
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . A blank line to separate one method from the next . . .
. . . which you can set up as an option in Eclipse.
 
Liutauras Vilda
Marshal
Posts: 4271
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:why eclipse wont remove blank lines when i say control A then Control Shift F to format?
Don't be lazy. You have around 25 effective lines of code. At this stage probably better would be not to use Eclipse at all, but rather have some text editor and console next to you, so you could compile and run the code from there.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


does above looks ok?
if i chanfge <= with < first element cut in console output which i do not want.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going through below link

https://coderanch.com/wiki/660020/Main-Pain

i feel it is too much for a simple class



i wonder who (client)  and where CashRegisterLauncher getting called?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i also commented one unsed import which actually i can delete as well in this case
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your main() method looks a little better but there's still a lot you can do to improve your code.

1. Why does your print() method return an array of integers which the main() method doesn't even use? What's the point and the logic of doing that?

2. Something like PyramidPrinter would probably be a more suitable name for your class. Class names should be nouns or noun phrases.

3. Your print() method still has too many lines and has the details for doing way too many things.  If a method is going to have detailed implementation in it, let it be for just one simple and focused task. If it does many different things, the method would be better written as a Composed method.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than going back to square one and repeating ALL the advice you've been given before over several weeks spanning 2016 and 2017, why don't you go back over this entire thread again and actually read and follow some of the advice you've been given?
 
Liutauras Vilda
Marshal
Posts: 4271
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:does above looks ok?
Nice code formatting, have a cow
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a cow

where?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i changed the class name. I wonder what you mean by point 1 where main does not even know about int?

my intention is to just print pyramid of numbers and i felt there is no need for main method to know about it. No other business logic or thought process behind that
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. Your print() method still has too many lines and has the details for doing way too many things.


if i do away to multiple methods later on orchestration becoming difficult to me. can you please show exactly what you mean by this with working code as i already solved this challenge?
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:. . . where?
Look above the text of one of your posts. Just right of where it says, “Posted 67 years ago” or similar.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read what Junilu wrote 2 posts ago? I really believed you'll absorb that information. Basically how I understood what he said, that you should take very small steps at a time and make some methods, so each of them would do very concrete singular task.

same thing i am trying in last 100 coments. I finally got solution. But now i do not know how to modify my code to look more like object oriented code with bunch of methods as i do not know how to interact printing spaces and printing numbers methods both similtaneously to print the pyramid structure i am interested?
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, fine. You have been doggedly looking for illumination on this problem and this thread has gone on far too long.

I'll demonstrate to you my process. Keep in mind that there are many ways you can solve this problem. The one I first came up with many posts ago was not even object-oriented; I had all static methods. The reason I did that was because this was only one class and there didn't seem to be a compelling reason for me to create an OO design when there was just one class involved and the process was pretty much sequential. On the other hand, it might be educational to try to do it with a proper object instead of just a bunch of static methods. So, I step through an entirely new solution, using proper objects.

First of all, I see no need to convert to integers as you have been doing. The input is a String and the user is expected to include only digits so I'll make the assumption that that is exactly what the user will do.

So, the first cut will be:

I'll compile this to make sure I have no errors so far. I see that it compiles with no errors, so I continue.

Edit: The code posted above actually does have an error but it's a pretty quick fix. I'll leave it as originally posted anyway.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next, I want to solve the problem of breaking apart the String into separate characters that I can deal with individually. So I'll add some code to do that:

Now, I need a way to verify that I've done the right thing. Normally, I'd write a unit test but this time, I'll just eyeball the result by printing out the characters. I'll add a call to a method that will display something, then add that method:

I see that there are no compiler errors so I run the program and eyeball the result. My run looks looks something like this:

Enter a sequence of digits, no spaces: 3402
3
4
0
2

Ok, now I've made a little bit of progress. That's good.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've run the program twice now and I've had to enter the sequence of digits manually. I miss my automated unit tests. I still don't want to go to the trouble of doing a JUnit right now, especially since I'll have to go and explain it to you and you'll probably have an exponential number of questions to follow. So I'll do the next best thing: I'll hard code my input data for now.  I go back to the getUserInput() method and modify it:

Now I won't have to type in anything and I'll have the same data to work with while I fill out the program.

I run the program again to make sure I didn't mess anything up. It still runs fine.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
makes more sense now. But how to get pyramid structure like
If 4023 I pass then I should see output like

      0
     2 2
   3  3 3
4  4 4  4

that is where all the complexity using multiple methods using OOP and OOD principles?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!