wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes MyPattern.Java - homework help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "MyPattern.Java - homework help?" Watch "MyPattern.Java - homework help?" New topic
Author

MyPattern.Java - homework help?

Michael Pickens
Greenhorn

Joined: Mar 01, 2012
Posts: 24
I am writing code for an assignment in where I need to display a triangle that looks like this..

1
2 1
3 2 1
...
n n-1 ... 3 2 1

I have already written the code to handle values fro 0 - 9. I am want now to figure out how to write the code so that it can handle any number and still look all lined up and nice.
I can post my code, but I am not sure if there are requirements with how this site wants the code posted. I did not see any sections that dealt with this question.

I do not want the answer, I just want to know which direction to go... There is a lot to be said for the feeling of accomplishment of writing a program that actually works!!

I know I need to probably insert an array into the mix, but I don't know where to start.

Thanks in advance for any help!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Michael Pickens wrote:I am writing code for an assignment in where I need to display a triangle that looks like this..


I have already written the code to handle values fro 0 - 9. I am want now to figure out how to write the code so that it can handle any number and still look all lined up and nice.


So, like this?


I can post my code,


Please do. And please UseCodeTags.(⇐click)

I do not want the answer, I just want to know which direction to go...


Start by describing in English, using pencil and paper, exactly what the steps will be. Don't worry about trying to do it in Java at first. Just make sure you get the description precise, and that each step is very simple and basic. And don't be afraid to use variables in your description. This is the hard part. If you do this part right, the translation to Java will be straightforward, maybe even trivial.

I know I need to probably insert an array into the mix,


Nope, not necessary. But if the solution that you come up with uses one, that's fine.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

If you've already got it sort of working, all you need to do is work out where and when to add the additional spaces. As a hint, I can suggest one of two general approaches:

- Work out a way of getting the number of digits in a number. Once you know that, you know how many spaces you need to insert in front of it.

- Investigate the String.format() method. That has lots of formatting options, some of which can be used to help with the alignment.
Michael Pickens
Greenhorn

Joined: Mar 01, 2012
Posts: 24
Here is my code



I wrote it down on paper to my ability, but It feels like I did it all wrong.
1. Get value from user using JOptionPane
2. Convert values to int datatype
3. reverse digits from n to 1.
4. Insert spaces from n - (n-1) decreasing by 1 each time
5. print n from value of user input decreasing the value by 1 each time

Would it be unwise to more difficult to try to use a Ragged Array? it is what we are learning this week and I am anxious to try it out, but I don't want to make this super complicated.

Again, thanks for your help!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11169
    
  16

Michael Pickens wrote:Would it be unwise to more difficult to try to use a Ragged Array? it is what we are learning this week and I am anxious to try it out, but I don't want to make this super complicated.

You should not let the classes drive your design. Your design should drive your classes.

What I mean by that is you should not say "Boy, I just learned about technology X, so I am going to use it to solve my problem". It doesn't matter how cool lasers are...if you need to drive a nail, a laser will always be the wrong tool.

You should go to your design and say "What exists that would best do what I need?"

additionally, I would say your design needs refinement. for example, what does "reverse the digits from n to 1" mean? Where is that in your spec?

personally, i'd approach it like this:

1) get an integer "n" from the user (doesn't matter how just yet)
2) print "n" lines of <stuff>

That would be my first draft. You seem to know how to get the input, so I'll just focus on #2. I would consider writing code that literally prints "line x" n times...something like

line 1
line 2
line 3
...
line n-1
line n

Once i have that working, I know all i have to do is write a method that prints what I need it to print, and I can drop it in place of my "print line X" line of code.

So, I'd start looking at what I need to print. I notice that for any given line, I print from some arbitrary value "y" down to 1. So now I need a method that might take an integer as a parameter and prints from that value down to 1. I wouldn't worry about spacing yet.

Once I got THAT done, I'd worry about spacing. I'm not sure what your spacing needs to be exactly, but i'm guessing it has something to do with the size of your input value. So maybe the method that prints from Y to 1 should take an additional parameter that would hint at how many spaces need to be between each value.

Yes, it may get tricky when you go from printing 3digit number to 2 digit number, but that shouldn't be too hard. You can probably take the log10 of a number, round it down (or up? - not sure which way) to figure out how 'wide' your current number is...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:It doesn't matter how cool lasers are...if you need to drive a nail, a laser will always be the wrong tool.


I don't know. Some lasers come in fairly solid, heavy housings that can still fit in a person's hand...
Michael Pickens
Greenhorn

Joined: Mar 01, 2012
Posts: 24


I have it close but I cannot for the life of me move 1-9 over 3 more spaces, perhaps I am not thinking of all the options. This is my 7th week of Java programming, prior to that I was a chef who played video games.... so you can see my experience.

my thought would be to either make a new method to deal with printing (hopefully I can figure that out) or to add in another if statement somewhere else that controls just the first 9 rows that gives them an extra 6 spaces. the problem is obviously that I can only control all the rows and columns.

I am having difficulty starting from the beginning. When I go to write down what I want to do, it is far from psuedocode.

Thank you
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11169
    
  16

"Pseudocode" means "english that describes what the program should do", so you ARE writing in pseudocode. You just need to practice your refinement of it.

I am 100% serious when I say "get it to print what you want without worrying about the spaces just yet". And I would submit that if you have hardcoded the number of spaces, you are going to have problems with values like 100, or 1000, or 10000000000.

So...can you write the program that does this for 4:

1
2 1
3 2 1
4 3 2 1

and does this for 12:
1
2 1
3 2 1
4 3 2 1
...
12 11 10 9 8 7 6 5 4 3 2 1

Get that to work before you worry about anything else.

Michael Pickens
Greenhorn

Joined: Mar 01, 2012
Posts: 24
The last code i posted will handle any positive integer from 0 - 9, though 0 outputs nothing.

I adjusted the code within the loops to do what you posted above. This too can handle user input of 0 - 9.



that being done, i moved on to spacing.

in the post above, my output is as follows:

1
2 1
3 2 1
4 3 2 1
...
10 9 8 7 6 6 5 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1

^^ when i submit this it is not formatting the right way on the screen. its supposed to start from the right hand side of the screen and print backwards left...

i need a way to control the rows separately or in a group... like all rows from 10 - 99 and 100 - 999 etc. I know that I read last chapter that an array can control rows independently. I understand how to put them into a program, but I am failing to connect the dots between coding them and outputting the result in the fashion I need.

I am flailing all over the place here. Thank you for the detailed steps on what I should be doing... I am finding out that is most of the issue for me... its not so much that I don't know what things are.. its the logic on how you put them together into a program.

Again, thanks

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11169
    
  16

Michael Pickens wrote:i need a way to control the rows separately or in a group... like all rows from 10 - 99 and 100 - 999 etc. I know that I read last chapter that an array can control rows independently. I understand how to put them into a program, but I am failing to connect the dots between coding them and outputting the result in the fashion I need.

I am flailing all over the place here. Thank you for the detailed steps on what I should be doing... I am finding out that is most of the issue for me... its not so much that I don't know what things are.. its the logic on how you put them together into a program.

As I understand it, when yo print your row with "1", you need to have some idea how wide the last row will be so you know how far over to move that "1". You can't have a hunk of code to handle 1-9 that knows nothing about 10-99.

so...what is this suppose to do:


it looks like it is a loop that prints...nothing..ever. I took it out. I ran your code, passing in "12", and got this:

C:\slop>java temp
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12

that's not quite what you want, from what you describe - or is it?
Michael Pickens
Greenhorn

Joined: Mar 01, 2012
Posts: 24
Thanks again for your help.

The line



outputs blank spaces so that the first "1" can be on lined up k amount of spaces to the right. Im afraid what i typed earlier didnt read right, my apologies.

The output needs to be lined up on the right as pictured in Jeff's post earlier today.

I can get it to output the right way if I have to line it up on the left as it is pictured in your post.

thus the right triangle opens from the vertex to the left... (that description was terrible but...)

Thank you so very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MyPattern.Java - homework help?
 
Similar Threads
Operator: & ^ |
Extracting portions of a string
Java Recursion and binary tree searching questions
Java help!!!!
creating multiple rows