• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array reverse printing  RSS feed

 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to java. And now in arrays I need his help







I partly understand whhat it means but I need to make this pprint in reverse.

Now it prints as:
6
5
4
3
2
1
. I want it to print descending order.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Do you mean that regardless of the input order you want to print them in descending order (ie you want to sort them) or do you mean you want to print the stack from bottom to top?
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... i want to print it the stack as 1-6 not 6-1...
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to re-write the main method so that there is a call to another method called reversePop that will take an array of chars and print the array elements in reverse order using the pop method.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I ask why you are using a stack if you just want to iterate over the collection in reverse order?

There are few ways you could do it. One way is to pop all the items off the stack and add them to an another stack, then pop them off this stack and print them.

Edit: This response is to the earlier post "Yes... i want to print it the stack as 1-6 not 6-1..." and was posted at the same time as the OP posted more detail on what was required.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you want to add a method to your stack class to reverse the stack?
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is alot to take in "One way is to pop all the items off the stack and add them to an another stack, then pop them off this stack and print them." I already have a stack interface. So I added the reversePop method. The issue is my little knowledge on going about it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if that confused you but you were not being clear about what you are trying to achieve. Have you got the reverse method working?
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Not yet. May be I should post the stack interface too. My main aim is to make another method that can be called so that it prints the array elements in reverse.

this is the stack interface:






Any advice if I've gone about it the wrong way will be taken
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick grimz wrote:My main aim is to make another method that can be called so that it prints the array elements in reverse.

No, I don't think that is what you want. I think you want a method that reverses the order of the stack so the existing code prints the values in the order you want.

You need to be very precise in what you are saying otherwise it becomes very confusing both for us and for yourself. If you can't clearly and precisely express what you are trying to do it is often a sign that you don't really understand the problem. At this stage you should stop coding until you have a thorough understanding of the problem and how you intend to solve it.
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looool. I'm sorry for laughing, The way you put it felt like I'm being scolded. But I couldn't explain it better. That is what I want. I will do my best next time.
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I go about this. Sorry for dragging it to this point. I guess its one of my problems understanding the problem
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is implementing this "reversePop" method something you have come up with, or something you are being asked/told to do?

The whole point of a stack is that it is LIFO: Last In - First out.
If you want to print out the elements of the stack in the order they were inserted, then what you need is a FIFO - First in First out approach - otherwise known as a Queue.

Is this print method going to be internal or external to your stack interface?
If you write a "printContentsofStack" in your stack class, then that is free to look in the underlying array, and print in whatever order makes sense.

If you are writing code that has to use the Stack interface, then use the stack interface as is. My suggestion would be then to NOT implement a "reversePop" method. That goes fundamentally against what a stack does.
Instead follow Tony's suggestion of create new stack (pop from old, push into new) and then do your current print logic.

 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked to do so. I guess what tony suggested would be more to do the point. But I was instructed to use the reversePop method.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rick,

I might have misunderstood it, but do you want to print your stack by popping
your elements? What if you want to print it again? You would find your stack
to be empty.

So, incorporate a 'print(boolean fromStartTofinish)' in your Stack class. Since
you have your array at hand, that should be a doddle.

Also, if you want to do a 'reversePop' then you could incorporate a 'bottom' variable
just like you have a 'top' variable. Then, when you find your 'bottom' becoming too far
away from 0, you might consider creating a new array, just like when your array
becomes too small.

A linkedList might be a better structure, though.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have already been given some useful advice. I have lots to say about code style, however. Never double‑space code. It becomes harder to read. Use blank lines to separate logical parts of the class, e.g. adjacent methods. And never start variable names with CapitalLetters. I have got rid of lots of empty lines. Your indentation is inconsistent. Look at lines 53-55 and 83-87. You should get yourself a decent text editor, e.g. Notepad++, and set up automatic indentation with automatic conversion of 1 tab→4 spaces. That way you won't confuse yourself with a mixture of tabs and spaces. Don't put spaces on either side of [ and ]. Not myArray [ 123 ] but myArray[123] please. You have CAPACITY and capacity fields. That is a surefire recipe for confusing yourself. Try changing CAPACITY to DEFAULT_INITIAL_CAPACITY. You demonstrated the problem yourself because you added a comment to the use of CAPACITY.
I suggest the array of values be called values. In which case you do not need a capacity field at all; you can use values.length.
Don't declare an unchecked Exception; describe it in the documentation comments.

Also some design things. Make all your fields private. Even the default capacity has no business being public. You can publish the value 2 like thisDon't use a loop to copy an array; use this or this (one of them not available in Java5).
Why are you using top for a field? Call it size, and start it at 0. Now you don't need to mess around with +1 and −1.You do however have to alter your push pop and peek methods. What you have called top should be celled peek. This is the simplestYou need different combinations or size++ and size-- too. You can read about that in Effective Java bu Joshua Bloch, where you will find code very like this:-I agree that reversePop is not something you associate with a stack. If you write any Forth, however, you do lots of work directly on the call stack and there are several other operations which are over and above the usual repertoire of stack methods. If you wish to print the whole stack, you should give it a toString method.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:. . .
A linkedList might be a better structure, though.
You can write stacks based on linked lists and on arrays. I think OP has been instructed to use an array. A linked list is easier to use because you never have to worry about capacity problems, but an array will give faster performance because there is usually no need for creating new objects when pushing, and when popping there is often no need for garbage collection.
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all. I'm grateful for all your answers. I tried it, after hours but luckily it worked. The method you gave is straight to the point, Although, I'm abit worried, the instructor wants what he asked for. i don't want them thinking, I'm getting ahead of myself. Although I do intend to show him the other method.

I attached an image for you to see.
IMG-20151024-WA0002.jpg
[Thumbnail for IMG-20151024-WA0002.jpg]
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't say anything about a reverse method in your stack. It says reverseUsingPop, and that can be in a different class. What we thought all along.

Please avoid attaching images with text in; they can be difficult to read or search.
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Note taken. Although the truth is I have a problem understanding java questions. calling this and calling that...

I ended up doing this at some point. Well aided, I would say.



This is how much I don't get questions sometimes
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[The OP PMed me with this code. I want to comment publically.]


1) You're not passing in a char []. The array should come from main, not be built in the method:

2) You're not using the pop method from the SimpleArrayStackofchars class.
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I use the pop method in the reverse method?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is calle reverseUsingPop, then you probably should use pop in the reverse method. I thought you already had a suggestion last week about how to implement reversal with a stack or two stacks.
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a hard time understanding that. I made the method reverseUsingPop as I was advised. Where I find difficult is using the pop method in the reverse method.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears last week you were not supposed to have a reverse method. So where does this new requirement come from?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the guts of pop():

So if S[0] = object1 and S[1] = object2 and S[2] = object3 and top = 2, what element will be returned if you call pop()? How could you use this to create a new array in reverse order?
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It appears last week you were not supposed to have a reverse method. So where does this new requirement come from?
I meant reverseUsingPop method. What I need to do (as advised by Knute) is; to use the pop method from my code to print the passed array in reverse order, That is my problem now.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rick,

have you been taught recursion yet?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No recursion is needed. A simple for loop will do the trick.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recursion is not needed, indeed, but:

I'm not sure that your simple loop is correct (it might be, of course).
If I understand the assignment correctly, then it is about a stack
that is backed up by an array. Therefore, when an array a[]
is input for this Reversed...-method, I assume the printing
sequence is meant to be a[0], a[1], et cetera. And that by using
the pop-method that starts with a[length - 1].

If I am correct, then recursion is certainly an easy way, provided
the use of it is allowed.

Therefore my question (to rick).
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Recursion is not needed, indeed, but:

I'm not sure that your simple loop is correct (it might be, of course).
If I understand the assignment correctly, then it is about a stack
that is backed up by an array. Therefore, when an array a[]
is input for this Reversed...-method, I assume the printing
sequence is meant to be a[0], a[1], et cetera. And that by using
the pop-method that starts with a[length - 1].

If I am correct, then recursion is certainly an easy way, provided
the use of it is allowed.

Therefore my question (to rick).


"Re-write the main method so that there is a call to another method called reverseUsingPop that will take an array of chars and print the array elements in reverse order using the pop method."... That is the question anything other than that reduces points. It's tricky. Well for me
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:No recursion is needed. A simple for loop will do the trick.
What loop will that be sir
 
rick grimz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi rick,

have you been taught recursion yet?
No. I haven't done that... Yet
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick grimz wrote:
Knute Snortum wrote:No recursion is needed. A simple for loop will do the trick.
What loop will that be sir

Look at the code you PMed me with and posted on the 5th. Then look at my message on the 9th.

You have a loop in your PMed code, but it doesn't use pop(). Use the hints in the 9ths post to create a loop that uses pop().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!