Explanation of Solution
Implementation of the derived class “CombinedDiscount”:
The implementation of the “CombinedDiscount” class derived from the “DiscountPolicy” abstract class is given below:
- • Declare the “p1”, and “p2” variables.
- • Define the constructor.
- ○ Set the values to the declared variables.
- • Define the “computeDiscount” method.
- ○ Declare the required variables.
- ○ Call the “computeDiscount” method with different variables.
- ○ Return the maximum value by checking “discount1” and “discount2” values.
- • Define the main method.
- ○ Create the objects for the “CombinedDiscount”, “BuyNItemsGetOneFree”, and “BulkDiscount” classes.
- ○ The “for” loop through 1 to10 numbers.
- ■ Call the “computeDiscount” method with objects and display the output.
The implementation of the “BulkDiscount” class derived from the “DiscountPolicy” abstract class is given below:
- • Declare the “min”, and “percentOff” variables.
- • Define the constructor.
- ○ Set the values to the declared variables.
- • Define the “computeDiscount” method.
- ○ If the purchase count is less than minimum, then calculate the “discount” value.
- ○ Otherwise make “discount” as 0.
- • Finally return the “discount” value.
- • Define the main method.
- ○ Create an object for the “BulkDiscount” class.
- ○ Call the “computeDiscount” method with different parameters and display the output.
The implementation of the “BuyNItemsGetOneFree” class derived from the “DiscountPolicy” abstract class is given below:
- • Declare the “x” variable.
- • Define the constructor.
- ○ Set the values to the declared variable.
- • Define the “computeDiscount” method.
- ○ Declare the required variables.
- ○ Calculate the “a” and “discount” values.
- ○ Return the “discount” value.
- • Define the main method.
- ○ Create an object for the “BuyNItemsGetOneFree” class.
- ○ The “for” loop through 1 to10 numbers.
- ■ Call the “computeDiscount” method with the parameter and display the output.
The creation of the abstract class “DiscountPolicy” is given below:
- • Declare the abstract method “computeDiscount” along with the two parameters “count” and “itemCost”.
- ○ This method compute and return the discount for the purchase of a given number of single item.
Program:
Filename: CombinedDiscount.java
//definition of "CombinedDiscount" class
public class CombinedDiscount extends DiscountPolicy
{
//declare the required variables
private DiscountPolicy p1;
private DiscountPolicy p2;
//definition of constructor
public CombinedDiscount(DiscountPolicy first, DiscountPolicy second )
{
//set the values
p1 = first;
p2 = second;
}
//definition of "computeDiscount" method
public double computeDiscount(int count, double itemCost)
{
//declare the required variables
double discount1;
double discount2;
//call the methods with different variables
discount1 = p1.computeDiscount(count, itemCost);
discount2 = p2.computeDiscount(count, itemCost);
//check the condition
if(discount1 > discount2)
//return the value
return discount1;
//otherwise
else
//return the value
return discount2;
}
//definition of main method
public static void main(String[] args)
{
//create the objects for the classes
DiscountPolicy buy = new BuyNItemsGetOneFree(3);
DiscountPolicy bulk = new BulkDiscount(5, 30...
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Create an abstract class named Course that can be used with course allocation mechanism of an enterprise. The Course class should track the course Id Number, course Title and course Credit Hours with appropriate accessor/getter and mutator/setter methods. Define an equals (Course obj) method, that returns true if caller and input argument course object have identical course Id Number. Also define an abstract method double getCourseEnrollmentFee(). Next, create two additional classes named ElectiveCourse and CoreCourse that are derived from Course. Finally, create an overridden method named getCourseEnrollmentFee() calculate and returns the total fee for that course. Elective course has Rs. 10,000 per credit hours and Core has Rs. 13,000 per credit hours.arrow_forwardplease answer this for a reviewarrow_forwardI need the answer as soon as possiblearrow_forward
- This is the question: Design and implement a class called Bug, which represents a bug moving along a horizontal wire. The bug can only move for one unit of distance at a time, in the direction it is facing. The bug can also turn to reverse direction. For your design, create a UML Class diagram similar to Figure 5.5 on page 180 of the textbook. Note that you need to include the constructor in the methods section if you code a constructor. Bug will require a toString method to return the current position and which direction the bug is facing to the driver so it can be output.Hint: Remember that a horizontal line has a zero position in the middle with positive to the right and negative to theleft. Consider that a bug will land on the wire at some point before starting along the wire.Write an interactive test driver that instantiates a Bug, then allows the user to manipulate it with simple commands like Output (to see the position and direction), Move, Turn, Exit ... single letters work…arrow_forwardConsider the above scenario, where Billing class has composition relationship with Doctor having private instance variables (docName, docID and docFee) and a public getDocID() method, Patient having private instance variables (pName, pID, pDisease), Medicine having private instance variables (medID, medName, medQty, medPrice), and MedicalTest having private instance variables (testID, testName, testPrice). In addition, each class shall have toString method to display its object state. Also define getDocInfo method in Billing class that shall return the doctor. Implement getPaymentAmount() method in Billing class that shall return the total billing amount that includes doctor fee, medicine cost and medical tests fee. Considering the above scenario, write code that shall perform the following in the driver/test class: Get an amount from user. Traverse the ArrayList<Billing> using enhanced for loop. Print the billing details of those bills having total billing amount greater or…arrow_forwardDesign and implement a class called Bug, which represents a bug moving along a horizontal wire. The bug can only move for one unit of distance at a time, in the direction it is facing. The bug can also turn to reverse direction. For your design, create a UML Class diagram . Note that you need to include the constructor in the methods section if you code a constructor. Bug will require a toString method to return the current position and which direction the bug is facing to the driver so it can be output Write an interactive test driver that instantiates a Bug, then allows the user to manipulate it with simple commands like Output (to see the position and direction), Move, Turn, Exit ... single letters work just fine. All output should be via the driver not methods within Bug. You should use this driver to create screenshot exhibits for a number of scenarios (e.g., output original position, move a few times, output, move a few more times, output, turn, output, move, output, etc.).…arrow_forward
- ... 5. Extend the Rectangle class by adding and implementing the special operator methods for the six relational operations (=, !-, , >=). The relational operators should logically compare the positions of two rectangles on the canvas by comparing the corresponding x- and y-coordinates. For example, the less than operator should determine whether the x- and y-coordinates of one rectangle are less than the x- and y-coordinates of a second rectangle. rectangle.py 1 class Rectangle : 2 #323 # Initializes a Rectangle object. # @param x the x-coordinate for the upper-left corner of the rectangle # @param y the y-coordinate for the upper-left corner of the rectangle # éparam width the width of the rectangle # éparam height the height of the rectangle 3 4 5 6 8 9 10 11 12 13 _init_(self, x, y, width, height): self._x = x self. y = y self. width = width self._height = height def 14 def lt (self, rhs): 15 16 17 18 19 20 ... # Include the new special operator methods here. 21arrow_forwardThe State Patrol Ticket-Processing System (Revisited 2. List the classes that would be involved in the use cases and decide which class should be responsible for collaborating with the other classes for the use case Record a traffic ticket. Consider some possibilities: A Driver object should be responsible for recording his/her ticket, the Officer object should be responsible for recording the ticket that he or she writes, and a Ticket object should be responsible for recording itself.arrow_forwardCan the answer be in more classes then one where the classes will have relationships with other classes through the personarrow_forward
- Consider a class BankAccount that has • Two attributes i.e. accountID and balance and• A function named balanceInquiry() to get information about the current amount in the account Derive two classes from the BankAccount class i.e. CurrentAccount and the SavingsAccount. Both classes (CurrentAccount and SavingsAccount) inherit all attributes/behaviors from the BankAccount class. In addition, followings are required to be the part of both classes• Appropriate constructors to initialize data fields of base class• A function named amountWithdrawn(amount) to withdraw certain amount while taken into account the following conditionso While withdrawing from current account, the minimum balance should not decrease Rs. 5000o While withdrawing from savings account, the minimum balance should not decrease Rs. 10,000• amountDeposit(amount) to deposit amount in the accountIn the main() function, create instances of derived classes (i.e. CurrentAccount and SavingsAccount) and invoke their respective…arrow_forward4. Consider a Person class (which keeps track of a Person’s name) and a Student class (which keeps track of a Student’s name and student ID number). State the relationship between the two classes as either an is-a or a has-a relationship (similar to the last sentence of each paragraph in the above Model).arrow_forwardConsider the class Movie that contains information about a movie. The class has the following attributes: . The movie name • The SA Film and Publication Board (FPB) rating (for example, A, PG, 7-9 PG, 10-12 PG, 13, 16, 18, X18, XX) 1 • The number of people that have rated this movie as 1 (Terrible) • The number of people that have rated this movie as 2 (Bad) • The • The • The number of people that have rated this movie as 3 (OK) number of number of people that have rated this movie as 4 (Good) people that have rated this movie as 5 (Great) The class Movie you created, can be implemented more efficiently if we use an array instead of 5 different member variables to count the number of people rating a movie at a specific score. Adjust your class Movie to use an array to count the score ratings for a movie, and test it with the same main function.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning