Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Given Code: /** * Test Harness for PA03 * * DO NOT CHANGE CODE IN THIS TEST HARNESS! * * This test harness, with

Javaimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedGiven Code:

/**

* Test Harness for PA03 * * DO NOT CHANGE CODE IN THIS TEST HARNESS! * * This test harness, with the 'helper' class KeyboardInput, will allow for continuous data entry from the keyboard. * * You will want to fully test each validation you've written. Only one validation error per object entry can be * caught and reported. This means you'll need to do A LOT OF TESTING to test all of your validations. * * */

//import KeyboardInput // The import is not necessary, but it's good to remember we're using this!

public class Test_XcptnHandling { // This object ( myPolicy) provides a 'holding space' for a reference to an object in the InsurancePolicy hierarchy. // NOTE: We are NOT INSTANTIATING an InsurancePolicy object here, just setting up a reference. private static Policy myPolicy; /************************************************************************************************************* * The main method will consist of a continuous input loop based on the boolean variable goAgain. * Use a while loop (while goAgain = true) to contol processing: * Ask user which type of policy to create * If Auto * call method to create Auto object * ElseIf HomeOwners * call method to create HomeOwners object * Else * Tell user valid options * Ask user whether to continue *************************************************************************************************************/ public static void main(String[] args) { boolean goAgain = true; // Set the initial value of the loop control variable to continue the loop. while ( goAgain ) // Initiate an input loop based on the control variable { try // The try block is placed INSIDE the loop to allow continous input { /* * The following statement calls the acceptString method of KeyboardInput. The charAt method of the returned * String object is then called to retrieve the first character of the String. That char is then converted * to upper case and the resulting value is stored in the variable polType */ char polType = Character.toUpperCase( KeyboardInput.acceptString( "Build an Auto policy (A) or a HomeOwners policy (H)? " ).charAt(0) ); /* * Test variable polType. If the value is 'A', go on to collect data to create an Auto object. * If the value is 'H', collect data to create a HomeOwners object. * If the value is neither, let the user know what the acceptable values are. */ if( polType == 'A' ) { buildAutoPolicy( ); // Call method to collect data and instantiate an Auto object } else if( polType == 'H' ) { buildHomeOwnersPolicy( ); // Call method to collect data/instantiate a HomeOwners object } else { // Remind user of the acceptable responses to the question about policy type... System.out.printf( "%n%s%n", "You must select Auto (A) or HomeOwners (H)." ); } } // end try block catch( PolicyException buildError ) { // Catch any validation exception that may have been thrown and display to the console. System.out.printf( "%n%s%n", buildError.getMessage( ) ); } // end catch block // Aks user if they wish to continue the loop -- the loop does not end if a PolicyException was thrown. goAgain = KeyboardInput.acceptBoolean( "Do you wish to enter another? true or false " ); } // end while loop System.out.printf( "%nThanks for playing!%n" ); } // end main method /************************************************************************************************************* * Use methods of KeyboardInput to deliver prompts and accept input from user. * Collect values for all non-static instance variables fo class Auto. * Instantiate an Auto object using those values; assign the object reference to myPolicy * Call the toString method of myPolicy to output information about the instantiated object * * IF a PolicyException is thrown, it is NOT CAUGHT AT THIS LEVEL; it is 'passed through' to the main method * and will be caught & handled there. *************************************************************************************************************/ private static void buildAutoPolicy( ) throws PolicyException { // Variables to hold values required for the superclass level (InsurancePolicy) String own, insd, pol; double prem; // Variables to hold values required for Auto subclass String mkMod, vin; int year, coll, comp, uim, ded; int[] limits = new int[3]; // Gather superclass level values own = KeyboardInput.acceptString( "Policy Owner: " ); insd = KeyboardInput.acceptString( "Insured: " ); pol = KeyboardInput.acceptString( "Policy Number (2 letters, 5 digits): " ); prem = KeyboardInput.acceptDouble( "Annual premium (no dollar sign): " ); // Gather Auto subclass level values mkMod = KeyboardInput.acceptString( "Make & Model (i.e., Ford Fusion): " ); year = KeyboardInput.acceptInteger( "Year of car (four digits): " ); vin = KeyboardInput.acceptString( "VIN (17 characters): " ); coll = KeyboardInput.acceptInteger( "Collision limit (no dollar sign): " ); comp = KeyboardInput.acceptInteger( "Compehensive limit (no dollar sign): " ); uim = KeyboardInput.acceptInteger( "Uinsured Motorist limit (no dollar sign): " ); ded = KeyboardInput.acceptInteger( "Deductible (no dollar sign): " ); // load limits array limits[0] = coll; limits[1] = comp; limits[2] = uim; // Instantiate object; set myPolicy to refer to new object myPolicy = new Auto( own, insd, pol, prem, mkMod, year, vin, limits, ded ); System.out.printf( "%n%s%n", myPolicy.toString( ) ); } // end buildAutoPolicy /************************************************************************************************************* * Use methods of KeyboardInput to deliver prompts and accept input from user. * Collect values for all non-static instance variables fo class HomeOwners. * Instantiate an HomeOwners object using those values; assign the object reference to myPolicy * Call the toString method of myPolicy to output information about the instantiated object * * IF a PolicyException is thrown, it is NOT CAUGHT AT THIS LEVEL; it is 'passed through' to the main method * and will be caught & handled there. *************************************************************************************************************/ private static void buildHomeOwnersPolicy( ) throws PolicyException { // Variables to hold values required for the superclass level (InsurancePolicy) String own, insd, pol; double prem; // Variables to hold values required for HomeOwners subclass String address; int type, struct, cont; double deduct; boolean umb;

// Gather superclass level values own = KeyboardInput.acceptString( "Policy Owner: " ); insd = KeyboardInput.acceptString( "Insured: " ); pol = KeyboardInput.acceptString( "Policy Number (2 letters, 5 digits): " ); prem = KeyboardInput.acceptDouble( "Annual premium (no dollar sign): " ); // Gather HomeOwners subclass level values address = KeyboardInput.acceptString( "Property Address: " ); type = KeyboardInput.acceptInteger( "Property Type Code (1 - 5): " ); struct = KeyboardInput.acceptInteger( "Insured value of structure: " ); cont = KeyboardInput.acceptInteger( "Insured value of contents: " ); deduct = KeyboardInput.acceptDouble( "Deductible as a Percentage of value (i.e., .05 for 5%): " ); umb = KeyboardInput.acceptBoolean( "Is this part of an Umbrella contract (true or false)?" ); // Instantiate object; set myPolicy to refer to new object myPolicy = new HomeOwners( own, insd, pol, prem, address, type, struct, cont, deduct, umb ); System.out.printf( "%n%s%n", myPolicy.toString( ) ); } // end buildHomeOwnersPolicy } // end Test_XcptnHandling test harness

----------------------------------------------------------------------------

/** * This is essentially a 'helper' class holding methods for retrieving keyboard input * * The methods assume a Scanner object named input has been associated with the keyboard. * * Each static method requires as input a String value to be used as a prompt to the user. * * Each static method returns a different primitive data type to the caller. */

import java.util.Scanner;

public class KeyboardInput { public static Scanner input = new Scanner( System.in ); /******************************************************************************************************************* * Methods for collecting user input from keyboard *******************************************************************************************************************/ /******************************************************************************** * This method returns a String * 1. Ask the user for the necessary input * 2. Return that input to the calling method ********************************************************************************/ public static String acceptString( String prompt ) { System.out.printf( "%n%s%n", prompt); return input.nextLine(); } // end acceptString /******************************************************************************** * This method returns an int * 1. Ask the user for the necessary input * 2. Clear the 'hanging' line feed from the keyboard * 3. Return the int value to the calling method ********************************************************************************/ public static int acceptInteger( String prompt ) { System.out.printf( "%n%s%n", prompt ); int aNumber = input.nextInt(); input.nextLine(); return aNumber; } // end acceptInteger /******************************************************************************** * This method returns a double * 1. Ask the user for the necessary input * 2. Clear the 'hanging' line feed from the keyboard * 3. Return the double value to the calling method *******************************************************************************/ public static double acceptDouble( String prompt ) { System.out.printf( "%n%s%n", prompt); double aNumber = input.nextDouble(); input.nextLine(); return aNumber; } // end acceptDouble

/******************************************************************************** * This method returns a float * 1. Ask the user for the necessary input * 2. Clear the 'hanging' line feed from the keyboard * 3. Return the float value to the calling method *******************************************************************************/ public static float acceptFloat( String prompt ) { System.out.printf( "%n%s%n", prompt); float aNumber = input.nextFloat(); input.nextLine(); return aNumber; } // end acceptDouble /******************************************************************************** * This method returns a boolean * 1. Ask the user for the necessary input * 2. Return the boolean value to the calling method *******************************************************************************/ public static boolean acceptBoolean( String prompt ) { System.out.printf( "%n%s%n", prompt); boolean aSwitch = input.nextBoolean(); return aSwitch; } // end acceptDouble }

Policy (abstract) implements BookValue Notes Attributes private owner: String Policy owner's full, legal name. is may not be a null String or all spaces; if the offered value is null or all spaces, throw an exception and do not create the Policy object. private insured: String Full, legal name of named insured on policy is may not be a null String or all spaces, if the offered value is null or all spaces, throw an exception and do not create the Policy object. private polNbr: String Unique identifier for specific policy s policy number must meet these requirements: The first two (2) characters must be alphabetic *These will be followed by exactly 5 digits If the offered value does not meet the requirements, throw an exception and do not create the Policy object. Use a Annual to validate the offered value olPrem: double ium amount for Operations public Policyl ): Polic public Policy( String own, Description Null constructor, establishes no instance variable values Constructor accepts four (4) parameter values, calls set methods and establishes instance variable values as appropriate String insd, String nbr, double prem): Polic ublic identifyContract Stri Returns polNbr public toStringl : String Returns a String reference to a formatted description of the Policy object owner owns Polic LNbr insuring insured, with a um of polPrem NOTES: Set & get methods are not listed here, but are assumed to be present. Further, standard naming is assume Policy (abstract) implements BookValue Notes Attributes private owner: String Policy owner's full, legal name. is may not be a null String or all spaces; if the offered value is null or all spaces, throw an exception and do not create the Policy object. private insured: String Full, legal name of named insured on policy is may not be a null String or all spaces, if the offered value is null or all spaces, throw an exception and do not create the Policy object. private polNbr: String Unique identifier for specific policy s policy number must meet these requirements: The first two (2) characters must be alphabetic *These will be followed by exactly 5 digits If the offered value does not meet the requirements, throw an exception and do not create the Policy object. Use a Annual to validate the offered value olPrem: double ium amount for Operations public Policyl ): Polic public Policy( String own, Description Null constructor, establishes no instance variable values Constructor accepts four (4) parameter values, calls set methods and establishes instance variable values as appropriate String insd, String nbr, double prem): Polic ublic identifyContract Stri Returns polNbr public toStringl : String Returns a String reference to a formatted description of the Policy object owner owns Polic LNbr insuring insured, with a um of polPrem NOTES: Set & get methods are not listed here, but are assumed to be present. Further, standard naming is assume

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

What is a spectator ion? Illustrate with a complete ionic reaction.

Answered: 1 week ago