This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using class Iterable and sorting ArrayList

 
mike karl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to write a method setOrder() which takes enumeration constant defined in Report so iterating over Bills is performed in the order specified by this constant and i have have no clue where to start.
this is what i have so far, let me know what i did wrong and what i should do next.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike karl wrote:i have to write a method setOrder() which takes enumeration constant defined in Report so iterating over Bills is performed in the order specified by this constant and i have have no clue where to start.
this is what i have so far, let me know what i did wrong and what i should do next.

What you did wrong was to start writing code without thinking about what you should do.

The single most important thing a programmer needs to do is NOT WRITE CODE. Before committing a single byte to file, you should think through the problem in very NON-java terms. you should pretend you are explaining to a child what needs to be done.

Only when you've done that, revised it and simplified it several times, should you write any actual code.

So I would say your next step is to delete what you have, turn off your computer, and put pencil to paper.
 
mike karl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
mike karl wrote:i have to write a method setOrder() which takes enumeration constant defined in Report so iterating over Bills is performed in the order specified by this constant and i have have no clue where to start.
this is what i have so far, let me know what i did wrong and what i should do next.

What you did wrong was to start writing code without thinking about what you should do.

The single most important thing a programmer needs to do is NOT WRITE CODE. Before committing a single byte to file, you should think through the problem in very NON-java terms. you should pretend you are explaining to a child what needs to be done.

Only when you've done that, revised it and simplified it several times, should you write any actual code.

So I would say your next step is to delete what you have, turn off your computer, and put pencil to paper.


is it really that badly done, this is my second semester in programming and my teacher doesnt explain much, im not sure how i can revise this if im not even sure what this is, i never used iterators. i never set up a enumerator to perform sorting. i need some helpful advise
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike karl wrote:is it really that badly done,

Fraid so; although I think I understand what you were attempting to achieve.

this is my second semester in programming and my teacher doesnt explain much, im not sure how i can revise this if im not even sure what this is, i never used iterators. i never set up a enumerator to perform sorting. i need some helpful advise

And you've already received it. Most of us have BTDTGTTS, so unless you haven't shown any effort (which you plainly have), we're not trying to belittle you.

The fact is that Fred's absolutely right, you've decided on an implementation (your code) before you've understood the problem - and that's always a bad sign - and now you're in the position of trying to defend the indefensible.

My advice: StopCoding (←click) before you have too much invested in this to turn back.

There are plainly some good ideas in your attempt; it just happens to be wrong.

Here's a possibility for you: Have your enum (one of your good ideas) implement java.util.Comparator<Bill>. Have a think about it and see if it makes any sense.

Winston
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike karl wrote:

This certainly looks wrong; it's an unbounded recursive loop. The method iterator() calls itself, which calls itself, which calls itself, which calls itself, etcetera, until it crashes with a StackOverflowError.
 
mike karl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
mike karl wrote:is it really that badly done,

Fraid so; although I think I understand what you were attempting to achieve.

this is my second semester in programming and my teacher doesnt explain much, im not sure how i can revise this if im not even sure what this is, i never used iterators. i never set up a enumerator to perform sorting. i need some helpful advise

And you've already received it. Most of us have BTDTGTTS, so unless you haven't shown any effort (which you plainly have), we're not trying to belittle you.

The fact is that Fred's absolutely right, you've decided on an implementation (your code) before you've understood the problem - and that's always a bad sign - and now you're in the position of trying to defend the indefensible.

My advice: StopCoding (←click) before you have too much invested in this to turn back.

There are plainly some good ideas in your attempt; it just happens to be wrong.

Here's a possibility for you: Have your enum (one of your good ideas) implement java.util.Comparator<Bill>. Have a think about it and see if it makes any sense.

Winston


ok thanks ill see what i can do, just understand i am in Poland and they dont explain well enough, i feel like i am being rushed through alot of material but maybe its just me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic