Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Now define a DepreciatingPolicy class as a subclass of Policy which has the foll

ID: 3722557 • Letter: N

Question

Now define a DepreciatingPolicy class as a subclass of Policy which has the following: a private instance variable called rate of type float which represents the rate at which the policy depreciates each time a claim is made. For example, a rate of 0.10 means that the value of the policy (i.e., the amount) depreciates 10% each time a claim is made (we will discuss the making of claims in the next section). a public constructor which takes a float representing the amount and another float representing the rate. This constructor MUST make full/proper use of inheritance. a public get method for the rate. a toString() method that returns a String with the following example format: DepreciatingPolicy: 0001 amount: $320.00 rate: 10.0% You MUST make use of inheritance by calling the toString() from the superclass. Also, the rate must be properly formatted. an instance method called isExpired() which returns true if the amount of this policy has depreciated to 0 (assume a value less than 0.01 is zero). You MUST write this method without any IF/SWITCH/TERNARY conditional statements. an instance method called depreciate() which reduces the amount of the policy by the rate percentage. For example, if the amount is $100 and the rate is 0.10, then the amount after this method is called should be $90.

Define an ExpiringPolicy class as a subclass of Policy which has the following: a private instance variable called expiryDate of type Date (from java.util.Date package) that contains the date after which the policy is invalid. a public constructor which takes a float representing the amount and a Date representing the expiryDate. This constructor MUST make full/proper use of inheritance. a public constructor which takes a float representing the amount. This constructor MUST make full/proper use of inheritance. The expiryDate should then be set to exactly one year after the policy was created. Here is how you can do this: GregorianCalendar aCalendar = new GregorianCalendar(); aCalendar.add(Calendar.YEAR,1); expiryDate = aCalendar.getTime(); a public get method for the expiryDate. a toString() method that makes use of inheritance by calling the toString() from the superclass and returns a String with the following example format which includes the parentheses (i.e., look at the SimpleDateFormat class described in section 13.3 of the course notes in order to see how to format the date properly). Notice the difference (underlined) between expired and no-expired policies: ExpiringPolicy: 0001 amount: $320.00 expired on: April 30, 2001 (12:08PM) ExpiringPolicy: 0006 amount: $500.00 expires: May 23, 2023 (4:46AM) An instance method called isExpired() which returns true if the computer's current date is ON or AFTER the expiryDate and false otherwise. (Hint: the Date class has methods before(Date d) and after(Date d) ... see section 13.3 of the course notes).

Test your new classes with the following program:

Here is the expected output (your date on line 4 will differ):

Policy: 0001 amount: $320.00

DepreciatingPolicy: 0002 amount: $500.10 rate: 10.0%

DepreciatingPolicy: 0002 amount: $450.09 rate: 10.0%

ExpiringPolicy: 0003 amount: $1000.00 expires: May 16, 2018 (01:18PM)

ExpiringPolicy: 0004 amount: $2000.00 expires: January 02, 2021 (11:59PM) false

ExpiringPolicy: 0005 amount: $2000.00 expired on: April 01, 2013 (11:59PM) true

Create the following in the Client class: a public method called totalCoverage() which returns a float containing the total amount of coverage of all the client's policies. a public method called addPolicy(Policy p) which adds the given Policy to the array of policies, provided that the limit has not been reached ... and returns the Policy object, or null if not added. a public method called openPolicyFor(float amt) which creates a new Policy for the amount specified in the parameter and adds it to the client using (and returning the result from) the addPolicy() method a public method called openPolicyFor(float amt, float rate) which creates a new DepreciatingPolicy for the amount and rate specified in the parameters and adds it to the client using (and returning the result from) the addPolicy() method. a public method called openPolicyFor(float amt, Date expire) which creates a new ExpiringPolicy for the amount and expiry date specified in the parameters and adds it to the client using (and returning the result from) the addPolicy() method above. a public method called getPolicy(int polNum) which examines all Policy objects in policies. If one is found whose policyNumber matches the input parameter, then it should be returned by the method. Otherwise null is returned. a public method called cancelPolicy(int polNum) which removes the police from the client's list with the given policy number, if found. It should return true if the policy was found, otherwise it should return false. When a policy is removed from the array, the empty location in the array should be replaced by the last policy in the array. a public abstract method called makeClaim(int polNum) that returns a float.

Now create the two subclasses of Client, one called IndividualClient and the other called CompanyClient. Try compiling them. Notice that your test code will not compile. That's because subclasses DO NOT inherit constructors and java requires a constructor to be available. Do the following: Create a constructor (taking a single String argument) for each of these subclasses that calls the one in Client. Go back and alter your toString() method in Client class so that the proper client type is displayed. For example: CompanyClient 0001: Bob B. Pins To do this, you will want to figure out how to get the name of the class as a String. (hint: type this. and then let IntelliJ show a list of available methods. You need to find out what class you have and then find out the name of the class). Implement the makeClaim(int polNum) method in the IndividualClient class. If the policy with that number is not found or has expired, then nothing is to be done, just return 0. IndividualClients are allowed to make only 1 claim per regular Policy but DepreciatingPolicys and ExpiringPolicys can have multiple claims. So, you should cancel a regular policy after a claim is made on it. If the claim is being made for a DepreciatingPolicy, then you must make sure to depreciate the policy. This method should return the amount of the policy (amount after depreciating in the case of DepreciatingPolicys) if the claim was made ok, otherwise 0 is returned. In the Policy class, implement a method called handleClaim() that returns the amount of the policy. In the DepreciatingPolicy class, implement handleClaim() so that it returns the amount of the policy but also depreciates the policy. Note that the amount returned is the amount of the policy before it was depreciated. In the ExpiringPolicy class, implement handleClaim() so that it returns the amount of the policy as long as the policy has not yet expired, otherwise it returns 0. In the CompanyClient class, implement the makeClaim(int polNum) method so that it first checks to make sure the policy is one belonging to this client and then uses the double-dispatching technique by calling the handleClaim() method. You MUST NOT use any IF statements to determine the policy type ... that's would undo the advantages of double-dispatching. Note that CompanyClients DO NOT have their policy removed afterwards, and so you should make use of the getPolicy() method. If no policy is found with the given polNum, return 0;

Test it with the code below:

Here is the expected output Here are the Individual Client's policies: Policy: 0001 amount: $100.00 DepreciatingPolicy: 0002 amount: $200.00 rate: 10.0% ExpiringPolicy: 0003 amount: $300.00 expires January 02, 2020 (11: 59PM) ExpiringPolicy: 0004 amount: $400.00 expired on: June 04, 2009 (12:00PM) Making claims: Claim for policy 0001: $100.00 Claim for policy 0002: $180.00 Claim for policy 0003: $300.00 Claim for policy 0004:$ 0.00 Claim for policy 0005: $0.00 Here are the Individual Client's policies after claims: ExpiringPolicy: 0004 amount: $400.00 expired on: June 04, 2009 (12:00PM) DepreciatingPolicy: 0002 amount: $180.00 rate: 10.0% ExpiringPolicy: 0003 amount: $300.00 expires: January 02, 2020 (11: 59PM) The total policy coverage for this client: $880.00 Here are the Company C1ient's policies: Policy: 0006 amount: $1000.00 DepreciatingPolicy: 0007 amount: $2000.00 rate : 10.0% ExpiringPolicy: 0008 amount: $3000.00 expires: January 02, 2020 (11:59PM) ExpiringPolicy: 0009 amount: $4000.00 expired on: June 04, 2009 (12:00PM) Making claims: Claim for policy 0006: $1000.00 Claim for policy 0007: $2000.00 Claim for policy 0008: $3000.00 Claim for policy 0009:$0.00 Claim for policy 0005:$0.00 Here are the Company Client' s policies after claims: Policy:0006 amount: $1000.00 DepreciatingPolicy: 0007 amount: $1800.00 rate: 10.0% ExpiringPolicy: 0008 amount: $3000.00 expires: January 02, 2020 (11:59PM) ExpiringPolicy: 0009 amount: $4000.00 expired on: June 04, 2009 (12:00PM) The total policy coverage for this client: $9800.00 Cancelling policy #12 did it work: false Cancelling policy #8 did it work: true The total policy coverage for this client: $6800.00

Explanation / Answer

Alaram.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Alarm extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new Scene(root);

stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Alarm Application - Hassan Althaf");

stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

MainView.java

import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;

public class MainView implements Initializable {

@FXML
private TextField hoursField;

@FXML
private TextField minutesField;

@FXML
private TextField secondsField;

private final AlarmController alarmController;
private Timer timer;
private boolean timerOn = false;
private boolean alarmPlayed = false;
private boolean timerValidation = true;

@FXML
private void startAlarm(ActionEvent event) {
if(!timerOn) {
this.alarmPlayed = false;
this.timerValidation = true;

String hoursString = this.hoursField.getText();
String minutesString = this.minutesField.getText();
String secondsString = this.secondsField.getText();

if(hoursString.equals("")) {
hoursString = "0";
}

if(minutesString.equals("")) {
minutesString = "0";
}

if(secondsString.equals("")) {
secondsString = "0";
}

int hours = 0;
int minutes = 0;
int seconds = 0;

try {
hours = Integer.parseInt(hoursString);
minutes = Integer.parseInt(minutesString);
seconds = Integer.parseInt(secondsString);
} catch(Exception ex) {
this.timerValidation = false;
this.showWarning("Please enter a number only for the times!", "Invalid input received");
}

int totalTime = seconds + (minutes * 60) + (hours * 3600);

if(totalTime == 0) {
this.timerValidation = false;
this.showWarning("Please set a time for the alarm before starting!", "Time not found!");
}

if(this.timerValidation) {
TimerTask task = new TimerTask() {
private final int ALARM_TIME = totalTime;
private int secondCounter = 0;

@Override
public void run()
{
if(secondCounter < ALARM_TIME) {
secondCounter++;
} else {
if(timerOn) {
alarmController.playAlarm();
alarmPlayed = true;
}
timerOn = false;
cancel();
}
}
};

this.timer = new Timer();
this.timer.schedule(task, 0, 1000);

this.timerOn = true;
}
} else {
this.showWarning("An alarm is already running at the moment! If you wish to override the current alarm, please stop it first!", "Cannot override current alarm.");
}
}

public void showWarning(String message, String title) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}

@FXML
private void stopAlarm(ActionEvent event) {
if(this.timerOn) {
this.timer.cancel();
this.timer.purge();
this.timerOn = false;
}
if(this.alarmPlayed) {
this.alarmController.stopAlarm();
this.alarmPlayed = false;
}
}

public MainView() {
this.alarmController = new AlarmController();
}

@Override
public void initialize(URL url, ResourceBundle rb) {

}   
}

MainWindow.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" maxHeight="300.0" maxWidth="500.0" minHeight="300.0" minWidth="500.0" prefHeight="300.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.HassanAlthaf.MainView">
<children>
<Label layoutX="171.0" layoutY="30.0" text="Alarm Application" textFill="#404040">
<font>
<Font name="Lato Regular" size="20.0" />
</font>
</Label>
<Label layoutX="150.0" layoutY="68.0" text="Set the timer for your alarm below" />
<TextField fx:id="hoursField" layoutX="193.0" layoutY="108.0" />
<Label layoutX="150.0" layoutY="113.0" text="Hours: " />
<Label layoutX="138.0" layoutY="150.0" text="Minutes: " />
<TextField fx:id="minutesField" layoutX="193.0" layoutY="145.0" />
<TextField fx:id="secondsField" layoutX="193.0" layoutY="182.0" />
<Label layoutX="133.0" layoutY="187.0" text="Seconds: " />
<Button layoutX="251.0" layoutY="222.0" mnemonicParsing="false" text="Start" />
<Button layoutX="309.0" layoutY="222.0" mnemonicParsing="false" text="Stop" />
</children>
</AnchorPane>

AlarmController.java

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class AlarmController {
private MediaPlayer mediaPlayer;

public void playAlarm()
{
Media media = new Media(getClass().getResource("alarm.mp3").toString());
this.mediaPlayer = new MediaPlayer(media);
this.mediaPlayer.play();
}

public void stopAlarm()
{
this.mediaPlayer.stop();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote