TO DO in JAVA: Finish the HealthInsurance and LifeInsurance classes. Uncomment m
ID: 3913434 • Letter: T
Question
TO DO in JAVA: Finish the HealthInsurance and LifeInsurance classes. Uncomment my instances in main() of TrackInsurance. The red Xs should be gone.
package itp120mod6;
Health Insurance (below)
// "Extends" means inherit all the methods and fields from Insurance
public class HealthInsurance extends Insurance {
// create a new field called numDependents which is an integer
int numDependents;
// creat a full constructor for a new object. Make certain it sets the rate
public HealthInsurance (Customer cust, int dependents)
{
// you code the body
numDependents = 2;
}
// full constructor if read from a file
public HealthInsurance (Customer cust, int polNum, double yrRate, int numD)
{
super(cust,polNum,yrRate);
numDependents = numD;
}
// empty constuctor
public HealthInsurance ()
{
}
// required to make a complete class since we inherited from Insurance that had this method abstract
public void calcRate()
{
yearlyRate = 500 * numDependents;
}
// toString method. First call the one from the super class (Insurance) to write those fields
// and add on the new fields for this class
// we override the toString method from the Insurance class
public String toString ()
{
// you put the code here
}
// generate getters and setters
}
----------------------------------------------------------------------------------------------------------------
Life Insurance (below)
package itp120mod6;
// "extends" means inherits methods and fields from the Insurance class
public class LifeInsurance extends Insurance
{
// declare two new fields added - the amount of insurance they want (amount - an integer) and their age (age - an integer )
// full constructor. Let super (Insurance) set any fields that really came from there.
// Make certain to also set the rate
public LifeInsurance (Customer cust,int amtIns, int custAge)
{
// you code the body
}
// empty constructor
public LifeInsurance ()
{
}
// full constructor if reading from a file
public LifeInsurance (Customer cust, int polNum, double yrRate, int amtIns, int custAge)
{
// you code this
}
// required to write this if this class is to be a real class - fulfills the abstract requirements
public void calcRate()
{
if (age>40)
yearlyRate = amount*.005*2;
else
yearlyRate = amount*.005;
}
// to String. Let the Insurance class print out its fields and let this class print out new fields
// we override the toString method from the Insurance class
public String toString ()
{
// you code the body
}
// generate the getters and setters
}
Explanation / Answer
HealthInsurance.java
public class HealthInsurance extends Insurance {
// create a new field called numDependents which is an integer
int numDependents;
// creat a full constructor for a new object. Make certain it sets the rate
public HealthInsurance (Customer cust, int dependents)
{
// you code the body
super(cust);//calling suoer class constructor if no needed you can remove this
numDependents = 2;
}
// full constructor if read from a file
public HealthInsurance (Customer cust, int polNum, double yrRate, int numD)
{
super(cust,polNum,yrRate);
numDependents = numD;
}
// empty constuctor
public HealthInsurance ()
{
numDependents=0;//if not neede dyou can remove this
}
// required to make a complete class since we inherited from Insurance that had this method abstract
public void calcRate()
{
yearlyRate = 500 * numDependents;
}
// toString method. First call the one from the super class (Insurance) to write those fields
// and add on the new fields for this class
// we override the toString method from the Insurance class
public String toString ()
{
return super.toString()+" with no of dependents are "+numDependents;
// you put the code here
}
public int getNumDependents() {
return numDependents;
}
public void setNumDependents(int numDependents) {
this.numDependents = numDependents;
}
// generate getters and setters
}
LifeInsurance.java
public class LifeInsurance extends Insurance
{
// declare two new fields added - the amount of insurance they want (amount - an integer) and their age (age - an integer )
private int amtIns;
private int custAge;
// full constructor. Let super (Insurance) set any fields that really came from there.
// Make certain to also set the rate
public LifeInsurance (Customer cust,int amtIns, int custAge)
{
super(cust);
this.amtIns=amtIns;
this.custAge=custAge;
// you code the body
}
// empty constructor
public LifeInsurance ()
{
}
// full constructor if reading from a file
public LifeInsurance (Customer cust, int polNum, double yrRate, int amtIns, int custAge)
{
super(cust,polNum,yrRate);
this.amtIns=amtIns;
this.custAge=custAge;
// you code this
}
// required to write this if this class is to be a real class - fulfills the abstract requirements
public void calcRate()
{
if (custAge>40)
yearlyRate = amtIns*.005*2;
else
yearlyRate = amtIns*.005;
}
// to String. Let the Insurance class print out its fields and let this class print out new fields
// we override the toString method from the Insurance class
public String toString ()
{
return super.toString()+" with insurance amount of "+amtIns+" and age of customer is "+" custAge";
// you code the body
}
public int getAmtIns() {
return amtIns;
}
public void setAmtIns(int amtIns) {
this.amtIns = amtIns;
}
public int getCustAge() {
return custAge;
}
public void setCustAge(int custAge) {
this.custAge = custAge;
}
// generate the getters and setters
}
//all red xs are gone you can check with this code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.