Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARETO BE WRITTEN IN JAVA. Notes: Assume that the classes listed in the Quick Reference

image text in transcribedimage text in transcribedimage text in transcribed

1. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARETO BE WRITTEN IN JAVA. Notes: Assume that the classes listed in the Quick Reference found in the Appendix have been imported where appropriate. . Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods may not receive full credit. An electric car that runs on batteries must be periodically recharged for a certain number of hours. The battery technology in the car requires that the charge time not be interrupted. The cost for charging is based on the hour(s) during which the charging occurs. A rate table lists the 24 one-hour periods, numbered from 0 to 23, and the corresponding hourly cost for each period. The same rate table is used for each day. Each hourly cost is a positive integer. A sample rate table is given below. Hour Cost Hour Cost Hour Cost 0 50 8 150 16 200 1 60 9 150 17 200 2 160 10 150 18 180 3 60 11 200 19 180 4 80 12 40 20 140 5 100 13 240 21 100 6. 100 220 22 80 7 120 15 220 23 60 The class Battery Charger below uses a rate table to determine the most economic time to charge the battery. You will write two of the methods for the Battery Charger class. public class Battery Charger ( /** rateTable has 24 entries representing the charging costs for hours through 23. */ private int[] rateTable; /** Determines the total cost to charge the battery starting at the beginning of startHour. @param startHour the hour at which the charge period begins Precondition: o s startHour s 23 @param chargeTime the number of hours the battery needs to be charged Precondition: chargeTime > 0 @return the total cost to charge the battery */ private int getChargingCost(int startHour, int chargeTime) ( /* to be implemented in part (a) */ } /** Determines start time to charge the battery at the lowest cost for the given charge time. @param chargeTime the number of hours the battery needs to be charged Precondition: chargeTime > 0 @return an optimal start time, with 0 S returned values 23 */ public int getChargeStartTime(int chargeTime) { /* to be implemented in part (b) */ ) 1/ There may be instance variables, constructors, and methods that are not shown. ) a. Write the Battery Charger method getCharging Cost that returns the total cost to charge a battery given the hour at which the charging process will start and the number of hours the battery needs to be charged. For example, using the rate table given at the beginning of the question, the following table shows the resulting costs of several possible charges. Start Hour of Total Cost Charge Hours of Charge Time 1 12 40 Last Hour of Charge 12 1 4 (the next day) 3 (two days later) 0 2 7 22 22 110 550 3,710 30 Note that a charge period consists of consecutive hours that may extend over more than one day. Complete method getCharging Cost below. /** Determines the total cost to charge the battery starting at the beginning of startHour. @param startHour the hour at which the charge period begins Precondition: 0 s startHour s 23 @param chargeTime the number of hours the battery needs to be charged Precondition: chargeTime > 0 @return the total cost to charge the battery private int getChargingCost(int startHour, int chargeTime) b. Write the Battery Charger method getChargeStartTime that returns the start time that will allow the battery to be charged at minimal cost. If there is more than one possible start time that produces the minimal cost, any of those start times can be returned. For example, using the rate table given at the beginning of the question, the following table shows the resulting minimal costs and optimal starting hour of several possible charges. Minimum Cost Last Hour of Charge Hours of Charge Time 1 Start Hour of Charge 12 40 12 1 0 2 110 or 7 550 3.710 23 22 22 0 (the next day) 4 (the next day) 3 (two days later) 30 Assume that getCharging Cost works as specified, regardless of what you wrote in part (a). Complete method getChargeStartTime below. /** Determines start time to charge the battery at the lowest cost for the given charge time. @param chargeTime the number of hours the battery needs to be charged Precondition: chargeTime > 0 @return an optimal start time, with 0 S returned values 23 public int getChargeStartTime(int chargeTime)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions