Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 8, Problem 13E

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

Blurred answer
Students have asked these similar questions
Consider a call centre with three personnel levels: responder, manager, and director. An incoming phone call must first be assigned to a responder who is available. If the respondent is unable to handle the call, it must be escalated to a management. If the manager is unavailable or unable to address the situation, the call should be escalated to a director. Create the classes and data structures needed for this problem. Create a dispatchCall () method that allocates a call to the first available employee.
Consider a call centre with three personnel levels: responder, manager, and director. An incoming phone call must first be assigned to a responder who is available. If the respondent is unable to handle the call, it must be escalated to a manager. If the manager is unavailable or unable to handle the situation, the call should be escalated to a director. Create the classes and data structures needed for this problem. Create a dispatchCall () method that assigns a call to the first available employee.
(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 Distane

Chapter 8 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 8.2 - Rewrite the definition of the method reset for the...Ch. 8.2 - Can an object be referenced by variables of...Ch. 8.2 - What is the type or types of the variable(s) that...Ch. 8.2 - Prob. 14STQCh. 8.2 - Prob. 15STQCh. 8.2 - Consider the code below, which was discussed in...Ch. 8.2 - Prob. 17STQCh. 8.3 - Prob. 18STQCh. 8.3 - Prob. 19STQCh. 8.3 - Is overloading a method name an example of...Ch. 8.3 - In the following code, will the two invocations of...Ch. 8.3 - In the following code, which definition of...Ch. 8.4 - Prob. 23STQCh. 8.4 - Prob. 24STQCh. 8.4 - Prob. 25STQCh. 8.4 - Prob. 26STQCh. 8.4 - Prob. 27STQCh. 8.4 - Prob. 28STQCh. 8.4 - Are the two definitions of the constructors given...Ch. 8.4 - The private method skipSpaces appears in the...Ch. 8.4 - Describe the implementation of the method drawHere...Ch. 8.4 - Is the following valid if ShapeBaSe is defined as...Ch. 8.4 - Prob. 33STQCh. 8.5 - Prob. 34STQCh. 8.5 - What is an advantage of having the main...Ch. 8.5 - What Java construct allows us to define and...Ch. 8 - Consider a program that will keep track of the...Ch. 8 - Implement your base class for the hierarchy from...Ch. 8 - Draw a hierarchy for the components you might find...Ch. 8 - Suppose we want to implement a drawing program...Ch. 8 - Create a class Square derived from DrawableShape,...Ch. 8 - Create a class SchoolKid that is the base class...Ch. 8 - Derive a class ExaggeratingKid from SchoolKid, as...Ch. 8 - Create an abstract class PayCalculator that has an...Ch. 8 - Derive a class RegularPay from PayCalculator, as...Ch. 8 - Create an abstract class DiscountPolicy. It should...Ch. 8 - Derive a class BulkDiscount from DiscountPolicy,...Ch. 8 - Derive a class BuyNItemsGetOneFree from...Ch. 8 - Prob. 13ECh. 8 - Prob. 14ECh. 8 - Create an interface MessageEncoder that has a...Ch. 8 - Create a class SubstitutionCipher that implements...Ch. 8 - Create a class ShuffleCipher that implements the...Ch. 8 - Define a class named Employee whose objects are...Ch. 8 - Define a class named Doctor whose objects are...Ch. 8 - Create a base class called Vehicle that has the...Ch. 8 - Create a new class called Dog that is derived from...Ch. 8 - Define a class called Diamond that is derived from...Ch. 8 - Prob. 2PPCh. 8 - Prob. 3PPCh. 8 - Prob. 4PPCh. 8 - Create an interface MessageDecoder that has a...Ch. 8 - For this Programming Project, start with...Ch. 8 - Modify the Student class in Listing 8.2 so that it...Ch. 8 - Create a JavaFX application that uses a TextField...Ch. 8 - Prob. 10PP
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning