The purpose of this lab is to practice using inheritance and
polymorphism. We need a set of classes for an
Insurance company. Insurance companies offer many
different types of policies: car, homeowners, flood, earthquake,
health, etc. Insurance policies have a lot of
data and behavior in common. But they also have
differences. You want to build each insurance policy
class so that you can reuse as much code as possible.
Avoid duplicating code. You’ll save time, have less
code to write, and you’ll reduce the likelihood of bugs.
Task I: Write a class Customer
The Customer class will describe the properties and behavior
representing one customer of the insurance
company.
1. The data members should include a name, and the state the
customer lives in
2. Override the equals() function. You may consider
two customers to be equal if they have the same name and live in
the same state (See the Object class API if you need to know what
the function prototype looks like)
3. Add any additional methods you need to complete the rest of
the lab
Task I:
CODE
class Customer {
private String name, state;
public Customer(String name, String state) {
super();
this.name = name;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public boolean equals(Customer other) {
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
return true;
}
}
Task II: Write a class InsurancePolicy
The InsurancePolicy class will describe the properties and
behavior that all insurance policies have in
common.
4. The data members should include a customer name, a policy
number (you can use an integer), and a policy expiration date (use
LocalDate)
5. Write a constructor that takes two parameters -- the Customer
name and the policy number . Your class should
not have a default constructor.
6. Write a set function that a user will use to modify the
policy expiration date. 7. Write get functions for your data
members
8. Write a function isExpired() that returns true if the policy
has expired, false otherwise. You can determine this by
comparing the expiration date against the current date.
9. Override the toString() function (See the Object class API if
you need to know what the function prototype looks like)
10. Override the equals() function. You may consider
two policies to be equal if they have the same policy number (See
the Object class API if you need to know what the function
prototype looks like)
Task II.
CODE
class InsurancePolicy {
private String name;
private int policyNumber;
private LocalDate expirationDate;
public InsurancePolicy(String name, int policyNumber) {
super();
this.name = name;
this.policyNumber = policyNumber;
}
public void setExpirationDate(LocalDate expirationDate) {
this.expirationDate = expirationDate;
}
public String getName() {
return name;
}
public int getPolicyNumber() {
return policyNumber;
}
public LocalDate getExpirationDate() {
return expirationDate;
}
public boolean isExpired() {
LocalDate today = LocalDate.now();
return expirationDate.compareTo(today) < 0;
}
@Override
public String toString() {
return "InsurancePolicy [name=" + name + ", policyNumber=" +
policyNumber + ", expirationDate=" + expirationDate
+ "]";
}
public boolean equals(InsurancePolicy other ) {
if (expirationDate == null) {
if (other.expirationDate != null)
return false;
} else if (!expirationDate.equals(other.expirationDate))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (policyNumber != other.policyNumber)
return false;
return true;
}
}
Task III: Write a class CarInsurancePolicy
The CarInsurancePolicy class will describe an insurance policy
for a car.
1. The data members should include all the data members of an
Insurance Policy, but also the driver’s license number of the
customer, whether or not the driver is considered a “good” driver
(as defined by state law) , and the car being insured (this should
be a reference to a Car object -- write a separate Car class for
this that includes the car’s make (e.g., “Honda” or “Ford”), model
(e.g. “Accord” or “Fusion”), and estimated value.
2. Write a constructor that takes two parameters -- the customer
and the policy number .
3. Write the necessary get/set functions for your class.
4. Write a function calculateCost() that will return the cost of
the car insurance policy . The cost of the policy
should be computed as follows: (a) 5% of the estimated
car value if the driver is rated as a “good” driver, 8% of the
estimated car value if the driver is not rated as a “good”
driver. (b) The cost should then be
adjusted based on where the customer lives. If the customer lives
in one of the high cost states of California (“CA”) or New York
(“NY”), add a 10% premium to the policy cost.
public class Car {
private String make;
private String model;
private double estimatedValue;
public Car(String make, String model, double
estimatedValue) {
this.make = make;
this.model = model;
this.estimatedValue =
estimatedValue;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getEstimatedValue() {
return estimatedValue;
}
public void setEstimatedValue(double
estimatedValue) {
this.estimatedValue =
estimatedValue;
}
@Override
public String toString() {
return "Make:" + make + "\n Model:"
+ model + "\nEstimated Value:"
+ estimatedValue;
}
}
_______________________________
// CarInsurancePolicy.java
public class CarInsurancePolicy {
private String licenceNumber;
private boolean isGood;
private Car car;
public CarInsurancePolicy(String licenceNumber,
boolean isGood, Car car) {
this.licenceNumber =
licenceNumber;
this.isGood = isGood;
this.car = car;
}
public String getLicenceNumber() {
return licenceNumber;
}
public void setLicenceNumber(String licenceNumber)
{
this.licenceNumber =
licenceNumber;
}
public boolean isGood() {
return isGood;
}
public void setGood(boolean isGood) {
this.isGood = isGood;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public double calculateCost() {
double cost = 0;
if (isGood) {
return 0.05 *
car.getEstimatedValue();
} else {
return 0.08 *
car.getEstimatedValue();
}
}
@Override
public String toString() {
return "Licence Number:" +
licenceNumber
+ "\nIs Good=" + isGood + "\ncar:" + car;
}
}
________________________________
// Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String state;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
// Getting the input entered by
the user
System.out.print("Enter Driver
Licence Number :");
String licenceNum =
sc.next();
System.out.print("Is Driver is Good
? ");
boolean isGood =
sc.nextBoolean();
System.out.print("Enter Car make
:");
String make = sc.next();
System.out.print("Enter Car model
:");
String model = sc.next();
System.out.print("Enter Car value
:");
double estimatedVal =
sc.nextDouble();
System.out.print("Enter State of
Customer lives in :");
state = sc.next();
Car c = new Car(make, model,
estimatedVal);
CarInsurancePolicy cip = new
CarInsurancePolicy(licenceNum, isGood, c);
double costOfPolicy =
cip.calculateCost();
if (state.equalsIgnoreCase("CA") ||
state.equalsIgnoreCase("NY")) {
costOfPolicy +=
0.10 * cip.calculateCost() + cip.calculateCost();
}
System.out.println(cip);
System.out.println("Cost of
Insurance Policy :$" + costOfPolicy);
}
}
Task IV: Write tests for the Customer class and the CarInsurance
classes
These tests should be put in a separate
package.
1. Make sure that you include tests for the equals() method of
Customer and the calculateCost() method of CarInsurance.
Task V: Write a class HomeInsurancePolicy
The HomeInsurancePolicy class will describe an insurance policy
for a home.
2. The data members should include all the data members of an
Insurance Policy, but also the estimated cost to rebuild the home,
and the estimated property value
3. Write a constructor that takes two parameters -- the customer
and the policy number .
4. Write the necessary get/set functions for your class.
5. Write a function calculateCost() that will return the cost of
the home insurance policy . The cost of the policy
should be 0.5% of the sum of rebuild cost and the property
value. Add a 5% premium to the policy cost if the
customer lives in a “high cost” state of California or New
York.
Task VI: Write a class FloodInsurancePolicy
Flood insurance is a special type of homeowners
insurance. Damage caused by floods is typically not
covered by a home insurance policy. To be covered in
case of a flood, homeowners need to buy a flood insurance policy.
1. The data members should include all the data members of
HomeInsurancePolicy (because this is a special type of homeowner’s
insurance), but also an integer value between 1 and 99 representing
the flood risk (a value of 1 will represent that the probability of
a flood in the home’s area is very low, a value of 99 will
represent that the home is in an area that frequently
floods.
2. Write a constructor that takes two parameters -- the customer
and the policy number .
3. Write the necessary get/set functions for your class.
4. Write a function calculateCost() that will return the cost of
the flood insurance policy . The cost of the policy
should be the flood risk number divided by 100 and then multiplied
by the home’s rebuild cost. Add a 7% premium to the
policy cost if the customer lives in a “high cost” state of
California or New York.
In: Computer Science