Question: public class Policy { private static int NEXT_POLICY_NUMBER = 1; private int policyNumber; protected float amount; public Policy(float amt) { amount = amt; policyNumber =
public class Policy { private static int NEXT_POLICY_NUMBER = 1; private int policyNumber; protected float amount; public Policy(float amt) { amount = amt; policyNumber = NEXT_POLICY_NUMBER++; } public int getPolicyNumber() { return policyNumber; } public float getAmount() { return amount; } public String toString() { return String.format("Policy: %04d amount: $%1.2f", policyNumber, amount); } public boolean isExpired() { return false; } } Now define a DepreciatingPolicy class as a subclass of Policy which has the following: a private instance variable called rate of type float which represents the rate at which the policy depreciates each time a claim is made. For example, a rate of 0.10 means that the value of the policy (i.e., the amount) depreciates 10% each time a claim is made (we will discuss the making of claims in the next section). a public constructor which takes a float representing the amount and another float representing the rate. This constructor MUST make full/proper use of inheritance. a public get method for the rate. a toString() method that returns a String with the following example format: DepreciatingPolicy: 0001 amount: $320.00 rate: 10.0% You MUST make use of inheritance by calling the toString() from the superclass. Also, the rate must be properly formatted. an instance method called isExpired() which returns true if the amount of this policy has depreciated to 0 (assume a value less than 0.01 is zero). You MUST write this method without any IF/SWITCH/TERNARY conditional statements. an instance method called depreciate() which reduces the amount of the policy by the rate percentage. For example, if the amount is $100 and the rate is 0.10, then the amount after this method is called should be $90.
Define an ExpiringPolicy class as a subclass of Policy which has the following: a private instance variable called expiryDate of type Date (from java.util.Date package) that contains the date after which the policy is invalid. a public constructor which takes a float representing the amount and a Date representing the expiryDate. This constructor MUST make full/proper use of inheritance. a public constructor which takes a float representing the amount. This constructor MUST make full/proper use of inheritance. The expiryDate should then be set to exactly one year after the policy was created. Here is how you can do this: GregorianCalendar aCalendar = new GregorianCalendar(); aCalendar.add(Calendar.YEAR,1); expiryDate = aCalendar.getTime(); a public get method for the expiryDate. a toString() method that makes use of inheritance by calling the toString() from the superclass and returns a String with the following example format which includes the parentheses (i.e., look at the SimpleDateFormat class described in section 13.3 of the course notes in order to see how to format the date properly). Notice the difference (underlined) between expired and no-expired policies: ExpiringPolicy: 0001 amount: $320.00 expired on: April 30, 2001 (12:08PM) ExpiringPolicy: 0006 amount: $500.00 expires: May 23, 2023 (4:46AM) An instance method called isExpired() which returns true if the computer's current date is ON or AFTER the expiryDate and false otherwise. (Hint: the Date class has methods before(Date d) and after(Date d) ... see section 13.3 of the course notes).
Test your new classes with the following program:
import java.util.*; public class PolicyTester { public static void main(String args[]) { System.out.println(new Policy(320)); DepreciatingPolicy p1 = new DepreciatingPolicy(500.1f, 0.1f); System.out.println(p1); p1.depreciate(); System.out.println(p1); System.out.println(new ExpiringPolicy(1000)); Date expDate = new GregorianCalendar(2021, 0, 2, 23, 59).getTime(); ExpiringPolicy p2 = new ExpiringPolicy(2000, expDate); System.out.println(p2); System.out.println(p2.isExpired()); expDate = new GregorianCalendar(2013, 3, 1, 23, 59).getTime(); ExpiringPolicy p3 = new ExpiringPolicy(2000, expDate); System.out.println(p3); System.out.println(p3.isExpired()); } } Here is the expected output (your date on line 4 will differ):
Policy: 0001 amount: $320.00
DepreciatingPolicy: 0002 amount: $500.10 rate: 10.0%
DepreciatingPolicy: 0002 amount: $450.09 rate: 10.0%
ExpiringPolicy: 0003 amount: $1000.00 expires: May 16, 2018 (01:18PM)
ExpiringPolicy: 0004 amount: $2000.00 expires: January 02, 2021 (11:59PM) false
ExpiringPolicy: 0005 amount: $2000.00 expired on: April 01, 2013 (11:59PM) true
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
