Question

In: Computer Science

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 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

Solutions

Expert Solution

Short Summary:

  • Provided the source code and sample output as per the requirements.

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!!!

**************************************************************************************


Related Solutions

In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
1. Java Abstract Class (2 pts) () • A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.• Following is an example of abstract class:abstract class Book{String title;abstract void setTitle(String s);String getTitle(){return title;}}If you try to create an instance of this class like the following line you will get an error:Book...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the user to choose game tester type...
java code: Problem 1: Create a class called Elevator that can be moved between floors in...
java code: Problem 1: Create a class called Elevator that can be moved between floors in an N-storey building. Elevator uses a constructor to initialize the number of floors (N) in the building when the object is instantiated. Elevator also has a default constructor that creates a five storey building. The Elevator class has a termination condition that requires the elevator to be moved to the main (i.e., first) floor when the object is cleaned up. Write a finalize() method...
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT