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 Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)
- Answer the given question with a proper explanation and step-by-step solution. Using Java Create an BankAccount Class that has an account id number (of type integer), balance (of type double), and rate (of type double) as private data. Add get and set methods for these three variables to work with these attributes. Make overloaded constructors to bring in and set all values, and also to just to bring in a customer name and id which uses 0 for the balance. Include error handling in the set for the balance which throws an exception if a change would put the balance below 0. In Main of another class file create 2-3 objects of BankAccount and work with their values and output them to the user to test your new class. Take the BankAccount Class from above and copy it into this program. You will use it as one of the classes in this one. Create a BankCustomer class with 3 BankAccount objects inside it as private variables (of type BankAccount) as well as a customer name (of type String).…arrow_forwardIn an Institute, faculties are planning to conduct activities for students [like workshop, seminar, codeathon …] as well as for Faculties [Training Programs, Faculty Development Programs, …] Define a class for the same with appropriate data members and also inherit to Student and Faculty Subclasses. In main method, if two faculties are planning any event on the same day, display warning message.arrow_forwardUML diagram analysis: refer to the following UML diagram of the Loan Class. a) How many instance variables does the TV class have? b) How many methods that return a value does the TV class have? c) How many static methods does the TV class have? d) How many constructors does the TV class have? e) How many defaults data fields does the TV class have?arrow_forward
- For a parking management system, use the Observer pattern to implement this interaction using Java and implement the three classes mentioned below. Implement a ParkingObserver that has the role of the Observer. The Observable classes (Subjects) will be the ParkingLots. It would be helpful if the notify method in the subjects (and update method in the observers) took a parameter, perhaps an object of type ParkingEvent, which could have fields including the lot, timeIn, timeOut (if relevant), and permit. The ParkingObserver should register with each ParkingLot and receive the messages. Once a car enters (in an entry-scan only lot) or leaves (in an entry-scan and exit-scan lot), then the ParkingObserver will be updated, and then can register the charge with the parking system via the TransactionManager’s park() method. Based on these requirements you should implement three actor classes in this interaction using java: TransactionManager, ParkingObserver and ParkingLot. 1. The…arrow_forwardCriticize the following implementation of a class intended to represent account balances. Why is compareTo() a flawed implementation of the Comparable interface? public class Balance implements Comparable { ... private double amount; public int compareTo(Balance that) { if (this.amount < that.amount - 0.005) return -1; if (this.amount > that.amount + 0.005) return +1; return 0; } ... }arrow_forwardConsider the case of a shop that sells CDs in cash. The CDs are of three types: Movie CDs, Software CDs and Music CDs. For each of the following CD types, the required attributes are:Movie CD: Movie Title, Year of Release, Lead Actor Name, CD price and CD quantitySoftware CD: Software Name, Edition, Year of Release, CD price and CD quantityMusic CD: Album Title, Year of Release, Number of Songs, Format, CD price and CD quantity All objects of the classes cannot change their state once they are instantiated. How will you implement this?arrow_forward
- a) Implement a class Point with three attributes, x, y, and z.b) Implement an init method with an optional parameter type: 1) Set the default value of x, y, and z to 0.c) Implement a display method to print the values of x, y, and z as the example output below.d) Instantiate two objects of type Point, one with arguments,1, 2, and 3, and the other one without any arguments.e) Call display() to print x, y, and z. Example Output(x, y, z): (1, 2, 3)(x, y, z) : (0, 0, 0)arrow_forwardIN C++ Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Provide another parameterized constructor which takes both efficiency and fuel as parameters. Also supply methods getGasInTank, returning the current amount of gasoline in the fuel tank. getEfficiency() return the efficiency of car. setGasInTank(double fuel), to add gasoline to the fuel tank. setEfficiency(double efficiency) Supply a method Drive () that simulates driving the car for a certain distance, reducing the amount of gasoline in the fuel tank according to its efficiency. Sample usage: Car myHybrid (50); // 50 miles per gallon myHybrid.addGas(20); // Tank 20 gallons myHybrid.Drive(100); // Drive 100 miles double gasLeft = myHybrid.getGasInTank(); // Get gas remaining in tank Supply a main method that tests…arrow_forwardUsing the Java language, write a definition for the PERSON class to represent a person's name and another variable to represent his age, and a constructor method that receives data values upon creation. The definition also includes change methods and retrieval methods for each variable of the class. In addition, the class definition contains a RESET method to change the value of both variables with new values that are received from during transactionsarrow_forward
- Using Python, create a program that has the following classes: Point - Just like in the lecture, with attributes x and y set to 0 by default. Circle - Contains two attributes, center and radius. center is of type Point defined above (initialize a Point instance for this), while radius is set to 0 by default. The program must have the function point_in_circle(c, p) where c is a Circle and p is a Point. The function returns True if the point is within the circle, and False otherwise. You may use the math module (and nothing else) if necessary. You may also create other functions that can help you code the main function much easier. Example usage # Initialize a circle with center at (0, 0) and radius 1 (unit circle). c = Circle() c.center.x, c.center.y, c.radius = 0, 0, 1 # Initialize a point located at (1, 1) p = Point() p.x, p.y = 1, 1 # This returns False since (1, 1) is outside the circle point_in_circle(c, p) hint: apply "classes" conceptarrow_forwardWhy is the ability to redefine a method important in object-oriented programming?Choose all options that apply.(a) Because it allows us to add extra work to a method.(b) Because it allows us to introduce abstract methods that are redefined as concrete methods.(c) Because it allows us to provide a more accurate or faster definition in a subclass.(d) Because it allows us to disable a method in a subclass.(e) Because it allows us to change the meaning of a method.arrow_forwardConsider an online holiday booking system. The software developers have identified the classes, Booking, Individual_Booking and Group_Booking via inheritance in their initial design. Which of the following should have a hollow triangle pointed to in the object-oriented design? a. Group_Booking b. Either Individual_Booking or Group_Booking c. Individual_Booking d. Bookingarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education