• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Abstract classes doubt

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got stuck in this exercise about abstract classes. These are 4 different exercises. The problem is in number 13 (although if you spot any other error do let me know).






Now, here I don't know if I'm misunderstanding what the author wants but I'm not sure how to approach this.

In CombinedDiscount's version of computeDiscount, should I call BulkDiscount and BuyNGetOneFree version of computeDiscount and then return the largest discount? If so, how would I call those methods from this sibling (?) class? Will I have to create a temp object from each of those classes and then call the methods using those objects?



Thank you in advance guys.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Johan Mena wrote:In CombinedDiscount's version of computeDiscount, should I call BulkDiscount and BuyNGetOneFree version of computeDiscount and then return the largest discount?


Yes, that's exactly what the instructions say:

Johan Mena wrote:Derive CombinedDiscount from DiscountPolicy, as described in Exercise 10. It should have a constructor that has two parameters of type DiscountPolicy. It should define the method computeDiscount to return the maximum value returned by computeDiscount for each of its two private discount policies.: BulkDiscount and BuyNItemsGetOneFree.



Johan Mena wrote:If so, how would I call those methods from this sibling (?) class? Will I have to create a temp object from each of those classes and then call the methods using those objects?


No, why do you think you have to create a temporary object? You already have references to the two other discounts, in the member variables first and second. You can just call the computeDiscount method on those two, and then return the largest of the two answers.

Do you understand how to call a method on an object? It's just: variable name, dot, method name, arguments:

 
Johan Mena
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:You already have references to the two other discounts, in the member variables first and second. You can just call the computeDiscount method on those two, and then return the largest of the two answers.



For some reason I wasn't realizing this. Thanks for pointing it out. Solved.
 
Marshal
Posts: 80617
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the bogof (=buy one, get one free) class, why have you got the > test? What does it achieve which would not happen in its absence? I think it is unnecessary.
If you want some sort of test, maybe a >= 0 test would be more useful, to prevent anybody trying to calculate a discount on -1 items.
 
Johan Mena
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:In the bogof (=buy one, get one free) class, why have you got the > test? What does it achieve which would not happen in its absence? I think it is unnecessary.
If you want some sort of test, maybe a >= 0 test would be more useful, to prevent anybody trying to calculate a discount on -1 items.



Buy N items, get one free.

count (the actual number of items the user buys) has to be greater than the predefined numberOfItems in order to get a discount every time numberOfItems are bought.
 
Campbell Ritchie
Marshal
Posts: 80617
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it doesn’t. If you buy 4 items and the amount for discount is 5, your formula will calculate 4 / 5 and get 0. See, you had it right already.
 
Johan Mena
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, it doesn’t. If you buy 4 items and the amount for discount is 5, your formula will calculate 4 / 5 and get 0. See, you had it right already.



If 4 items are bought and the amount for discount is 5, the wouldn't even execute and the default discount amount, 0, would be returned.

 
Ranch Hand
Posts: 58
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This should probably be giving you some error. Declaring the type as you have done above, creates local variables which end up hiding your class variables of the same name.
 
Johan Mena
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Sykes wrote:

This should probably be giving you some error. Declaring the type as you have done above, creates local variables which end up hiding your class variables of the same name.



Thank you. Changed the type of minimum to int and used them instead of creating new local variables.
 
reply
    Bookmark Topic Watch Topic
  • New Topic