Test_Policy.java /* * This is a test harness for the Policy class. * * This proc
ID: 3876918 • Letter: T
Question
Test_Policy.java
/*
* This is a test harness for the Policy class.
*
* This process will exercise all methods included in the Policy class:
* > null constructor
* > full constructor
* -- all set methods
* > toString
* -- all get methods
* -- txtPolType method
*
* This process will use an array type to hold Policy objects and an enhanced for loop.
*
*
*/
public class Test_Policy
{
/*
* Global scoping for varialbes/data structures is STRONGLY DISCOURAGED, but here it makes
* our lives (and code) simpler, so we'll allow it.
* COMPLETE THE FOLLOWING ARRAY DECLARATION
*/
private static
/*
* The main method will only 'direct traffic' from this point forward.
* This main calls one method to build/load the array and a second method to process it.
*/
public static void main( String[] args )
{
buildArray( );
printToStrings( );
} // end main
/*
* The buildArray method instantiates 6 Policy objects and stores them in the globally
* declared array.
* The first object instantiated will use the null constructor; all remaining objects will
* call the full constructor of Policy.
* Thus, both constructors and all set methods will be tested.
*/
private static void buildArray( )
{
grpPolicy[0] = new Policy( ); // Call null constructor
grpPolicy[1] = new Policy( "Ted Arroway", // Call full constructor, which will call all set
"Ted Arroway", // methods
"HO658542",
2,
2563.58 );
grpPolicy[2] = new Policy( "Eleanor Arroway",
"Eleanor Arroway",
"AU002584",
1,
1503.27 );
grpPolicy[3] = new Policy( "Drumlin, LLC",
"David Drumlin",
"HO963214",
2,
5980.73 );
grpPolicy[4] = new Policy( "Hadden Industries",
"S.R. Hadden",
"HO658542",
2,
9870.85 );
grpPolicy[5] = new Policy( "Palmer Joss",
"Palmer Joss",
"AU456852",
1,
813.79 );
} // end buildArray
/*
* The printToStrings method will call the toString method of each object in the array and output
* the formatted String object provided.
* Thus, the toString method, the txtPolType method, and all get methods will be tested.
*/
private static void printToStrings( )
{
System.out.printf( "%n%nList of Policy Objects in Array grpPolicy%n%n" );
// Add the control statement for the ENHANCED FOR LOOP here
for
{
System.out.printf( "%s%n",
oneContract.toString( ) );
} // end for loop
} // end printToStrings
} // end Test_Policy
Explanation / Answer
public class Policy {
private String owner;
private String insured;
private String polNbr;
private int polType;
private double polPrem;
public Policy() {
// TODO Auto-generated constructor stub
}
/**
* @param owner
* @param insured
* @param polNbr
* @param polType
* @param polPrem
*/
public Policy(String owner, String insured, String polNbr, int polType, double polPrem) {
super();
this.owner = owner;
this.insured = insured;
this.polNbr = polNbr;
this.polType = polType;
this.polPrem = polPrem;
}
/**
* @return the owner
*/
public String getOwner() {
return owner;
}
/**
* @param owner the owner to set
*/
public void setOwner(String owner) {
this.owner = owner;
}
/**
* @return the insured
*/
public String getInsured() {
return insured;
}
/**
* @param insured the insured to set
*/
public void setInsured(String insured) {
this.insured = insured;
}
/**
* @return the polNbr
*/
public String getPolNbr() {
return polNbr;
}
/**
* @param polNbr the polNbr to set
*/
public void setPolNbr(String polNbr) {
this.polNbr = polNbr;
}
/**
* @return the polType
*/
public int getPolType() {
return polType;
}
/**
* @param polType the polType to set
*/
public void setPolType(int polType) {
this.polType = polType;
}
/**
* @return the polPrem
*/
public double getPolPrem() {
return polPrem;
}
/**
* @param polPrem the polPrem to set
*/
public void setPolPrem(double polPrem) {
this.polPrem = polPrem;
}
public String txtPolType(){
if (polType == 1){
return "AUTO";
}
else{
return "HOMEOWNERS";
}
}
public String toString() {
return(owner +" owns Policy "+ polNbr + " a(n) "+txtPolType()+" policy,insuring"+insured + " , with a premium of " + polPrem +".");
}
}
------------------------------------------------------------------------------
public class Test_Policy
{
/*
* ARRAY DECLARATION
*/
private static Policy[] grpPolicy = new Policy[6];
/*
* The main method will only 'direct traffic' from this point forward.
* This main calls one method to build/load the array and a second method to process it.
*/
public static void main( String[] args )
{
buildArray( );
printToStrings( );
} // end main
/*
* The buildArray method instantiates 6 Policy objects and stores them in the globally
* declared array.
* The first object instantiated will use the null constructor; all remaining objects will
* call the full constructor of Policy.
* Thus, both constructors and all set methods will be tested.
*/
private static void buildArray( )
{
grpPolicy[0] = new Policy( ); // Call null constructor
grpPolicy[1] = new Policy( "Ted Arroway", // Call full constructor, which will call all set
"Ted Arroway", // methods
"HO658542",
2,
2563.58 );
grpPolicy[2] = new Policy( "Eleanor Arroway",
"Eleanor Arroway",
"AU002584",
1,
1503.27 );
grpPolicy[3] = new Policy( "Drumlin, LLC",
"David Drumlin",
"HO963214",
2,
5980.73 );
grpPolicy[4] = new Policy( "Hadden Industries",
"S.R. Hadden",
"HO658542",
2,
9870.85 );
grpPolicy[5] = new Policy( "Palmer Joss",
"Palmer Joss",
"AU456852",
1,
813.79 );
} // end buildArray
/*
* The printToStrings method will call the toString method of each object in the array and output
* the formatted String object provided.
* Thus, the toString method, the txtPolType method, and all get methods will be tested.
*/
private static void printToStrings( )
{
System.out.printf( "%n%nList of Policy Objects in Array grpPolicy%n%n" );
// Add the control statement for the ENHANCED FOR LOOP here
for(Policy oneContract:grpPolicy)
{
System.out.printf( "%s%n",
oneContract.toString( ) );
} // end for loop
} // end printToStrings
} // end Test_Policy
-----------------------------------------------------------------
Sample output:
List of Policy Objects in Array grpPolicy
null owns Policy null a(n) HOMEOWNERS policy,insuringnull , with a premium of 0.0.
Ted Arroway owns Policy HO658542 a(n) HOMEOWNERS policy,insuringTed Arroway , with a premium of 2563.58.
Eleanor Arroway owns Policy AU002584 a(n) AUTO policy,insuringEleanor Arroway , with a premium of 1503.27.
Drumlin, LLC owns Policy HO963214 a(n) HOMEOWNERS policy,insuringDavid Drumlin , with a premium of 5980.73.
Hadden Industries owns Policy HO658542 a(n) HOMEOWNERS policy,insuringS.R. Hadden , with a premium of 9870.85.
Palmer Joss owns Policy AU456852 a(n) AUTO policy,insuringPalmer Joss , with a premium of 813.79.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.