This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Help with stacks  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pastebin
Pastebin
Pastebin
The links above take you to the three classes i have.


I need to write a class called Book that has 3 Attributes: title, author, and category. The class has a constructor, a getter for the category, and a toString method. The toString method should return a string that has the same format as this example: How To Finish Homework On Time by Will B. Dunn, category: nonFiction. After that i need to write a driver class that has 3 stacks: toBeSorted, fiction, and noFiction. The driver class should push all the books from the file books.txt onto the toBeSorted stack(which i have already done). The driver class should then iterate through the toBeSorted stack, display the book on top of the stack (Hint: This is where the toString method in the Book class comes in), and sort them by category. That means to  push each book onto the fiction or nonFiction stack, based on its category and then pop the book of the toBeSorted stack. At the end, the program should display the number of books on each stack, if the toBeSorted stack is empty, and which book is on top of each of the other stacks.
 
Marshal
Posts: 62801
203
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

So you want to read something from a text file and sort it into two categories.
How are you determining the category?
Beware of giving so much implementation detail; you need to work out what you want to do before you try to work out how.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jordan Cozart wrote:Pastebin
Pastebin
Pastebin
The links above take you to the three classes i have.


I need to write a class called Book that has 3 Attributes: title, author, and category. The class has a constructor, a getter for the category, and a toString method. The toString method should return a string that has the same format as this example: How To Finish Homework On Time by Will B. Dunn, category: nonFiction. After that i need to write a driver class that has 3 stacks: toBeSorted, fiction, and noFiction. The driver class should push all the books from the file books.txt onto the toBeSorted stack(which i have already done). The driver class should then iterate through the toBeSorted stack, display the book on top of the stack (Hint: This is where the toString method in the Book class comes in), and sort them by category. That means to  push each book onto the fiction or nonFiction stack, based on its category and then pop the book of the toBeSorted stack. At the end, the program should display the number of books on each stack, if the toBeSorted stack is empty, and which book is on top of each of the other stacks.


Well, you have all to start with. So what's the problem? Iterate through the first stack and categorize all the books.
BTW, is it requirement to implement your own stack?
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tyoma Sakurakoji wrote:. . . BTW, is it requirement to implement your own stack?

I hope not, because I think the exercise can be done better without stacks.
Actually, it is by no means difficult to implement a stack.
 
Sheriff
Posts: 12952
216
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second link in the original post goes to OP's stack implementation.
 
Junilu Lacar
Sheriff
Posts: 12952
216
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP: some minor issues with your code. Some not so minor issue with your design choice.

1. Formatting could be better. Whitespace is good, both vertical and horizontal, because it makes your code easier to read and makes the structure of the code easier to see. However, too much of a good thing is not good either. You have wide vertical gaps that really could be reduces to one blank line. Indentation levels are not consistent either.

2. Don't write this:

This kind of code can easily lead to bugs if you inadvertently use "=" instead of "==". Also, it's redundant.

Prefer this instead:

Better yet, flip the conditional logic so you can write:

3. By declaring your pop() with a void return, that forces users of your stack class to always call peek() before pop(). These two methods should work independently of each other and client code should not have to depend on the order in which these methods are called. Your design makes this stack implementation a little more difficult to use than it should be and invites bugs into the client code (your driver class).

You should design the pop() method so that it can be used like this:

The way your pop() method is right now, you have to do this instead:

See the difference?
 
Tyoma Sakurakoji
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Actually, it is by no means difficult to implement a stack.


It is not as dificcult as it sounds. You can build stack on top of the list as a delegate, just by adding peek/pop/push methods...
 
Junilu Lacar
Sheriff
Posts: 12952
216
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tyoma Sakurakoji wrote:

Campbell Ritchie wrote:Actually, it is by no means difficult to implement a stack.


It is not as dificcult as it sounds.


It seems like you meant to state a dissenting opinion but "by no means difficult" means "it's not that difficult" so you're basically agreeing with what you quoted from Campbell.
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an example of a (very basic) stack implementation in Effective Java by Joshua Bloch; it occupies slightly over half of page 26, and reappears on page 130. In both cases there are problems with the original version which are corrected in subsequent text. So, yes, it is “by no means difficult” to write your own Stack implementation.
But I don't think it is necessary. I think there may be no need for the three stacks mentioned in the first post.
 
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:[It's] by no means difficult” to write your own Stack implementation.
But I don't think it is necessary.


Maybe I'm a purist, but I disagree.

Stacks were one of the first "collections" ever devised in computing and, in Java terms, came in two "flavours" - FIFO and LIFO - and had two "methods": push() and pop().
I don't have any problem with the non-destructive peek(), or allowing them to change their "endian" state, or even having a method that returns a List or Stream (although I have my doubts about that too).
But what Stack is NOT is an "addition" to the Java collections framework (which IMO, has gone a bit mad).
And because it is, it has about 65 methods now, twelve of which are called "of()".

Specifically, a Deque, while very useful, is NOT a stack. It's a queue ... although I'm quite sure I'll get CS "purists" telling me otherwise.
What it might be (given a decent implementation) is a subclass of Stack, but it isn't.
Either way, neither (IMO) are Collections. :-)

Winston
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agre that a Stack isn't a deque, nor pace java.util.Stack is it a List. I think I agree with you that it isn't a collection, so we are actually in agreement. I always thought stacks were FILO (no, that's pastry) LIFO, and if they are FIFO, they change to become queues. My idea of a basic stack is that it has four methods: push(E), pop(), peek() and isEmpty(). I can envisage a more advanced form of stack like that used in Forth with additional methods.
What I meant about not necessary is that I don't see why the exercise in question needs any stacks at all.
 
Winston Gutkowski
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:My idea of a basic stack is that it has four methods: push(E), pop(), peek() and isEmpty().


Yup, I can go with that. I forgot about isEmpty().

I can envisage a more advanced form of stack like that used in Forth with additional methods.


I certainly don't have any major problems with composing or subclassing it to do that.

But I still say it isn't a Collection, and certainly not for any methods that might back it by another "view" that could update it.

What I meant about not necessary is that I don't see why the exercise in question needs any stacks at all


Unfortunately, we don't have enough background info for a considered opinion. Maybe it was an exercise in how Stacks work.

Winston
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . But I still say it isn't a Collection . . .

Haven't I agreed with you on that point already?

What I meant about not necessary is that I don't see why the exercise in question needs any stacks at all


Unfortunately, we don't have enough background info for a considered opinion. Maybe it was an exercise in how Stacks work.

Winston

Good point.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!