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 (7th Edition)
- Consider a class named Calculator that contains Two data members i.e. num1 and num2 A parameterized constructor to assign values to both data members Four functions i.e. addition(), subtraction(), multiplication(), and division() to perform their respective functions on data members and return result in double An input() can also be used to get values for num1 and num2 during execution of the program. In main() use do while loop having switch statement inside to execute the functions of class accordingly. There should be options such as ‘0’ to exit, ‘+’ to add, ‘-‘ to subtract, ‘*’ or ‘x’ to multiply, ‘/’ or ‘D’ to divide and ‘r’ or ‘R’ to run again. Comment your program properlyarrow_forwardAssume that CSCourse class has been written, which has the below UML Diagram. You are giyen the CourseManager class and are asked to complete the main method. Write a body of the main method only that creates two objects of type CSCourse, so that: the first CSCourse is named "CS210% and has 32 students and an average GPA of 3.1 the second CSCourse is named "CS211" and has 31 students and an average GPA of 3.4 sum the students in two courses using the getNumStudents() method, and print the sum to the screen. C) cSCourse name : String -numStudents: int -averageGPA double +CSCourse(name String, numStu int) +getNumStudents() int +getName(): String +setAverageGPA(AVGPA: double) : void public class CourseManager{ public static void main(String[] args) [ / Your code goes here // Sample output is "Sum of students: 63".arrow_forward(3) On the line provided in the main method, write one line of code todeclare the variable average by amounts received as parameters (in that order). See example below (→) of a call to this method. (1) Write the instance method addStats() that increases a runningback's total yards and total carries Consider the class Runningback and its client (main) shown below. Add the following code to this program: (2) Write the instance method ypc () which returns to the client program the number of yards per 6. carry that a running back has attained G.e. number vards divided by number of carries) and assign to it the average number of vards Barkley has gained per carry. This line oJ code should have humber literals in it, actual number public class Runningback { private int yards; private int carries; public Runningback(int yds, int car) { yards = yds; carries = car; //Write the two methods described above: Public vord adJStars (double yards) yard Distanearrow_forward
- 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_forwardDesign and code a class in C++ that represents a single month of the year. It should have a single integer-typed data member to represent the month. Make sure to provide proper construction, access, and mutation for this member as well as input and output methods. In addition, methods for comparing two months chronologically for ordering would be useful as would a method to advance to the next month (note that January follows December and begins the cycle anew) or previous month. In fact, thinking about it, it might be good to be able to advance to a month an arbitrary number of whole months in the future or past: January '+' 5 = June; March '-' 5 = October. Place your Month class in a library. Write a test application (driver) for your class. Oh, and the programmers who commissioned this class wanted to make sure it could handle not only integer-valued representation of a Month but also either whole-word names or three-letter abbreviations for the Months. That means that they should…arrow_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_forwardCan the answer be in more classes then one where the classes will have relationships with other classes through the personarrow_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_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning