 In order to help you solve the electric car charging portion of the assignment for Chapter 5, let's solve a similar problem. Here we have code that calculates the price of key rings from two companies. One company charges $1.50 each with a 5% discount. The other company charges $1.75 each with a 10% discount. We can compile and run this program and buy 10 key rings, and there's the prices. The program works, but as you can see there's a lot of duplicated code here. What we'd like to have is a method that calculates the cost and eliminates the duplication. Your first thought might be to separate the first company's code into a method. Let's call it CalCost and it will take the number purchased as its parameter. We'll copy and paste this code here and return the result. Then here in our code we'll call CalCost with the number purchased. Let's compile that and run it so far so good. But now what do we do for the second company? Do you want to create another method? We could do that. Let's call it CalCost2 which also takes the number purchased as its parameter. Put the code in there and return that result. And then here our cost2 will be CalCost2 with the number purchased. Compile it and run it and it works. But the problem is we haven't eliminated the duplication. You might say no problem. We'll just combine both of these into one method. Compile it and this time we get unreachable statement. The reason that happens is because once we do the return in line 15 the method is finished. We can never get the code here. Well let's try and return both costs at the same time. Java won't let us do that either. So how are we going to solve this problem? Let's go back to our original code. The only thing that's different between the two calculations are the unit price and the percent. Instead of having just the number purchased as our parameter, let's also make the unit price and the percent into parameters. Now our method looks like this. We're going to calculate the cost based on the number purchased, the unit price and the discount percent. Our subtotal is going to be the number purchased times the unit price. The discount is going to be the subtotal times the percent divided by 100 and the total cost will be the subtotal minus the discount. And we'll return that cost. We now have a generalized method that can calculate the cost for any company at all as long as we know the unit price and the percentage for that company. Instead of having one method that tries to return two things, what we're going to do is we're going to call the method twice. For the first company, we'll call the calc cost with the number purchased. The unit price will be $1.50 and the percent will be $5. For the second company, we'll call the same method again with the same number of key rings purchased, but this time our unit price will be passed in as $1.75 and our discount percent will be passed in as $10. Instead of having a method that tries to return two things, we have a method that returns one thing and we call it twice using different values for the arguments. Let's compile and run it and it works and we've gotten rid of the duplicated code. Now let's examine the car charging problem and start by solving the problem by hand. We'll use simpler numbers here so we can do all the calculations in our head. Let's say we have one company that charges $0.10 per minute for the first 15 minutes and $0.30 for every minute after 15. If we charge the car for 10 minutes, how much do we pay? 10 minutes is less than 15 minutes and paying 10 minutes at $0.10 per minute comes out to $1. What if we charge the car for 20 minutes? Then how much do we pay? 20 minutes is greater than 15 minutes. The first 15 minutes will cost $0.10 per minute, which works out to $1.50, and then there are 20 minus 15, or 5 additional minutes of charging at $0.30 per minute. That gives us another $1.50 and the grand total is $3. Now let's look at company B, where they charge $0.20 per minute for the first 12 minutes and $0.25 for every minute after that. How much do we pay for 10 minutes? That's less than the 12 minute breakpoint, so we pay $0.10 times $0.20 or $2. And for 20 minutes? That's over the 12 minute breakpoint. We pay $0.20 a minute for the first 12 minutes for a total of $2.40, and $0.25 a minute for the remaining 8 minutes, 20 minus 12, for another $2, and the grand total is $4.40. Now we want to generalize these calculations so that they can be used to calculate the cost for any company's chargers. In order to do the calculation in general, we need to know 4 values, the number of minutes, the breakpoint, the number of minutes where the cost changes, the cost per minute before the breakpoint, and the cost per minute after the breakpoint. We make this a generic calculation for any company by using symbols to stand for those values. And that's why the assignment asks you to write the calculate cost method with 4 parameters to represent those values, the number of minutes, the breakpoint, the cost before, and the cost after. And here's the generalized calculation, depending on whether the customer charges their car for more than the breakpoint number of minutes or not. Here's your code.