Reputation: 41
I have an assignment that I'm not quite sure where to start. This is what I'm supposed to do.
This is how I began to set it up. I want to make my methods and parameters are in the correct locations and am wondering where I define the parameters that my teacher wants me to pass.
public class Ex1012 {
public static void main(String[] args) {
// TODO Auto-generated method stub
DiscountPolicy bulk = new BulkDiscount();
System.out.println();
DiscountPolicy bngo = new BuyNItemsGetOneFree();
}
}
public abstract class DiscountPolicy {
abstract void computeDiscount(int count, float itemCost){
return discount;
}
}
public class BuyNItemsGetOneFree extends DiscountPolicy {
BuyNItemsGetOneFree() {
}
BuyNItemsGetOneFree(int n){
DiscountPolicy.computeDiscount(int count, float itemCost);
//set n to a variable here??
//calculations go here
//Where to set count and itemCost??
}
}
public class BulkDiscount extends DiscountPolicy {
public BulkDiscount(int minimum, float percent){
if (quantity > minimum){
super.ComputeDiscount(int count, float itemCost);
//calculations go here
//Where to define count, itemCost, minimum, and percent??
}
}
}
I'm simply worried about the relationship between the classes and the parameters themselves because I get confused once I have multiple classes such as these. Any insight would be greatly appreciated. Thanks!
Upvotes: 0
Views: 1644
Reputation: 18344
DiscountPolicy
is a base abstract class. This, in your case, gives a structure for a class that represents some type of Discount: all such classes should have a method computeDiscount, and this method would calculate the discount based on the respective policies. This is a contract that all classes representing a DiscountPolicy has to follow.
DiscountPolicy
itself does not give any logic for calculating a discount (no "default policy"). Every class has to provide their own logic. You enforce it by making the computeDiscount
method abstract
.
An abstract method in java does not have any body. It is just the signature: just the contract (structure) and no implementation.
So the computeDiscount in your DiscountPolicy class should look like (Note the ;
at the end of the signature itself. Also no {}
) :
abstract float computeDiscount(int count, float itemCost);
Also, this method will return the discount for the purchase of a given number of a single item, so the return type should be a float
and not void
The BuyNItemsGetOneFree
and BulkDiscount
classes, being subclasses of DiscountPolicy
should implement the computeDiscount
method. The logic in the computeDiscount
methods of the two sub classes will be different, based on the discount calculation logic for bulk discount and the buy n get 1 free (this logic is given in your exercise).
class BulkDiscount extends DiscountPolicy
{
//Same signature as computeDiscount of DiscountPolicy
//Not abstract, no semicolon at end of signature, has body.
//Also called "Concrete" method
float computeDiscount(int count, float itemCost)
{
//The logic as given in the exercise.
//Return the discount calculated by the logic
}
}
You test these as (in Ex1012
's, main
method):
DiscountPolicy bulk = new BulkDiscount();
float discount = bulk.computeDiscount(10, 1); //Data used to test
DiscountPolicy bngo = new BuyNItemsGetOneFree();
float discount = bngo.computeDiscount()
Upvotes: 0
Reputation: 3865
Firstly, abstract methods do not have body.
Secondly as it should compute discount it should return it so computeDiscount canot be void it should in your case return float.
So DiscountPolicy should look like
public abstract class DiscountPolicy {
abstract float computeDiscount(int count, float itemCost);
}
Furthermore you can not use
DiscountPolicy.computeDiscount(int count, float itemCost);
as computeDiscount method is not static method, it is even not concrete.
Just a note, java is case sensitive language so you should be using stuff as you declared them, computeDiscount and ComputeDiscount are two different things.
Upvotes: 0
Reputation: 88707
Abstract methods may not have a body, so your definition of computeDiscount(...)
should be:
abstract void computeDiscount(int count, float itemCost);
In each of the concrete classes that extend the abstract class, you'd then have to implement that method. Generally, abstract methods behave like methods defined in interfaces to some extent (they are declared but without a default implementation), yet still have differences (can be protected or package private as well, can only be implemented by subclasses etc.).
In most cases you have an abstract class that provides some default logic and just requires the subclasses to fill in some "holes" that depend on the concrete implementation.
So basically, you store the parameters to BuyNItemsGetOneFree
and BulkDiscount
as instance variables and use them when computeDiscount(...)
is called. You're calling it in the constructor, which is most likely the wrong place. I guess your main should call the method on the objects you create directly, e.g.
DiscountPolicy bngo = new BuyNItemsGetOneFree(5);
double discountForFour = bngo.computDiscount(4,4.95f);
double discountForFive = bngo.computDiscount(5,4.95f);
Note that your computeDiscount(...)
method should return a value, according to your assignment:
...a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item...
Edit:
//Where to set count and itemCost??
As I said above, you don't "set" (store) them, but use them for the calculation only.
Upvotes: 2
Reputation: 32949
You need to review how to define abstract methods. An abstract method does not have a body "{}". It should be defined in sub-classes. The sub-classes do the calculation of the discount generally when the base class calls the abstract method.
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html
Upvotes: 1