(求助) 2 classes
(1) The Policy ClassDefine a public class called Policy which has a private static/class variable
called NEXT_POLICY_NUMBER which is an int (initially set to 1) representing
the number to be given to the next policy created. Create the following attributes
in the class as well:
• a private attribute called policyNumber of type int that identifies the policy with a unique integer.
• a protected attribute called amount that contains the amount (a float) of coverage for the policy.
Create a public constructor which takes a single float parameter and uses it to set the amount variable. The constructor sets the policyNumber such that each created policy has a unique number and also updates the class variable appropriately.
Create public get methods for your attributes as well as a public toString() method that returns a String with the following format (use String.format() for both the policy number and amount):
Policy: 0001 amount: $320.00
Create a public instance method called isExpired() which always returns false. Now test your code with this program:
程序代码:
public class PolicyTestProgram { public static void main(String args[]) { System.out.println(new Policy(320)); // displays Policy: 0001 amount: $320.00 System.out.println(new Policy(500.1f)); // displays Policy: 0002 amount: $500.10 System.out.println(new Policy(0)); // displays Policy: 0003 amount: $0.00 System.out.println(new Policy(320).isExpired()); // displays false } }
(2) The DepreciablePolicy Class
Now we will make some subclasses to represent different types of policies. Define a
public class called DepreciablePolicy as a subclass of Policy which has a private
attribute 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). Implement these:
• a public constructor which takes a float representing the amount and another float representing
the rate. This constructor should use inheritance by calling the constructor from the superclass to
set the amount and policyNumber.
• a public get method for the rate.
• a public toString() method that returns a String with the following format:
DepreciablePolicy: 0001 amount: $320.00 rate: 10.0%
You MUST make use of inheritance by calling the toString() from the superclass (You will have to
make a change to the Policy class’ toString() method as well. Also, use String.format() again to
display the rate.
• an instance method called isExpired() which returns true if the amount of this policy has
depreciated to 0. You should be able to write this method WITHOUT any IF statements … try.
• 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 once should be $90.