Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CISC 124 700 - Paul David Allison

Summer 2021

Object Oriented Programming

In this section, you will write five classes:

•   Insurance

•   LifeInsurance

•   AutoInsurance

•   HomeInsurance

•   InsuranceException

The LifeInsurance, AutoInsurance and HomeInsurance classes extend the Insurance class.

Insurance is an abstract class that serves as the base class for the LifeInsurance, AutoInsurance, and HomeInsurance subclasses.

InsuranceException is an exception class used by the other three classes. InsuranceException only needs the one constructor that accepts a String type message.

Details About the Insurance Class

Attributes

An Insurance object is described by the following attributes: policy number, monthly rate, number of claims, and years insured. See the details about the constructor for information on the type of these attributes.

Error Checking:

Throw an InsuranceException if the following conditions hold:

•     there is an attempt to set the number of claims to a value less than 0.

•     there is an attempt to set the number of years insured to a value less than 0.

Constructor

public Insurance(int policyNumber, int numberClaims, int yearsInsured)

Methods:

•     setMonthlyRate(): This abstract method will be overridden by the concrete sub-classes. The method returns the monthly rate for the insurance policy (type double). The way that we calculate the monthly rate differs depending on the type of insurance policy - life

insurance, auto insurance or home insurance.

•     toString(): Returns a string that includes the policy number, the number of claims, and the number of years insured by the Insurance policy in an easy-to-read format.

Details About the LifeInsurance Class

Attributes

A LifeInsurance object is described by the following attributes: policy number, monthly rate, number of claims, years insured, age, health rating, and whether the plan is covering pre-existing conditions.

Error Checking:

Throw an InsuranceException if the following condition holds:

•     there is an attempt to set the health rating to a value less than 1 or greater than 10.

•     there is an attempt to set the age to a value less than 0 or greater than 110.

Constructor

public LifeInsurance(int policyNumber, intnumberClaims, int yearsInsured, int age, int healthRating, Boolean preExisting)

Methods

•     setMonthlyRate(): The method returns the monthly rate for the insurance policy (double)

The base rate for life insurance is $75/month with the following additional fees added to the base rate:

- Add $25 dollars if the client is over the age of 45.

- Multiply the client's health rating by $5

- Add $15 dollars if the insurance plan is covering a pre-existing condition.

- Add $2 per previous claim. Subtract $10 if the client has been insured for over 15 years.

•     toString(): Returns a string that includes the policy number, the number of claims, the number of years insured, the health rating, and the pre-existing condition status of the LifeInsurance policy in an easy-to-read format.

Details About the AutoInsurance Class

An AutoInsurance object is described by the following attributes: policy number, monthly rate, number of claims, years insured, monthly kilometrage driven, and whether the plan holder

passed a driver education course.

Constructor

public AutoInsurance(int policyNumber, int numberClaims, int yearsInsured, double monthlyKilometrage, int yearsDriving, Boolean driversEducation)

Error Checking:

Throw an InsuranceException if the following conditions hold:

•     there is an attempt to set the monthly kilometrage driven to a value less than 0.

•     there is an attempt to set the number of years driving experience to a value less than 0.

Methods

•     setMonthlyRate(): The method returns the monthly rate for the insurance policy (type double).

The base rate for auto insurance is $150/month with the following additional fees added to the base rate:

- Add $20 per previous claim.

- Subtract $12 if the client has been insured for over 5 years and has been driving for 10+ years.

- Add $0.25 per additional kilometer over 16000 kilometers for monthly kilometrage.

- Subtract $5 if the client participated in drivers’ education.

•     toString(): Returns a string that provides a description of the AutoInsurance policy,

including the policy number, the number of claims, the number of years insured, the

yearly kilometrage, the number of years driving, and whether the plan holder completed drivers ’ education, in an easy-to-read format.

Details About the HomeInsurance Class

A HomeInsurance object is described by the following attributes: policy number, monthly rate, number of claims, years insured, value of the home, number of people living in the home, type of home (one of "townhouse", "bungalow", "cottage", "condominium").

Constructor

public HomeInsurance(int policyNumber, int numberClaims, int yearsInsured, double homeValue, int numResidents, String homeType)

Error Checking:

Throw an InsuranceException if the following conditions hold:

•     there is an attempt to set the value of the home to a value less than $15,000.

•     there is an attempt to set the number of residents of the home to a value less than 1 or greater than 10.

•     there is an attempt to set the type of home to a String other than one of "townhouse", "bungalow", "cottage", "condominium".

Methods

•     setMonthlyRate(): The method returns the monthly rate for the insurance policy (type double).

The base rate for home insurance is 0.7% of the home's value with the following additional fees added to the base rate:

- Add $25 for each previous claim.

- Subtract $20 if the client has been insured for over 20 years.

- Add $15 dollars if the insurance plan is covering a condominium or a townhouse.

- Add $20 dollars if the insurance plan is covering a bungalow or a cottage.

- Add $12.50 for each person residing in the home.

•     toString(): Returns a string that includes the policy number, the number of claims, the

number of years insured, the home value, the number of residents, and the type of home of the HomeInsurance policy in an easy-to-read format.

Comparing Insurance Plans

You must also write an equals and compareTo methods.

•     The equals method will return True if two insurance plans are equal and False otherwise.  Equality will be defined by the policy number, claim history, and years insured of the two insurance plans being the same.

•     Comparison, for the compareTo method, will be based on the monthly cost for the insurance plan.

Important Details

•     All attributes must be declared private. The base insurance  class must contain only the

policy number, monthly cost, number of claims, and years insured attributes which should not be re-declared in either of the child classes.

•     Please ensure that your classes include accessor ("getter") and mutator ("setter") methods for each of the instance variables.

•     My suggestion is that you write the exception class first followed by the Insurance class and then the other three. No other classes are required. Write just the minimum set of

methods needed. I recommend that you may wish to begin solving this question by drawing a flow-chart to visualize the hierarchy.

Question 1 (10 marks): In the space below, write the code for the InsuranceException class.

Question 2 (10 marks): In the space below, please write the code for the Insurance class.

Question 3 (10 marks): In the space below, please write the code for the LifeInsurance class:

Question 4 (10 marks): In the space below, please write the code for the AutoInsurance class.

Question 5 (10 marks): In the space below, please write the code for the HomeInsurance class.

General Java Programming

Question 6 (5 marks): Write a function called findTriplets that accepts an array of positive integers, called valArray, and an integer, called sumVal, as its parameters.

The findTriplets function will search the array for three numbers whose sum is equal to sumVal. We call these three numbers a "triplet".

If a triplet is found, findTriplets will return True. If a triplet does not exist, findTriplets will return False.

You may assume that the method will always be called with an array that contains at least three positive integers.

For example, consider the following array:  myArray = [1,4,6,7,8,2,1,7]

A call findTriplets(myArray, 14) would return True.

A call findTriplets(myArray, 30) would return False.

Question 7 (5 marks): Write a method called sumArrayLis that accepts two ArrayLists of

Integers as its parameters. The sumArraysLis function should return aprimitive array where each element in the array is the sum of the values at that corresponding index in the two original ArrayLists.

The ArrayLists that are passed in as the parameters to the function may not be identical in length. Your function will pad the smaller ArrayList with trailing zeros.

For example, given the following two ArrayLists, arrayLis1 and arrayLis2, you can assume that arrayLis1 is padded with two trailing zeros (indicated in red) to make

arrayLis1 the same length as arrayLis2.

arrayLis1 = [4, 5, 6, 2, 8, 0, 0]

arrayLis2 = [9, 2, 7, 5, 9, 5, 4]

If the following two ArrayLists of unequal length are passed into the sumArrayLis function:

arrayLis1 = [4, 5, 6, 2, 8]

arrayLis2 = [9, 2, 7, 5, 9, 5, 4]

A call to sumArrayLis(arrayLis1, arrayLis2) would return:

[13, 7, 13, 7, 17, 5, 4].

Question 8 (5 marks): Consider the following classes and interfaces.

public interface InterfaceA {

int sum(int val1, int val2, int val3);

int sum(int val1, int val2);

int subtract(int val1, int val2);

}

public abstract class ClassB {

public abstract int sum(int val1, int val2);

public abstract int divide(int val1, int val2);

public abstract int multiply(int val1, int val2);

}

public interface InterfaceC {

int findDifference(int val1, int val2);

}

public abstract class ClassD extends ClassB implements

InterfaceA, InterfaceC {

public int multiply(int val1, int val2){

return val1*val2;

}

}

Which methods still need to be implemented in ClassD to make ClassD concrete class instead of an abstract class?

You do not need to write the implementation for the methods - you can simply write the method signatures.

Be sure to identify the method name(s)parameter types, and numbers of parameters.

Question 9 (5 marks): We say that an array is a magic square if all row sums, all column sums,   and the two diagonal-sums are equal. Write a function called isMagic that accepts a 2D array of integers as its parameter and returns True if the array is a magic square and False otherwise.

Here is an example of a magic square. Notice that the sum of each of the rows, the columns, and diagonals are all equal to 15.

2   7   6

9   5   1

4   3   8

You may assume that the array that is passed into the function is square -- the lengths of the columns and the rows are all equal.

You are welcome to write any helper functions and call them from within your isMagic function.

Examples:

magicArray = new int[][] { {2,7,6}, {9,5,1}, {4,3,8} }

isMagic(magicArray) would return True

magicArray = new int[][] { {1,2,3}, {4,5,6}, {7,8,9} }

isMagic(magicArray) would return False

Generic Java Programming and ArrayLists

Question 10 (5 marks):

Write a single generic method called countTwice that accepts an array called lis and returns the number of values that appear exactly twice in the array. Your method should work for any type of array.

Consider the following two array declarations:

Double[] nums = {1.0, 4.0, 6.0, 18.0, 10.0, 13.0, 4.0, 6.0, 11.0};

String[] fruit = {"Apple", "Peach", "Plum", "Jam", "Apple", "Apricot", "Jam", "Peach"};

A call to countTwice(nums) would return 2 since there are 2 values in the nums array that appear twice (the values 4.0 and 6.0 all occur two times in the array).

A call to countTwice(fruit) would return 3 since there are 3 values in the fruit array that appear twice ("Apple""Peach""Jam").

Here is the header for your function:

public static <T> int countTwice (T[] lis)

Question 11 (5 marks): Write a function called countMatches that accepts an ArrayList of type

String as its parameter. The function returns the number of strings in the ArrayList that begin and end with the same character.

public static int countMatches(ArrayList<String> strings)

For example, suppose that there exists an ArrayList called words. The ArrayList is populated as follows:

ArrayList<String> words = new ArrayList<String>(); words.add("kayak");

words.add("local");

words.add("Canada");

words.add("Mazda");

words.add("divided");

words.add("Queen");

System.out.println(words);

A call to countMatches(words) would return 3 because there are 3 strings in the ArrayList words that begin and end with the same letter ("kayak", "local" and "divided").

Reminder: You may find the charAt built-in method helpful.

public char charAt(int index)

JavaFX and GUI Programming

Here, you are provided with a complete JavaFX program and a screenshot of the window that the code produces. You may assume that all of the necessary libraries have been imported. You will  be using this program for the next question.

 

 

Question 12 (4 marks):  In the space below, write code that uses a lambda function to add an event listener to the button1 node so that when the user enters a temperature in degrees

Celsius (a double value) into the text-field (txtField1) and clicks on button1label3 will display the result of converting the value entered in txtField1 to degrees Fahrenheit.

Example: if the user enters 25.2 into txtField1 and clicks on button1label3 will

display: 77.36

To convert temperatures in degrees Celsius to Fahrenheit, multiply the temperature in Celsius by 1.8 and add 32.

You may find some or all the following methods from the JavaFX API useful for this question:

•     The setText(String arg) method sets the text caption for the label to the String arg.

•     The getText() method returns the text entered in a TextField (return value is a string).

•     The setOnAction(EventHandler<ActionEvent> arg) method is used to attach an event listener to a button.

•     The functional interface EventHandler<ActionEvent> contains a single abstract method called handle. The signature of this method is: void handle(ActionEvent event);

Multiple Choice

Question 13 (1 mark): Abstract classes cannot contain any concrete methods.

o true

o false

Question 14 (1 mark): In the context of object-oriented programming, encapsulation can also be referred to as:

o information hiding

o extending

o inheritance

o immutable

Question 15 (1 mark): When a method in a child class has the same name, the same parameters, and the same return type as a method in the parent class, we say that the method in the subclass    the method in the parent class.

o overloads

o overrides

o extends

o implements

Question 16 (1 mark): A method called doSomething() does not return a value. Which of the following is the correct method signature?

o public static doSomething() {}

o public doSomething() {}

o public static void doSomething() {}

o public static void () {}

Short Answer

Question 17 (3 marks): Java is an object-oriented programming language.

Please describe the key features of the object-oriented programming paradigm. In your answer, please be sure to demonstrate your understanding of the following concepts:

•   classes

•   encapsulation

•   polymorphism

•   inheritance

Question 18 (3 marks): There are two important keywords in Java - extends and implements.

Please explain when we would use each keyword and the difference between

the extends and implements keywords. Include an example to illustrate your answer.

Question 19 (3 marks): How is a constructor different from a method? Using an example, support your answer with three specific points about the purpose of a constructor. In your answer, please be sure to demonstrate your understanding of the following concepts:

•   classes

•   instances

•   objects

•   attributes (or instance variables)

Question 20 (3 marks): Please explain why we would choose to write an abstract class.

In your answer, please use an example identify:

•     the purpose of an abstract method.

•     the implications that defining an abstract class has for subclasses that extend an abstract class containing an abstract method.