• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to implement Halving Carousel?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a superclass called DecrementCarousel which has a method that returns an object called CarouselRun. CarouselRun has its own methods which I need to override in HalvingCarousel, but I don't know how.

Decrement Carousel:


Here are methods in CarouselRun:


This is HalvingCarousel class:


Main Class:


In this exercise I need to extend DecrementingCarousel. I need to implement HalvingCarousel. This subclass must halve elements instead of decrementing it by one. Note that you need to apply regular integer division, discarding the remainder. For example, 5 / 2 = 2.
How can I do that? Any hints?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: Find the method which decrements something, and override it with a method which halves that thing.

But this immediately leads to the question, that code is embedded in a method which does a lot of other stuff unrelated to decrementing or halving. You don't want to duplicate all of that other stuff. So the first step is to fix the code so that decrementing the something is done in a separate method. That makes it easy for the subclass to just override that separate method.

There's also the question of why you have to override DecrementingCarousel by HalvingCarousel. You really shouldn't. A HalvingCarousel is in no way a DecrementingCarousel, since a D--C-- is based on decrementing things and a H--C-- is not. It would be better to have an AbstractCarousel superclass which contains all of the machinery except the bit which modifies a value, and then create the two subclasses D--C-- and H--C--,
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In HalvingCarousel I override the method run from DecrementingCarousel

HalvingCarousel



Here I create one more class to run the code with next() and I override it. But seems that my code its useless. My logic is far from solving this problem.

HalvingCarouselRun:



I get result:
true
-1
true
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
true
-1

But should be:

true
-1
false
20
30
10
10
15
5
5
7
2
2
3
1
1
1
true
-1

How can I replace my code from next() in order to have the right result?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cris Marinescu wrote:How can I replace my code from next() in order to have the right result?



You would have to ask somebody who knew what "Decrementing" means in "DecrementingCarousel". I looked at the code and there was no hint of that. My guess was that the "decrementing" was this code:

but only because there was decrementing going on there. Otherwise there was just code fossicking around in an array for no reason I could determine.

You may feel that I am being over-critical of your code. On the contrary, it's the documentation of the code that I'm criticizing. I see nothing which says what it's supposed to do, nor any comments in the code which say what it is actually doing.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . there was decrementing going on there. . . .

Because OP is using postdecrement, the value retuned is the old value before decrementing.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . there was decrementing going on there. . . .

Because OP is using postdecrement, the value retuned is the old value before decrementing.



True, but that's irrelevant to any of the discussion in this thread.
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Exactly this is "decrementing"
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the subclass would have to contain code which halves instead of decrements. And as I think I mentioned before, that code should really be in its own method so that the subclass could override it without having to duplicate a lot of code from the superclass.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to write the code in the Halving Carousel with out the use of other class ?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am sure you can avoid another class if you want to, but that looks like suspect design to me.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic