2009 Battery Charger
<< 2009StockpileCritter | APQuestionsTrailIndex | 2009NumberTile >>
BatteryCharger.java
/**
*
* @version 11 May 2009
*/
public class BatteryCharger
{
/** rateTable has 24 entries representing the charging costs for hours 0 through 23. */
private int[] rateTable={50,60,160,60,80,100,100,120,150,150,150,200,40,240,220,220,200,200,180,180,140,100,80,60};
public BatteryCharger()
{
// There may be instance variables, constructors, and methods that are not shown.
}
/** 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 <= startHour <= 23
* @param chargeTime the number of hours the battery needs to be charged
* Precondition: chargeTime > 0
* @return the total cost to charge the battery
* --made public for testing purposes
*/
public int getChargingCost(int startHour, int chargeTime)
{ /* to be implemented in part (a) */
int result=0;
return result;
}
/** 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 <= returned value <= 23
*/
public int getChargeStartTime(int chargeTime)
{ /* to be implemented in part (b) */
int bestTime=0;
return bestTime;
}
}
BatteryChargerTester.java
/**
* Tests the BatteryCharger class
*
* @author ChrisThiel
* @version 11 May 2009
*/
public class BatteryChargerTester
{
public static void main (String[] args)
{
BatteryCharger b = new BatteryCharger();
System.out.println("Testing part a:");
System.out.println("start at 12 for 1 hour (should be 40): "+b.getChargingCost(12,1));
System.out.println("start at 0 for 2 hours (should be 110): "+b.getChargingCost(0,2));
System.out.println("start at 22 for 7 hours (should be 550): "+b.getChargingCost(22,7));
System.out.println("start at 22 for 30 hour (should be 3710): "+b.getChargingCost(22,30));
System.out.println("Testing part b:");
System.out.println("best for 1 hour(should be 12): "+b.getChargeStartTime(1));
System.out.println("best for 2 hours(should be 23 or 0): "+b.getChargeStartTime(2));
System.out.println("best for 3 hours(should be 23): "+b.getChargeStartTime(3));
System.out.println("best for 4 hours(should be 22): "+b.getChargeStartTime(4));
System.out.println("best for 7 hours(should be 22): "+b.getChargeStartTime(7));
System.out.println("best for 30 hours(should be 22): "+b.getChargeStartTime(30));
}
}
