computer science
COMP1406/1006 Tutorial 0 Assignment #3 Due Friday, March 5th at 11:59 pm COMP1406 – Winter 2021 Submit a single file called assignment3-ID.zip, where ID is your student ID number, to cuLearn. There are 50 possible marks. 1 Policy Classes [14 marks] You will be creating this class hierarchy: To begin, read and understand the provided Policy class: 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). COMP1406 - Winter 2021 1 https://culearn.carleton.ca/moodle/ Assignment #3 Due Friday, March 5th at 11:59 pm • a public constructor which takes a float representing the amount and another float represent- ing the rate. • a public get method for the rate. (getRate()) • 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. You MUST write this method without any IF 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. • a public constructor which takes a float representing the amount. 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. (getExpiryDate() • a toString() method that makes use of inheritance by calling the toString() from the super- class and returns a String with the following example format which includes the brackets (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). ExpiringPolicy: 0001 amount: $320.00 expired on: April 30, 2001 (12:08PM) ExpiringPolicy: 0006 amount: $500.00 expires: May 23, 2023 (4:46AM) Notice the difference between expired and not-expired policies: • 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). Again, see section 13.3 of the course notes. COMP1406 - Winter 2021 2 Assignment #3 Due Friday, March 5th at 11:59 pm You can test your new classes with the following provided program: import java.util.Date; import java.util.GregorianCalendar; 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 2 The Clients [21 marks] Now, you will define the following hierarchy: To begin, read and understand the provided abstract Client class: import java.util.Date; public abstract class Client { private static final int MAX_POLICIES_PER_CLIENT = 10; private static int NEXT_CLIENT_ID = 1; COMP1406 - Winter 2021 3 Assignment #3 Due Friday, March 5th at 11:59 pm private String name; private int id; protected Policy[] policies; protected int numPolicies; public Client(String n) { name = n; id = NEXT_CLIENT_ID++; policies = new Policy[MAX_POLICIES_PER_CLIENT]; numPolicies = 0; } public String getName() { return name; } public int getId() { return id; } public Policy[] getPolicies() { return policies; } public int getNumPolicies() { return numPolicies; } public String toString() { return String.format("Client: %06d amount: %s", id, name); } } Create the following in the Client class: • a public method called totalCoverage() which returns a float containing the total amount of coverage of all the client’s policies. • a public method called addPolicy(Policy p) which adds the given Policy to the array of policies provided that the limit has not been reached and returns the Policy object, or null if not added. • a public method called openPolicyFor(float amt) which creates a new Policy for the amount specified in the parameter and adds it to the client using (and returning the result from) the addPolicy() method • a public method called openPolicyFor(float amt, float rate) which creates a new DepreciatingPolicy for the amount and rate specified in the parameters and adds it to the client using (and returning the result from) the addPolicy() method. • a public method called openPolicyFor(float amt, Date expire) which creates a new ExpiringPolicy for the amount and expiry date specified in the parameters and adds it to the client using (and returning the result from) the addPolicy() method above. • a public method called getPolicy(int polNum) which examines all Policy objects in policies. If one is found whose policyNumber matches the input parameter then it is returned by the method. Otherwise null is returned. • a public method called cancelPolicy(int polNum) which removes the police from the client’s list with the given policy number, if found. It should return true if the policy was found, otherwise it should return false. When a policy is removed from the array, the empty location in the array should be replaced by the last policy in the array. • a public abstract method called makeClaim(int polNum) that returns a float. COMP1406 - Winter 2021 4 Assignment #3 Due Friday, March 5th at 11:59 pm Now create the two subclasses of Client: one called IndividualClient and the other called CompanyClient. Try compiling them. Notice that your test code will not compile. That’s because subclasses DO NOT inherit constructors and java requires a constructor to be available. Do the following: • Create a constructor (taking a single String argument) for each of these subclasses that calls the one in Client. • Go back and alter your toString() method in Client class so that the proper client type is displayed. For example: CompanyClient 0001: Bob B. Pins To do this, you will want to figure out how to get the name of the class as a String. • Implement the makeClaim(int polNum) method in the IndividualClient class. If the policy with that number is not found or has expired, then simply return 0. IndividualClients are allowed to make only 1 claim for a regular Policy but DepreciatingPolicy and ExpiringPolicy objects can have multiple claims. So, you should cancel any regular policies after a claim is made. If the claim is being made for a DepreciatingPolicy, then you must make sure to depreciate the policy. This method should return the amount of the policy (amount after depreciating in the case of DepreciatingPolicy objects) if the claim made was ok, otherwise 0 is returned. • In the Policy class, implement a method called handleClaim() that returns the amount of the policy. • In the DepreciatingPolicy class, implement handleClaim() so that it returns the amount of the policy but also depreciates the policy. Note that the amount returned is the amount of the policy before it was depreciated. • In the ExpiringPolicy class, implement handleClaim() so that it returns the amount of the policy as long as the policy has not yet expired, otherwise it returns 0. • In the CompanyClient class, implement the makeClaim(int polNum) method so that it first checks to make sure the policy is one belonging to this client and then uses the double- dispatching technique by calling the handleClaim() method. Double-dispatching is discussed in Section 4.5 of the textbook. Note that you MUST NOT use any IF statements to determine the policy type as that would undo the advantages of double-dispatching. Note that CompanyClient objects DO NOT have their policy removed afterwards, and so you should make use of the getPolicy() method. If no policy is found with the given polNum, return 0; COMP1406 - Winter 2021 5 Assignment #3 Due Friday, March 5th at 11:59 pm 3 Testing [no marks] Now test what you have done with the provided Clienttester program. Here is the expected output: Here are the Individual Client’s policies: Policy: 0001 amount: $100.00 DepreciatingPolicy: 0002 amount: $200.00 rate: 10.0% ExpiringPolicy: 0003 amount: $300.00 expires: January 02, 2020 (11:59PM) ExpiringPolicy: 0004 amount: $400.00 expired on: June 04, 2009 (12:00PM) Making claims: Claim for policy 0001: $100.00 Claim for policy 0002: $180.00 Claim for policy 0003: $300.00 Claim for policy 0004: $ 0.00 Claim for policy 0005: $ 0.00 Here are the Individual Client’s policies after claims: ExpiringPolicy: 0004 amount: $400.00 expired on: June 04, 2009 (12:00PM) DepreciatingPolicy: 0002 amount: $180.00 rate: 10.0% ExpiringPolicy: 0003 amount: $300.00 expires: January 02, 2020 (11:59PM) The total policy coverage for this client: $880.00 Here are the Company Client’s policies: Policy: 0006 amount: $1000.00 DepreciatingPolicy: 0007 amount: $2000.00 rate: 10.0% ExpiringPolicy: 0008 amount: $3000.00 expires: January 02, 2020 (11:59PM) ExpiringPolicy: 0009 amount: $4000.00 expired on: June 04, 2009 (12:00PM) Making claims: Claim for policy 0006: $1000.00 Claim for policy 0007: $2000.00 Claim for policy 0008: $3000.00 Claim for policy 0009: $ 0.00 Claim for policy 0005: $ 0.00 Here are the Company Client’s policies after claims: Policy: 0006 amount: $1000.00 DepreciatingPolicy: 0007 amount: $1800.00 rate: 10.0% ExpiringPolicy: 0008 amount: $3000.00 expires: January 02, 2020 (11:59PM) ExpiringPolicy: 0009 amount: $4000.00 expired on: June 04, 2009 (12:00PM) The total policy coverage for this client: $9800.00 Cancelling policy #12 ..