Question

In: Computer Science

Create an Interface DiscountPolicy. It should have a single abstract method computeDiscount that will return the...

Create an Interface 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 (int type) and itemCost (double type). Create a class BulkDiscount that implements Interface DiscountPolicy. The BulkDiscount class has two proprieties: theMinimum (int type), and percent (double type). It should have a constructor that sets the parameters, minimum and percent to a given values. It should define the method computeDiscount so that if the quantity (count) purchased of an item is more than minimum, the discount is calculated as count*itemCost*percentOff/100.0, otherwise, discount will be equal=0.0; Write a test program that tests your method. Create at least three objects of BulkDiscount class and print out their discount values. (JAVA)

Solutions

Expert Solution

DiscountPolicy.java


//Interface DiscountPolicy with abstract method computeDiscount
public interface DiscountPolicy {
   public double computeDiscount(int count, double itemCost);
}

BulkDiscount.java


public class BulkDiscount implements DiscountPolicy{
   //member variables
   private int theMinimum;
   private double percent;
  
   //constructor that sets the member variables
   public BulkDiscount(int minimum, double discountPercent)
   {
       theMinimum = minimum;
       percent = discountPercent;
   }
  
   //method that computes and returns the discount amount for a bulk purchase of an item
   public double computeDiscount(int count, double itemCost)
   {
       //declare and set discount amount to 0.0
       double discountAmt = 0.0;
       //if count is greater than minimum
       if(count > theMinimum)
           //compute discount amount
           discountAmt = count * itemCost * percent / 100.00;
       //return discount amount
       return discountAmt;
   }
}

TestBulkDiscount.java


public class TestBulkDiscount {

   public static void main(String[] args) {
       //create three objects of BulkDiscount class with different minimum and discount percent values
       BulkDiscount bulkObj1 = new BulkDiscount(10, 5);
       BulkDiscount bulkObj2 = new BulkDiscount(20, 5.5);
       BulkDiscount bulkObj3 = new BulkDiscount(5, 4.2);
      
       //compute and display discount amount of object 1
       double discountAmount = bulkObj1.computeDiscount(15, 59.95);
       System.out.printf("Discount Amount for bulkObj1: $%.2f",discountAmount);
      
       //compute and display discount amount of object 2
       discountAmount = bulkObj2.computeDiscount(10, 44.45);
       System.out.printf("\n\nDiscount Amount for bulkObj2: $%.2f",discountAmount);
          
       //compute and display discount amount of object 3
       discountAmount = bulkObj3.computeDiscount(20, 75.25);
       System.out.printf("\n\nDiscount Amount for bulkObj3: $%.2f",discountAmount);

   }

}

Output

Code screenshot:


Related Solutions

Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
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. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
Create an interface MessageEncoder that has a single abstract method encode(plainText) where the plainText is the...
Create an interface MessageEncoder that has a single abstract method encode(plainText) where the plainText is the message to be encoded. The method will return the encoded version of the message. Then create a class SubstitutionCipher that implements this interface. The constructor should have on parameter called shift. Define the method encode so that each letter is shifted by the value in shift. For example if shift is 3 a will be replaced by d, b will be replaced by e....
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It...
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...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing. Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount for the purchase of items. DiscountPolicy knows the name of the item and its cost as well as the number of items being purchased. - Class BulkDiscount, derived from DiscountPolicy, has two fields, minimum and percentage. computeDiscount method will return the discount based on the percentage applied, if the quantity of items purchased is more than minimum. For example: if minimum is 3 and...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
Create a method called firstLetter that takes a String parameter and integer parameter. It should return...
Create a method called firstLetter that takes a String parameter and integer parameter. It should return -1 if the number of words in the given String is greater than or equal to the integer parameter (it should return -1 and not process the String any further) (4 points). If the String does not have more words than the given integer, the method should return 1 if all the words in the string start with the same letter(8 points) and 0...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite of whats below). instead of finding the the best paid convert it find the lowest paid person. Given the Company and FullTimeEmployee classes and the Employee interface, write a Java program that reads in a file of full time employees with each line of input containing the name of the employee (String) and gross Salary (double) and stores them in an array list. Then...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite of whats below). instead of finding the the best paid convert it find the lowest paid person. write a Java program that reads in a file of full time employees with each line of input containing the name of the employee (String) and gross Salary (double) and stores them in an array list. Then the arraylist of stored employees, find and print the less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT