Rate estimatordriver /** * Driver to demonstrate RateEstimator class. * * @author Tina Comston/ * @version Fall
Question:
Rate estimatordriver /** * Driver to demonstrate RateEstimator class. * * @author Tina Comston/
/** * Creates RateEstimator object, displays values. * */ public static void main(String[] args) { // Non smoker, Male, 25, Low Risk, 1 ticket, Good health RateEstimator rate = new RateEstimator(false, 'M', 25, false, 1, "Good"); System.out.println(rate);
rate.setSmoker(true); rate.setGender('F'); rate.setAge(55); rate.setHighRisk(true); rate.setNumTickets(3); rate.setHealth("Fair"); System.out.println(rate); } }
------------------------------
Rate estimator
/** * Class to generate a Term Life Insurance Quote. * * @author Doyt Perry/
// constants
/** * base rate for a 100K face value policy. */ public static final double BASE_RATE = 45.0;
/** * First constructor for objects of class RateEstimator. */ public RateEstimator() { // REPLACE these comments and return statement // with your coding of this constructor }
/** * Second constructor for objects of class RateEstimator. * * @param inSmoker whether or not candidate is a smoker. * @param inGender gender (M/F) of candidate. * @param inAge age of candidate. * @param inHighRisk whether or not candidate is high risk. * @param inNumTickets number of traffic tickets for candidate. * @param inHealth candidate health status (Good, Fair, Poor). */ public RateEstimator(boolean inSmoker, char inGender, int inAge, boolean inHighRisk, int inNumTickets, String inHealth) { // REPLACE these comments and return statement // with your coding of this constructor }
/** * Update whether or not the candidate is a smoker. * * @param inSmoker whether or not candidate is a smoker. */ public void setSmoker(boolean inSmoker) { this.smoker = inSmoker; }
/** * Return whether or not the candidate is a smoker. * * @return boolean True if smoker, false if not. */ public boolean isSmoker() { return this.smoker; }
// REPLACE these comments with the coding of the getter/setter // setGender // getGender // Be sure to add appropriate javaDoc comments public void setGender(char inGender) { ; }
public char getGender() { return 'a'; }
// REPLACE these comments with the coding of the getter/setter // setAge // getAge // Be sure to add appropriate javaDoc comments public void setAge(int inAge) { ; }
public int getAge() { return -1; }
// REPLACE these comments with the coding of the getter/setter // setHighRisk // isHighRisk // Be sure to add appropriate javaDoc comments public void setHighRisk(boolean inRisk) { ; }
public boolean isHighRisk() { return false; } // REPLACE these comments with the coding of the getter/setter // setNumTickets // getNumTickets // Be sure to add appropriate javaDoc comments public void setNumTickets(int inTickets) { ; }
public int getNumTickets() { return -1; } // REPLACE these comments with the coding of the getter/setter // setHealth // getHealth // Be sure to add appropriate javaDoc comments public void setHealth(String inHealth) { ; }
public String getHealth() { return null; }
/** * Calculate monthly rate. * * @return double Monthly rate for a 100K face value policy */ public double calcMonthlyRate() { // The monthly rate starts with at the BASE_RATE and // is adjusted for each demographic.
// Use methods of RateUtility to find needed adjustment for each // demographic, and then update the monthly rate accordingly // Return the monthly rate after all adjustments are made
// NOTE: Some demographic data can be invalid - if the utility // method returns INVALID, then stop processing and return // INVALID as the monthly rate
// Variable to accumulate monthly rate double monthlyRate = BASE_RATE;
// Variable whose value will be adjustment for specific demographic data double rateAdjustment = 0.0;
// Adjust rate based on age rateAdjustment = RateUtility.calcAgeAdj(this.age);
if (rateAdjustment == RateUtility.INVALID) { return RateUtility.INVALID; } else { monthlyRate = monthlyRate + rateAdjustment; }
// Adjust rate based on whether or not a smoker rateAdjustment = RateUtility.calcSmokerAdj(this.smoker); monthlyRate = monthlyRate + rateAdjustment;
// Adjust rate based on gender // REPLACE THIS comment with your code
// Adjust rate based wheter high risk // REPLACE THIS comment with your code
// Adjust rate based on number of tickets // REPLACE THIS comment with your code
// Adjust rate based on health // REPLACE THIS comment with your code
// Adjust rate based on multiple factors // (using the calcDemogAdj method of RateUtility) // REPLACE THIS comment with your code
return monthlyRate; }
/** * Display candidate information. * * @return String formatted candidate information. * *
* The resulting string has the format: * * Policy quote information: * Smoker: false * Gender: M * Age: 25 * # of tickets: 1 * Risky: false * Health: Good * Monthly premium: 50.0 **/ public String toString() { // initialize the variable that will hold the output string String quote = "Policy quote information: ";
quote += "Smoker: " + this.isSmoker() + " ";
// REPLACE these 2 comments with your code to complete // the output string per the specified format
return quote; }
}
-------------------------------
Rateestimatortest
import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * The test class RateEstimator. * * @author Doyt Perry/
/** * Default constructor for test class RateEstimatorTest. */ public RateEstimatorTest() { // not used for this lab }
/** * Sets up the test fixture. * * Called before every test case method. */ @Before public void setUp() { // not used for this lab }
/** * Tears down the test fixture. * * Called after every test case method. */ @After public void tearDown() { // not used for this lab }
/** * Test constructor #1. */ @Test public void testConstructor1() { RateEstimator rateTest = new RateEstimator(); assertNotNull(rateTest);
// check smoker attribute value assertFalse(rateTest.isSmoker());
// check gender attribute value assertEquals(' ', rateTest.getGender());
// check age attribute value assertEquals(0, rateTest.getAge());
// check high risk attribute value assertFalse(rateTest.isHighRisk());
// check number of tickets attribute value assertEquals(0, rateTest.getNumTickets());
// check health attribute value assertNull(rateTest.getHealth()); }
/** * Test constructor #2. */ @Test public void testConstructor2() { // REMOVE these comments and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test the setSmoker method. */ @Test public void testSetSmoker() { // REMOVE these comments and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test the setGender method. */ @Test public void testSetGender() { // REMOVE these comments and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test the setAge method. */ @Test public void testSetAge() { // REMOVE these comments and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test the setHighRisk method. */ @Test public void testSetHighRisk() { // REMOVE these comments and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test the setNumTickets method. */ @Test public void testSetNumTickets() { // REMOVE these comments and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test the setHealth method. */ @Test public void testSetHealth() { // REMOVE these comments and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test the calcMonthlyRate method. */ @Test public void testCalcMonthlyRate() { // Exhaustive testing of all permutations would require many // tests. This is a small subset of all permutations.
// Non smoker, Female, 24, Low Risk, 0 tickets, Good health RateEstimator rateTest1 = new RateEstimator(false, 'F', 24, false, 0, "Good"); assertEquals(19.0, rateTest1.calcMonthlyRate(), DELTA);
// REMOVE these 3 comment lines and then code // EACH test case specified below // (the last two test for INVALID demographic data)
// Smoker, Male, 79, High Risk, 3 tickets, Poor health
// Non smoker, Female, 34, High Risk, 3 tickets, Fair health
// Non smoker, Female, 56, Low Risk, 0 tickets, Fair health
// Smoker, Male, 24, High Risk, 0 tickets, Good health
// Non smoker, Female, 80, Low Risk, 0 tickets, Good health
// Non smoker, Female, 24, Low Risk, -1 tickets, "Average" health
}
/** * Test the toString method. */ @Test public void testToString() { // Smoker, Male, 25, Low Risk, 1 ticket, Good health RateEstimator rateTest = new RateEstimator(true, 'M', 25, false, 1, "Good"); String attributes = rateTest.toString();
assertTrue(attributes.contains("Smoker: true")); assertTrue(attributes.contains("Gender: M")); assertTrue(attributes.contains("Age: 25")); assertTrue(attributes.contains("Risky: false")); assertTrue(attributes.contains("# of tickets: 1")); assertTrue(attributes.contains("Health: Good")); assertTrue(attributes.contains("Monthly premium: 127")); }
}
---------------------------
Rateutility
/** * A utility class to help calculate demographic impact on rates. * * @author Doyt Perry/
// constants
/** * Indicates that policy cannot be quoted due * to value of one of the rate factors. */ public static final double INVALID = -999.0;
/** * Calculate age-related rate adjustment. * * @param inAge age of candidate. * @return double monthly premium adjustment * (return INVALID if bad input). */ public static double calcAgeAdj(int inAge) { // Age-related adjustment if (inAge < 0) { return INVALID; } else if (inAge < 25) { return 0.00; }
// REPLACE these comments and replace with coding // of SEQUENCE OF COMPARISONS for other ages
else { return INVALID; } }
/** * Calculate smoker-related rate adjustment. * * @param inSmoker whether or not candidate is a smoker. * @return double monthly premium adjustment. * */ public static double calcSmokerAdj(boolean inSmoker) { // REPLACE these comments and return statement // with your coding of this method return -1.0; }
/** * Calculate gender-related rate adjustment. * * @param inGender gender (M/F) of candidate. * @return double monthly premium adjustment * (return INVALID if bad input). * */ public static double calcGenderAdj(char inGender) { // REPLACE these comments and return statement // with your coding of this method return -1.0; }
/** * Calculate risk-related rate adjustment. * * @param inHighRisk whether or not candidate is high risk. * @return double monthly premium adjustment. * */ public static double calcRiskAdj(boolean inHighRisk) { // REPLACE these comments and return statement // with your coding of this method return -1.0; }
/** * Calculate tickets-related rate adjustment. * * @param inNumTickets number of traffic tickets for candidate. * @return double monthly premium adjustment * (return INVALID if bad input). * */ public static double calcTicketsAdj(int inNumTickets) { // REPLACE these comments and return statement // with your coding of this method return -1.0; }
/** * Calculate health-related rate adjustment. * * @param inHealth candidate health status (Good, Fair, Poor). * @return double monthly premium adjustment * (return INVALID if bad input). * */ public static double calcHealthAdj(String inHealth) { if (inHealth.equalsIgnoreCase("Good")) { return -6.00; } // REPLACE these comments and return statement // with your coding of a // of SEQUENCE OF COMPARISONS for other health status return -1.0; }
/** * Calculate demographics-related rate adjustment. * * @param inSmoker whether or not candidate is a smoker. * @param inAge age of candidate. * @param inHighRisk whether or not candidate is high risk. * @param inNumTickets number of traffic tickets for candidate. * @param inHealth candidate health status (Good, Fair, Poor). * @return double monthly premium adjustment. * */ public static double calcDemogAdj(int inAge, boolean inSmoker, boolean inHighRisk, int inNumTickets, String inHealth) { // Variable to count factors that contribute to premium decrease int asset = 0;
// Variable to count factors that contribute to premium increase int liability = 0;
// Variable to hold rate adjustment double rateAdjustment = 0;
// Determine if demographic data counts toward increase/decrease
if (inAge < 25) { ++asset; }
if (inSmoker) { ++liability; } else { ++asset; }
// REPLACE these comments and replace with coding // to test other demographic data contributing to // a premium increase/decrease
// were there enough factors to decrease premium? if (asset >= 3) { rateAdjustment = -15.00; }
// REPLACE these comments and replace with coding // to determine if an increase in premium is needed
return rateAdjustment; } } ----------------------------
Rate utility test
import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * The test class RateUtilityTest. * * @author Doyt Perry/
/** * Tolerance variable when comparing floating point values. */ public static final double DELTA = .0001;
/** * dummy variable to keep WebCat from complaining about RateUtility class */ private RateUtility rt = new RateUtility(); /** * Default constructor for test class RateUtilityTest. */ public RateUtilityTest() { // not used for this lab }
/** * Sets up the test fixture. * * Called before every test case method. */ @Before public void setUp() { // the following line is needed to keep WebCat from // complaining about RateUtility class rt.toString(); }
/** * Tears down the test fixture. * * Called after every test case method. */ @After public void tearDown() { // not used for this lab }
/** * Test calcAgeAdj method. */ @Test public void testCalcAgeAdj() { assertEquals(0.0, RateUtility.calcAgeAdj(24), DELTA);
assertEquals(18.0, RateUtility.calcAgeAdj(25), DELTA); assertEquals(23.0, RateUtility.calcAgeAdj(35), DELTA); assertEquals(36.0, RateUtility.calcAgeAdj(45), DELTA); assertEquals(51.0, RateUtility.calcAgeAdj(55), DELTA); assertEquals(100.0, RateUtility.calcAgeAdj(65), DELTA); assertEquals(RateUtility.INVALID, RateUtility.calcAgeAdj(80), DELTA); assertEquals(RateUtility.INVALID, RateUtility.calcAgeAdj(-1), DELTA); }
/** * Test calcSmokerAdj method. */ @Test public void testCalcSmokerAdj() { // REMOVE this comment and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test calcGenderAdj method. */ @Test public void testCalcGenderAdj() { // REMOVE this comment and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test calcRiskAdj method. */ @Test public void testCalcRiskAdj() { // REMOVE this comment and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test calcTicketsAdj method. */ @Test public void testCalcTicketsAdj() { // REMOVE this comment and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test calcHealthAdj method. */ @Test public void testCalcHealthAdj() { // REMOVE this comment and assert statement // and complete the coding of this test method assertTrue(true); }
/** * Test calcDemogAdj method. */ @Test public void testcalcDemogAdj() { // 24, Non smoker, Low Risk, 0 tickets, Good health assertEquals(-15.0, RateUtility.calcDemogAdj(24, false, false, 0, "Good"), DELTA);
// REMOVE these 2 comment lines and then code // EACH test case specified below
// 79, Smoker, High Risk, 3 tickets, Poor health
// 34, Non smoker, High Risk, 3 tickets, Fair health
// 56, Non smoker, Low Risk, 0 tickets, Fair health
// 24, Smoker, High Risk, 0 tickets, Poor health
// 80, Non smoker, Low Risk, -1 tickets, Fair health
// 24, Non smoker, Low Risk, 0 tickets, Average health
// add a few test cases that will return INVALID to // make sure code logic can handle exceptions
}
}
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates