Now define a DepreciatingPolicy class as a subclass of Policy which has the foll
ID: 3722829 • Letter: N
Question
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:
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
Explanation / Answer
Policy.java
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 void setAmount(float amount) {
this.amount = amount;
}
public String toString() {
return String.format("Policy: %04d amount: $%1.2f", policyNumber, amount);
}
public boolean isExpired() {
return false;
}
}
DepreciatingPolicy .java
public class DepreciatingPolicy extends Policy {
private float rate; //variable
//constructor
public DepreciatingPolicy(float amt, float rate) {
super(amt);
this.rate = rate;
}
public float getRate() {
return rate;
}
//instance method
public boolean isExpired()
{
return Math.signum(rate) == 0;
}
public void depreciate()
{
setAmount(getAmount()-getAmount()*rate);
}
@Override
public String toString() {
return "Depreciating"+super.toString()+ " Rate : " + getRate()*100+" % ";
}
}
ExpiringPolicy.java
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class ExpiringPolicy extends Policy {
private Date expiryDate;
public ExpiringPolicy(float amt) {
super(amt);
GregorianCalendar aCalendar = new GregorianCalendar();
aCalendar.add(Calendar.YEAR, 1);
this.expiryDate = aCalendar.getTime();
}
public ExpiringPolicy(float amt, Date expiryDate) {
super(amt);
this.expiryDate = expiryDate;
}
public Date getExpiryDate() {
return expiryDate;
}
public boolean isExpired()
{
Date now=new Date();
return expiryDate.before(now);
}
@Override
public String toString() {
boolean isExpired=isExpired();
String s=isExpired?" expired":" expires";
return "Expiring"+super.toString()+s+" on "+expiryDate;
}
}
output:
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 on Mon Mar 04 11:21:47 IST 2019
ExpiringPolicy: 0004 amount: $2000.00 expires on Sat Jan 02 23:59:00 IST 2021
false
ExpiringPolicy: 0005 amount: $2000.00 expired on Mon Apr 01 23:59:00 IST 2013
true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.