In: Computer Science
This Code Is Supposed To Be Performed In JAVA
1.) Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Create a driver class that tests this class and provide the UML.
2.) In a separate program, define DiscountPolicy as an interface instead of the abstract class. Create a driver class that tests this class and provide the UML.
Perform The Following
1.) Create an abstract class DiscountPolicy with a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item.
2.) Create a driver class
3.) Create an interface class DiscountPolicy with a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item.
4.) Create a driver class or use the same driver class that you created earlier
Short Summary:
Source Code:
1) Create an abstract class DiscountPolicy with a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item.
DiscountPolicy.java:
/**
*
* Abstract class DiscountPolicy
*
*/
public abstract class DiscountPolicy {
// abstract method with two parameters
public abstract double computeDiscount(int count,
double itemCost);
}
DiscountPolicyDerive.java:
/**
*
* Class DiscountPolicyDerive that derives abstract class
*
*/
public class DiscountPolicyDerive extends DiscountPolicy {
// variable to store the number of items
private int numberOfItems;
public DiscountPolicyDerive(int itemsTotal) {
this.numberOfItems =
itemsTotal;
}
// computeDiscount that calculates the discount
@Override
public double computeDiscount(int count, double
itemCost) {
double discount = 0;
if (count > numberOfItems)
discount =
(count / numberOfItems) * itemCost;
return discount;
}
}
DiscountPolicyDemo.java:
/**
*
* Driver class DiscountPolicyDemo
*
*/
public class DiscountPolicyDemo {
public static void main(String[] args) {
// Creating object by passing the
number of items
DiscountPolicy discountDerive = new
DiscountPolicyDerive(2);
// calling the abstract
method
double discount =
discountDerive.computeDiscount(10, 20.5);
// displaying the discount
amount
System.out.print("Discount Amount :
" +discount);
}
}
SAMPLE OUTPUT:
UML DIAGRAM:
2.) In a separate program, define DiscountPolicy as an interface instead of the abstract class. Create a driver class that tests this class and provide the UML
DiscountPolicyInterface.java:
/**
* interface DiscountPolicy
*/
public interface DiscountPolicyInterface {
double computeDiscount(int count, double
itemCost);
}
DiscountPolicyDerive.java:
/**
*
* Class DiscountPolicyDerive that derives abstract class
*
*/
public class DiscountPolicyDerive implements
DiscountPolicyInterface {
// variable to store the number of items
private int numberOfItems;
public DiscountPolicyDerive(int itemsTotal) {
this.numberOfItems =
itemsTotal;
}
// computeDiscount that calculates the
discount
@Override
public double computeDiscount(int count, double
itemCost) {
double discount = 0;
if (count >
numberOfItems)
discount =
(count / numberOfItems) * itemCost;
return discount;
}
}
DiscountPolicyDemo.java:
/**
*
* Driver class DiscountPolicyDemo
*
*/
public class DiscountPolicyDemo {
public static void main(String[] args) {
// Creating object by passing the
number of items for the class
DiscountPolicyDerive discountDerive
= new DiscountPolicyDerive(2);
// calling the abstract
method
double discount =
discountDerive.computeDiscount(10, 20.5);
// displaying the discount
amount
System.out.print("Discount Amount :
" +discount);
}
}
SAMPLE RUN:
UML DIAGRAM:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************