Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

1. Finish the HealthInsurance and LifeInsurance classes. Uncomment my instances in main() of TrackInsurance. The red Xs should be gone. 2. Add a class called

image text in transcribed

1. Finish the HealthInsurance and LifeInsurance classes. Uncomment my instances in main() of TrackInsurance. The red Xs should be gone.

2. 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.

3.The program that runs is TrackInsurance. Open this up and add five instances of your ArtInsurance class at the noted location in the main method. Use whatever data you like.

4. Finish the compareTo() method in the Insurance class.

5.Finish the code for the methods in TrackInsurance so that all the menu items work.

CODES

AutoInsurance

// "extends" = inherits from

// So-- AutoInsurance gets all of the fields and methods from the Insurance class

// and can add or override any that it wants

public class AutoInsurance extends Insurance {

// we add one field - the number of cars

private int numCars;

// full constructor - but since the full constructor for the Insurance class

// sets customer, we pass it to that constructor.

// super = "class above me"

public AutoInsurance (Customer cust,int cars)

{

super (cust);

numCars = cars;

calcRate();

}

// empty constructor

public AutoInsurance ()

{

}

// constructor for reading from a file

public AutoInsurance (Customer cust, int polNum, double yrRate, int numC)

{

super(cust,polNum,yrRate);

numCars = numC;

}

// we were required to write a complete calcRate method by the abstract method in the

// Insurance class. Must have the exact same signature

public void calcRate()

{

yearlyRate = 500*numCars;

}

// we override the toString method from the Insurance class

public String toString ()

{

String ans = super.toString(); // print out the toString method from my super (ie the Insurance class)

return ans + (" for auto insurance. The number of cars are " + numCars + "."); // add on the info for this class

}

// getters and setters - only for the fields added by this class

public int getNumCars() {

return numCars;

}

public void setNumCars(int numCars) {

this.numCars = numCars;

}

}

Customer

/**

* Customer class

* used to describe a Customer object

* @author db2admin

*

**/

public class Customer implements Comparable {

// three private fields

/**

* used to hold the Customer's last name

*/

private String last;

/**

* used to hold the Customer's first name

*/

private String first;

/**

* a unique Customer id - generated by the program

*/

private int id;

/**

* a static variable used to make Customer numbers unique

* NOTE: this should be private unless you need to access it from another class

* We will not need that here---but will need to do so if you were writing and

* reading files, so I made it public in case that extension was needed

*/

public static int num=200;

/**

* Full Constructor - used to creat a new customer

* Creates a new unique id for them

* @param custLast - Customer last name

* @param custFirst - Customer first name

*/

public Customer(String custLast, String custFirst)

{

last=custLast;

first=custFirst;

id = num;

num++;

}

/**

* empty constructor but still creates a unique id for them

*/

public Customer()

{

id = num;

num++;

}

/**

* used for reading back from a file where id is already set

* read the id - do not create a new one

* @param custLast - Customer last name

* @param custFirst - Customer first name

* @param idNum - Customer ID

*/

public Customer(String custLast, String custFirst, int idNum)

{

last=custLast;

first=custFirst;

id = idNum;

}

/**

* used to convert the object to a String output

* @see java.lang.Object#toString()

*/

public String toString()

{

return first + " " + last +",Id=" + id;

}

/**

* used for writing to a file

* @return String

*/

public String toStringF()

{

return first + "|" + last +"|" + id;

}

/**

* used to define how to compare two Customer objects

* @see java.lang.Comparable#compareTo(java.lang.Object)

*

**/

public int compareTo(Customer c)

{

if ((c.getLast() + c.getFirst()).compareTo( getLast() + getFirst())>0)

return 1;

else if ((c.getLast() + c.getFirst()).compareTo( getLast() + getFirst())

return -1;

else

return 0;

}

/**

* used to get the first name

* @return a String that is the first name

*/

public String getFirst() {

return first;

}

/**

* used to get the id

* @return a String that is the id

*/

public int getId() {

return id;

}

/**

* used to get the last name

* @return a String that is the last name

*/

public String getLast() {

return last;

}

/**

* used to set the first name

* @param String - the first name

*/

public void setFirst(String string) {

first = string;

}

/**

* used to set the id

* @param int - that is the id

*/

public void setId(int i) {

id = i;

}

/**

* used to set the last name

* @param string that is the last name

*/

public void setLast(String string) {

last = string;

}

}

HealthInsurance

// "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

// creat a full constructor for a new object. Make certain it sets the rate

public HealthInsurance (Customer cust, int dependents)

{

// you code the body

}

// 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

}

Insurance

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

{

// 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;

}

}

LifeInsurance

// "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

}

TrackInsurance

import java.util.*;

public class TrackInsurance extends Object {

public static Scanner scan = new Scanner(System.in);

// method that runs first

public static void main(String[] args) throws Exception {

// make an ArrayList of customers and insurance policies

ArrayList cust = new ArrayList();

// note - the ArrayList below can hold Insurance objects

// but with inheritance, that includes Auto, Health, Life and Art

ArrayList ins = new ArrayList();

// create some fake customers (used for testing the program)

Customer c = new Customer("Duck", "Donald");

Customer c1 = new Customer("Mouse", "Minnie");

Customer c2 = new Customer("Mouse", "Mickey");

// add the customers to the array list

cust.add(c2);

cust.add(c1);

cust.add(c);

// make and add some insurance policies to the ArrayList

ins.add(new AutoInsurance(c, 2));

ins.add(new AutoInsurance(c1, 3));

/*ins.add(new HealthInsurance(c, 5));

ins.add(new HealthInsurance(c2, 1));

ins.add(new LifeInsurance(c, 30000, 65));

ins.add(new LifeInsurance(c1, 400000, 34));*/

// add your ArtInsurance instances here

//ins.add(new ArtInsurance(.....));

int choice = 0;

while (choice >= 0) {

choice = menu();

if (choice == 1)

printAllCustomers(cust);

else if (choice == 2)

printAllInsurance(ins);

else if (choice == 3) {

System.out

.println("Now lets find the information for a certain policy number");

System.out.println("What policy number do you want to find?");

int num = scan.nextInt();

printPolicy(ins, num);

} else if (choice == 4) {

System.out

.println("Now let's find all of the policies for a given customer");

System.out.println("What is the customer id?");

int custNum = scan.nextInt();

getCustomer(ins, custNum);

} else if (choice == 5)

sortCustNum(ins);

else if (choice == 6)

sortPolNum(ins);

else if (choice == 7) {

System.out.println("Bye!!!!!");

choice = -1;

}

} // end while

}

public static int menu() {

System.out.println("Choice:");

System.out

.println(" 1. Print all customers (call the toString method)");

System.out

.println(" 2. Print all insurance information (call the toString method)");

System.out

.println(" 3. Given a policy number, print the policy information");

System.out

.println(" 4. Find all of the policies for a given customer");

System.out

.println(" 5. Sort the insurance policy information by customer number");

System.out

.println(" 6. Sort the insurance policy information by policy number");

System.out.println(" 7. QUIT!! ");

System.out.println(" CHOICE:");

int value = scan.nextInt();

return value;

}

// write a printAllCusts method that prints out the toString method for all

// of the customers

public static void printAllCustomers(ArrayList cust) {

}

// write a printAllInsurance method that prints out the toString method for

// all of the insurance policies

public static void printAllInsurance(ArrayList insure) {

// print out all of the information

for (Insurance ins : insure)

System.out.println(ins.toString());

}

// write a printPolicy method that prints the information for the policy

// number

// passed in or the statement "That policy does not exist" if it is not

// present

public static void printPolicy(ArrayList insure, int num) {

}

// write a getCustomer method that prints the information for all of the

// policies for a given customer

// that customer number is passed in. If none, have it print

// "There are no policies for that customer"

public static void getCustomer(ArrayList insure, int num) {

}

// write a method that sorts the policies by policy number

// look at the example in the search_sort package

public static void sortPolNum(ArrayList insure) {

Collections.sort(insure);

}

// write a method that sorts the policies by customer number

// this one is tougher since you can not use the Collections.sort() method

// so you need to just slug out some code.

// Look at the bubble sort from the SortByHand in the search_sort package

// You will want to do something similar

// Here is some pseudocode to help

//

public static void sortCustNum(ArrayList insure) {

{

for (int out = insure.size() - 1; out > 1; out--)

for (int in = 0; in

// get the first insurance policy

// get the customer from that insurance policy

// get the customer number from that insurance policy

// get the second insurance policy

// get the customer from that insurance policy

// get the customer number from that insurance policy

// We want to check to see if the second customer number is

// less than the first one

// NOTE: When comparing customer numbers:

// SortByHand uses Strings so it uses the compareTo()

// method.

// We are comparing integers so we can just use

// if the second customer number is less than the first one

// swap the two insurance policies in the original "insure"

// ArrayList

// check out the SortByHand to see how to swap.

}

}

}

}

Customen Mrst Soning oCustomerin String in String) voi Customer:vold CusomerSoning in String i Ino ogeSering String) vod oeSering) void Autolnsurance Lifelnsurance d Healthlnsurance age in Auinurancei Customer in intvoid cacRa0:void ostring Sering Customen Mrst Soning oCustomerin String in String) voi Customer:vold CusomerSoning in String i Ino ogeSering String) vod oeSering) void Autolnsurance Lifelnsurance d Healthlnsurance age in Auinurancei Customer in intvoid cacRa0:void ostring Sering

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students explore these related Databases questions