Question

In: Computer Science

The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.


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.

Solutions

Expert Solution

Dear Student,

Kindly create two package.

1. main: put all java file other than test files

2. test : put CustomerTest.java and CarInsuranceTest.java

package main;

public class HomeInsurancePolicy extends InsurancePolicy {

       private double rebuildCost;

       private double propertlyValue;

       private Customer customer;

       /**

       * Constructor

       *

       * @param name

       * @param policyNumber

       */

       public HomeInsurancePolicy(String name, int policyNumber) {

              super(name, policyNumber);

       }

       /**

       * Constructor

       *

       * @param name

       * @param policyNumber

       */

       public HomeInsurancePolicy(Customer customer, int policyNumber) {

              super(customer.getName(), policyNumber);

              this.setCustomer(customer);

       }

       /**

       * @return the rebuildCost

       */

       public double getRebuildCost() {

              return rebuildCost;

       }

       /**

       * @param rebuildCost

       *            the rebuildCost to set

       */

       public void setRebuildCost(double rebuildCost) {

              this.rebuildCost = rebuildCost;

       }

       /**

       * @return the propertlyValue

       */

       public double getPropertlyValue() {

              return propertlyValue;

       }

       /**

       * @param propertlyValue

       *            the propertlyValue to set

       */

       public void setPropertlyValue(double propertlyValue) {

              this.propertlyValue = propertlyValue;

       }

       //calculat cost

       public double calculateCost() {

              // The cost of the policy should be 0.5% of the sum

              // of rebuild cost and the property value.

              double cost = (.5 * (rebuildCost + propertlyValue) / 100.0);

              if (customer.getState().equalsIgnoreCase("CA") || customer.getState().equalsIgnoreCase("NYC")) {

                      // 5% premium cost

                      cost += (5 * cost / 100.0);

              }

              return cost;

       }

       /**

       * @return the customer

       */

       public Customer getCustomer() {

              return customer;

       }

       /**

       * @param customer

       *            the customer to set

       */

       public void setCustomer(Customer customer) {

              this.customer = customer;

       }

       @Override

       public String toString() {

              return "HomeInsurancePolicy [rebuildCost=" + rebuildCost + ", propertlyValue=" + propertlyValue + ", customer="

                             + customer + "]";

       }

      

      

}

package main;

public class FloodInsurancePolicy extends HomeInsurancePolicy {

       private int floodRisk;

       /**

       * Constructor

       *

       * @param customer

       * @param policyNumber

       */

       public FloodInsurancePolicy(Customer customer, int policyNumber) {

              super(customer, policyNumber);

       }

       /**

       * @return the floodRisk

       */

       public int getFloodRisk() {

              return floodRisk;

       }

       /**

       * @param floodRisk

       *            the floodRisk to set

       */

       public void setFloodRisk(int floodRisk) {

              this.floodRisk = floodRisk;

       }

       // calculat cost

       public double calculateCost() {

              // The cost of the policy should be 0.5% of the sum

              // of rebuild cost and the property value.

              double cost = (floodRisk * (getRebuildCost() / 100.0));

              if (getCustomer().getState().equalsIgnoreCase("CA") || getCustomer().getState().equalsIgnoreCase("NYC")) {

                      // 5% premium cost

                      cost += (7 * cost / 100.0);

              }

              return cost;

       }

       @Override

       public String toString() {

              return "FloodInsurancePolicy [floodRisk=" + floodRisk + "]";

       }

}

package test;

import main.Car;

import main.CarInsurancePolicy;

public class CarInsuranceTest {

                  private static Car c = new Car("AUDI", "Q6", 23000000);

                  private static CarInsurancePolicy cip = new CarInsurancePolicy("121", false, c);

                  /**

                  * Main test stub

                  *

                  * @param args

                  */

                  public static void main(String[] args) {

                                    System.out.print("TEST#1: ");

                                    test01_cost_HIGH_COST_STATE();

                                    System.out.print("TEST#2: ");

                                    test01_cost_NON_HIGH_COST_STATE();

                  }

                  // test price value

                  private static void test01_cost_HIGH_COST_STATE() {

                                    String state = "CA";

                                    double costOfPolicy = cip.calculateCost();

                                    if (state.equalsIgnoreCase("CA") || state.equalsIgnoreCase("NY")) {

                                                      costOfPolicy += 0.10 * cip.calculateCost();

                                    }

                                    if (costOfPolicy == 2024000.0) {

                                                      System.out.println("PASS");

                                    } else {

                                                      System.out.println("FAIL");

                                    }

                  }

                  // test price value

                  private static void test01_cost_NON_HIGH_COST_STATE() {

                                    String state = "OTHER";

                                    double costOfPolicy = cip.calculateCost();

                                    if (state.equalsIgnoreCase("CA") || state.equalsIgnoreCase("NY")) {

                                                      costOfPolicy += 0.10 * cip.calculateCost() + cip.calculateCost();

                                    }

                                    if (costOfPolicy == 1840000.0) {

                                                      System.out.println("PASS");

                                    } else {

                                                      System.out.println("FAIL");

                                    }

                  }

}

package test;

import main.Customer;

public class CustomerTest {

       private static Customer c = new Customer("Nick", "NY");

       /**

       * Main Test Stub

       * @param args

       */

       public static void main(String[] args) {

              System.out.print("TEST#1:(Name) ");

              test01_cusName();

              System.out.print("TEST#2:(State) ");

              test02_cusState();

       }

       // Customer name

       private static void test01_cusName() {

              if (c.getName().equals("Nick")) {

                      System.out.println("PASS");

              } else {

                      System.out.println("FAIL");

              }

       }

       // Customer state

       private static void test02_cusState() {

              if (c.getState().equals("NY")) {

                      System.out.println("PASS");

              } else {

                      System.out.println("FAIL");

              }

       }

}


Related Solutions

Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way. For example, have a look at the code we’ve included for this problem. We’ve defined Shape as an abstract base class. It doesn’t provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
I need to see a working example using inheritance and the classes (below) The person class...
I need to see a working example using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student as a String this methods has...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
In this lab, we will build a couple of classes, where one will be composed into...
In this lab, we will build a couple of classes, where one will be composed into the other. Work in pairs if you wish. Problem We saw Point.java in class last week. Do the following, one step at a time, making sure it works before you go on to the next step. Be sure to get help if you have any difficulties. You may use Point.java from last week or build a new one of your own. If you are...
Language: C++ 3 Campus Travel Game This lab will practice using loops. We will construct a...
Language: C++ 3 Campus Travel Game This lab will practice using loops. We will construct a short computer game. Please look at the Test Cases for exact wording. For your game, the player’s goal is to reach campus exactly. The player starts 14 miles away and has up to 4 turns to reach campus. At each turn the play can ride either use a Bus, a Subway, or a Jetpack: Riding a Bus moves the player forward 2 miles each...
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT