A checkout method in a Java shop application should apply whatever discount rule it is given to every price in a cart. Put the following steps for writing and using a named-class function object in the order they must happen, from first to last.
1.
Create an instance with new TenPercentOff() and store it in a variable of type PriceRule.
2.
Call checkout(prices, rule), passing the stored instance as the second argument.
3.
Write a class TenPercentOff that implements PriceRule and overrides apply to return 0.9 * price.
4.
Declare an interface PriceRule with one abstract method double apply(double price).
5.
Inside checkout, call rule.apply(p) for each price p and add up the returned values.
Unlock full course notes by signing up
Shared references to a running total
A warehouse records scanned units using this Java function object. Each invocation adds its argument to the object's stored unit count and returns the updated count.
The following statements execute in the order shown:
Determine middle and last. Give both answers in units as exact integers.
Independent capacity rules
A storage allocator uses function objects to accept or reject requests for whole storage blocks. Each object retains its own remaining capacity. The method returns 1 for an accepted request and 0 for a rejected request.
Two separate objects are constructed:
Execute the calls in table order. Enter each returned integer and the remaining capacity of the object called immediately afterward. All entries are exact integers; capacities are in blocks.
Can two invocations become one?
A Java program records how often an adjustment rule is invoked. Every fresh CountingOffset object starts with a stored call count of zero.
Consider each statement independently, starting with fresh objects as specified. Which statements are true? Select all that apply.
Composing and repeating function objects
The following Java code builds new function objects from existing ones. The method compose returns a function object that first applies g and then applies f to that result, and applyTwice invokes a given function object two times in a row.
Evaluate each of the following expressions and give each answer as an integer: h.apply(4), k.apply(4), applyTwice(h, 2) and applyTwice(k, 1).