Question: Hello. I am stuck with my first java assignment. My instructor states that all info needed is in the javadocs, only 8 instance variables necessary,
Hello. I am stuck with my first java assignment. My instructor states that all info needed is in the javadocs, only 8 instance variables necessary, as many local variables as needed within reason, and use accessors to do the calculations. Variables I chose are: units, hours, QUARTER_VALUE = 0.25, DIME_VALUE = 0.1, NICKEL_VALUE = 0.05, PENNY_VALUE = 0.01, overQuota and underQuota.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/** * A basic system to track costs incurred when purchasing on-demand services from a cloud provider. * * The costs are tracked for a current billing period as well as over the lifetime of the contract. * * ACADEMIC NOTE: Only primitive types int and double are to be used. All calculations should be * performed at double precision, with only the final result rounded or truncated if applicable. * * ACADEMIC NOTE: You may NOT use any methods of the Math class, except as explicitly specified in * the comments below. * * ACADEMIC NOTE: You may NOT use any of the following: branches; loops; conditionals; arrays; or * collections. We haven't yet covered these topics quite yet. * * @author NF * @version * */ public class CloudCostCalculator { //TODO: Declare your private instance variables (fields) here /** * Create a new CloudCalculator object with the specified parameters. * * Usage is charged in blocks of unit-hours, meaning some number of units used over a defined * number of hours. Each block may incur two distinct costs per unit-hour. The number of units * in a block up to (and including) the quota are billed at the base rate per unit per hour. The * number of units exceeding the quota are billed at the overage rate per unit per hour. * * @param baseQuota maximum number of units that will be charged at the base rate. * @param baseRate cost (in cents per unit per hour) charged for any usage up to the quota. * @param overageRate cost (in cents per unit per hour) charged for any usage over the quota. */ public CloudCostCalculator(int baseQuota, int baseRate, int overageRate) { //TODO: Your implementation goes here }
/** * Record a block of usage of the cloud services. * * This method is called by an external system, each time a block of usage has been recorded. * All such usage must be tracked as part of the current billing period (and the total billing). * Partial hours and partial units are already rounded up, so the parameters are always positive * (meaning both non-zero and non-negative). * * ACADEMIC NOTE: You may safely assume that the parameters are always valid, according to the * expectations noted. Normally we would add validation code to ensure this is true, but we * haven't covered this quite yet. * * ACADEMIC NOTE: You may use Math.min() and/or Math.max() for your calculations. * * @param units number of units in this block; minimum value is 1. * @param hours number of hours in this block; minimum value is 1. */ public void recordUsage(int units, int hours) { //TODO: Your implementation goes here }
/** * Return the amount of usage (in number of unit-hours) so far witin the current billing period. * * The returned value includes all usage, whether it was above or within the quota. * * @return usage (in number of unit-hours) so far in the current billing period. */ public int getCurrentUsage() { //TODO: Your implementation goes here return 0; //TODO: Replace this placeholder }
/** * Return the charges (in dollars and cents) incurred so far within the current billing period. * * The returned value includes all charges, meaning usage as well as all one-time quota increase * fees. The value should be expressed in dollars and decimal cents. For example, if a charge is * 12 dollars and 34 cents, the return value must be exactly 12.34 (to double precision). * * @return charges incurred within the current billing period, in dollars and cents. */ public double getCurrentCharges() { //TODO: Your implementation goes here return 0; //TODO: Replace this placeholder }
/** * Return the effective rate (in cents per unit-hour) charged within the current billing period. * * This provides an indication of how much is being charged, on average, per unit-hour of usage. * The charges must include usage (over and within quota) plus all one-time quota increase fees. * * The rate will be calculated at dobule precision, but the return value will be rounded DOWN * (truncated) to the nearest multiple of 0.001 cents. For example, if the rate is calculated to * be 5.6789... cents, the return value should be exactly 5.678 (to double precision). * * ACADEMIC NOTE: Use only arithmetic and casting for the truncation. * * @return truncated effective rate (in cents per unit-hour) within the current billing period. */ public double getCurrentEffectiveRate() { //TODO: Your implementation goes here return 0; //TODO: Replace this placeholder }
/** * Increase the quota for the remainder of the current billing period. * * This method is called by an external system to indicate that an increase in quota has been * purchased for the remainder of the billing period, for a one-time charge. The increased quota * only affects new usage for the current period, and does not affect any already-billed usage. * The quota increase is always positive (greater than zero), and the one-time charge is always * non-negative (zero or greater). At the end of the current period, the quota is reset to the * base quota. * * ACADEMIC NOTE: You may safely assume that the parameters are always valid, according to the * expectations noted. Normally we would add validation code to ensure this is true, but we * haven't covered this quite yet. * * @param dollars number of dollars charged for the increased quota; minimum value is 0. * @param extraUnits number of units to add to the quota; minimum value is 1. */ public void increaseQuota(int dollars, int extraUnits) { //TODO: Your implementation goes here }
/** * Reset the current billing period and begin a new period with no charges. * * Clears all current billing statistics, but doesn't alter the total billing statistics at all. * */ public void resetBillingPeriod() { //TODO: Your implementation goes here }
/** * Return the charges (in dollars and cents) incurred within all billing periods. * * The returned value includes all charges, meaning usage as well as all one-time quota increase * fees. The value should be expressed in dollars and decimal cents. For example, if a charge is * 12 dollars and 34 cents, the return value must be exactly 12.34 (to double precision). * * @return charges incurred within the current and past billing periods, in dollars and cents. */ public double getTotalCharges() { //TODO: Your implementation goes here return 0; //TODO: Replace this placeholder }
/** * Return the effective rate (in cents per unit-hour) charged within all billing periods. * * This provides an indication of how much is being charged, on average, per unit-hour of usage. * The charges must include usage (over and within quota) plus all one-time quota increase fees. * * The rate will be calculated at double precision, but the return value will be rounded DOWN * (truncated) to the nearest multiple of 0.001 cents. For example, if the rate is calculated to * be 5.6789... cents, the return value should be exactly 5.678 (to double precision). * * ACADEMIC NOTE: Use only arithmetic and casting for the truncation. * * @return truncated effective rate (in cents per unit-hour) within all billing periods. */ public double getTotalEffectiveRate() { //TODO: Your implementation goes here return 0; //TODO: Replace this placeholder }
}
-------------------------------------Here is the tester code provided if this helps----------------------------------------------------------------
//CHECKSTYLE:OFF @SuppressWarnings("javadoc") public class CloudCostCalculatorTester { public static void main(String[] args) { CloudCostCalculator calc = new CloudCostCalculator(100, 1, 5);
System.out.println("--- Initial state ---"); System.out.println("current usage - expected: 0, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 0.0, actual: " + calc.getCurrentCharges()); System.out.println("total charges - expected: 0.0, actual: " + calc.getTotalCharges());
calc.recordUsage(50, 24);
System.out.println(); System.out.println("--- After u(50, 24) ---"); System.out.println("current usage - expected: 1200, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 12.0, actual: " + calc.getCurrentCharges()); System.out.println("current effective rate - expected: 1.0, actual: " + calc.getCurrentEffectiveRate()); System.out.println("total charges - expected: 12.0, actual: " + calc.getTotalCharges());
calc.recordUsage(50, 96);
System.out.println(); System.out.println("--- After u(50, 96) ---"); System.out.println("current usage - expected: 6000, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 60.0, actual: " + calc.getCurrentCharges()); System.out.println("current effective rate - expected: 1.0, actual: " + calc.getCurrentEffectiveRate()); System.out.println("total charges - expected: 60.0, actual: " + calc.getTotalCharges());
calc.recordUsage(120, 24);
System.out.println(); System.out.println("--- After u(120, 24) ---"); System.out.println("current usage - expected: 8880, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 108.0, actual: " + calc.getCurrentCharges()); System.out.println("current effective rate - expected: 1.216, actual: " + calc.getCurrentEffectiveRate()); System.out.println("total charges - expected: 108.0, actual: " + calc.getTotalCharges());
calc.recordUsage(20, 24);
System.out.println(); System.out.println("--- After u(20, 24) ---"); System.out.println("current usage - expected: 9360, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 112.8, actual: " + calc.getCurrentCharges()); System.out.println("current effective rate - expected: 1.205, actual: " + calc.getCurrentEffectiveRate()); System.out.println("total charges - expected: 112.8, actual: " + calc.getTotalCharges());
calc.increaseQuota(20, 100); calc.recordUsage(200, 72);
System.out.println(); System.out.println("--- After q(20, 100) then u(200, 72) ---"); System.out.println("current usage - expected: 23760, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 276.8, actual: " + calc.getCurrentCharges()); System.out.println("current effective rate - expected: 1.164, actual: " + calc.getCurrentEffectiveRate()); System.out.println("total charges - expected: 276.8, actual: " + calc.getTotalCharges()); System.out.println("total effective rate - expected: 1.164, actual: " + calc.getTotalEffectiveRate());
calc.resetBillingPeriod();
System.out.println(); System.out.println("--- After reset ---"); System.out.println("current usage - expected: 0, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 0.0, actual: " + calc.getCurrentCharges()); System.out.println("total charges - expected: 276.8, actual: " + calc.getTotalCharges()); System.out.println("total effective rate - expected: 1.164, actual: " + calc.getTotalEffectiveRate());
calc.recordUsage(150, 72);
System.out.println(); System.out.println("--- After u(150, 72) ---"); System.out.println("current usage - expected: 10800, actual: " + calc.getCurrentUsage()); System.out.println("current charges - expected: 252.0, actual: " + calc.getCurrentCharges()); System.out.println("current effective rate - expected: 2.333, actual: " + calc.getCurrentEffectiveRate()); System.out.println("total charges - expected: 528.8, actual: " + calc.getTotalCharges()); System.out.println("total effective rate - expected: 1.53, actual: " + calc.getTotalEffectiveRate());
System.out.println(); System.out.println("--- END OF PROVIDED TESTS ---");
//TODO: Place your additional test cases here
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
