TO DO in JAVA: Add a class called ArtInsurance that inherits from Insurance. Loo
ID: 3913436 • Letter: T
Question
TO DO in JAVA: Add a class called ArtInsurance that inherits from Insurance. Look at the other three classes as a pattern. ArtInsurance should have new fields of String description (describes the art work) and double value (the value of the art work that you are insuring). Make certain your toString method prints these out. You can use whatever calcRate() calculation that you like for ArtInsurance.
Insurance (below)
package itp120mod6;
import java.text.*;
// note that this is an abstract class (see p455 of your text)
// I do not want any one to create a generic Insurance instance
// (we only sell Auto, Life, and Health) but so as not to repeat
// common fields in those , we move them up one hierarchy level
// and make a generic Inurance class
// implements the interface Comparable so I can define how to order
// or sort Insurance objects
public abstract class Insurance implements Comparable<Insurance>
{
// NOTE: these fields are protected!!!!! If I am to inherit
// these , they must be protected (private allows NO other classes
// to use them) - protected means this class and any class that inherits
// from this one can use these
protected Customer customer;
protected double yearlyRate;
protected int policyNumber;
// for currency output
NumberFormat currency = NumberFormat.getCurrencyInstance();
// a static variable used to make the insurance id unique
public static int num = 1000;
// full constructor - automatically creates a new Insurance id
public Insurance (Customer cust)
{
customer = cust;
policyNumber=num;
num++;
}
// used for reading from a file where policy number and rate already set
public Insurance (Customer cust, int polNum, double yrRate)
{
customer = cust;
policyNumber=polNum;
yearlyRate = yrRate;
}
// the empty constructor but still sets a unique id
public Insurance ()
{
policyNumber=num;
num++;
}
// the calcRate method is used to set the yearlyRate
// NOTE: this is an abstract class. That means it can not be used (has no body)
// but by putting here, we require any class that inherits from the Insurance class
// to fully implement this method if they want to be a concrete class
public abstract void calcRate();
// toString method. Note - we let the customer write themself out since they have their own toString method
// a class should only write out their fields in the toString method
public String toString ()
{
return (customer.toString() + " with policy number " + policyNumber + " has a yearly rate of " + currency.format(yearlyRate));
}
// getters and setters
public Customer getCustomer() {
return customer;
}
public int getPolicyNumber() {
return policyNumber;
}
public double getYearlyRate() {
return yearlyRate;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void setPolicyNumber(int policyNumber) {
this.policyNumber = policyNumber;
}
public void setYearlyRate(double yearlyRate) {
this.yearlyRate = yearlyRate;
}
// required by the Comparable interface. Describes how to compare two Insurance instances
// In this one, we want to compare policy numbers
// Look at how we did the one in the Customer blueprint
// Look at the one in the search_sort.Account class
// Note the difference if you are sorting on primitives versus objects
public int compareTo(Insurance ins)
{
// you get to write this!!
return -1;
}
}
Explanation / Answer
public class ArtInsurance extends Insurance {
//variables
private String description;
private double value;
//constructor which internally calls the super class constructoor which sets values to super class variables as well
public ArtInsurance(Customer cust, String description, double value,int policyNum,double yearlyRate) {
super(cust,policyNum,yearlyRate);
this.description = description;
this.value = value;
}
//constructor
public ArtInsurance(String description, double value) {
super();
this.description = description;
this.value = value;
}
//getters and setters
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
@Override
public void calcRate() {
// TODO Auto-generated method stub
yearlyRate=0.6;
}
//toString()
@Override
public String toString() {
return "ArtInsurance [description=" + description + ", value=" + value
+ ", customer=" + customer + ", yearlyRate=" + yearlyRate
+ ", policyNumber=" + policyNumber + "]";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.