有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中完成类的精确副本的构造函数的单元测试

我在学校的一个实验室工作,目的是通过人寿保险,并进行测试,以确保我的施工人员正常工作。我有常规的建设者,但我对第83行真正需要什么感到困惑。当我编译并提交作业时,我没有得到任何信息,除了该区域的一个粉红色突出显示。我尝试创建一个新的策略对象插入

WholeLifePolicy WholeLifePolicy2 = new WholeLifePolicy;

这不是它想要的

}
       /**
         * Constructor for objects of class WholeLifePolicy.
         * This constructor creates an exact copy of a
         * WholeLifePolicy object.
         *
         * @param inPolicyObject   WholeLifePolicy object.
         *
         */
        public WholeLifePolicy(WholeLifePolicy inPolicyObject)
        {
            this.setPolicyNum(inPolicyObject.getPolicyNum());
              **// your code here, complete the constructor**


        }

我还尝试从WholeLifePolicy类初始化实例变量,但我知道这不会起作用,因为副本无论如何都会接受这些参数

我真的不知道该怎么办。我在网上搜索过,在这里,我不断获得有关克隆和暗拷贝的信息。这不是我应该做的。这只是我第一节编程课的第六周。我也联系了我的老师,但还没有得到回复

public class WholeLifePolicy
{
    // instance variables
    private String policyNum;
    private double faceValue;
    private int policyYrs;
    private String beneficiary;

    // your code here - code the remaining 3 instance fields

    // constants
    /**
     * surrender rate.
     */
    public static final double SURRENDER_RATE = .65;

    /**
     * ADMINFEE.
     */
    public static final double ADMINFEE = .005;

    // constructors and methods

    /**
     * Constructor for objects of class WholeLifePolicy.
     * This is the default constructor.
     *
     * Default values are:
     *     face value = 0.0
     *     policy years = 0
     *     policy number WLP9999999
     *     beneficiary Unknown
     *
     */
    public WholeLifePolicy()
    {
        this.setPolicyNum("WLP9999999");
        this.setFaceValue(0.0);
        this.setPolicyYrs(0);
        this.setBeneficiary("Unknown");
    }
    /**
     * Constructor for objects of class WholeLifePolicy.
     * A policy number will be auto-generated.
     *
     * @param inPolicyNum  Number of the policy
     * @param inFaceValue  Face Value of policy.
     * @param inPolicyYrs  Length of policy in years.
     * @param inBeneficiary name of the beneficiary
     *
     */
    public WholeLifePolicy(String inPolicyNum, double inFaceValue,
       int inPolicyYrs, String inBeneficiary)
    {
        // Initialize instance variables
        // Using values passed in as parameters
        this.policyNum = inPolicyNum;
        this.faceValue = inFaceValue;
        this.policyYrs = inPolicyYrs;
        this.beneficiary = inBeneficiary;

*} / *类WholeLifePolicy的对象的构造函数。 *此构造函数创建一个 *全方位的政治目标。 * *@param inPolicyObject全局策略对象。 * */ 公共全局策略(policyObject中的全局策略) { 这setPolicyNum(inPolicyObject.getPolicyNum()); //在这里输入代码,完成构造函数

    }***

    /**
     * Set the Policy Face Value.
     *
     * @param inFaceValue  Face Value of policy.
     *
     */
    public void setFaceValue(double inFaceValue)
    {
        this.faceValue = inFaceValue;
    }
    /**
     * Get the Policy Face Value.
     *
     * @return double  Face Value of policy.
     *
     */
    public double getFaceValue()
    {
        return this.faceValue;
    }
    /**
     * Set the Policy Years.
     *
     * @param inPolicyYrs  Length of policy in years.
     *
     */
    public void setPolicyYrs(int inPolicyYrs)
    {
        this.policyYrs = inPolicyYrs;
    }
    /**
     * Get the Policy Years.
     *
     * @return int  Length of policy in years.
     *
     */
    public int getPolicyYrs()
    {
        return this.policyYrs;
    }
    /**
     * Set the Policy Number. Use to override the default
     * policy number or for copy object processing.
     *
     * @param inPolicyNum  Policy Number.
     *
     */
    public void setPolicyNum(String inPolicyNum)
    {
        this.policyNum = inPolicyNum;
    }
    /**
     * Get the Policy Number.
     *
     * @return String  Policy Number.
     *
     */
    public String getPolicyNum()
    {
        // your code here, replace the following statement
        return this.policyNum;
    }

    /**
     * Set the beneficiary. Use to override the default
     * beneficiary or for copy object processing.
     *
     * @param inBeneficiary Beneficiary to set up.
     *
     */
    public void setBeneficiary(String inBeneficiary)
    {
        this.beneficiary = inBeneficiary;
    }
    /**
     * Get the Beneficiary.
     *
     * @return String  Beneficiary.
     *
     */
    public String getBeneficiary()
    {
        // your code here, replace the following statement
        return this.beneficiary;
    }
    /**
     * Calculate surrender value.
     * Surrender value is calculated as:
     *      (surrender rate * face value) *
     *      (years held / policy years) - amount borrowed -
     *      (face value * administrative fee)
     *
     * @param inYrsHeld  Length in years policy held to date.
     * @param inBorAmt   Amount borrowed against policy if any.
     * @return double  Policy surrender value.
     *
     */
    public double surrenderVal(double inYrsHeld, double inBorAmt)
    {
        // Termination value is the surrender rate percentage of the face
        // value as a proportion of the years held to policy years less any
        // amount borrowed on the policy less a ADMINFEE on the
        // face value.
        // your code here, compute the surrender value and replace the
        // following line
        return (SURRENDER_RATE * faceValue) * (inYrsHeld) / (policyYrs) 
            - inBorAmt - (faceValue * ADMINFEE);  
    }
    /**
     * Return a string representation of the WholeLifePolicy.
     *
     * @return String  output string.
     *
     *  <pre>
     *  Produce output in the following format:
     *
     *  Policy Information:
     *  Policy #: WLP1000000
     *  Policy Years: 20
     *  Face Value: 50000.0
     *  Beneficiary: John Doe
     *
     *  </pre>
     */
    public String toString()
    {
        String output = "Policy Information:\n";
        output = output + "Policy #: " + this.getPolicyNum() + "\n";
        // your code here, finish the output string
        return output;
    }
}

这是测试课程,以防万一

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
 * The test class WholeLifePolicyTest.
 *
 * @author Tina  & edited by Jeremy 
 * @version Fall 2015
 */
public class WholeLifePolicyTest 
{
    /**
     * Default constructor for test class WholeLifePolicyTest.
     */
    public WholeLifePolicyTest()
    {
        // not used
    }
    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @Before
    public void setUp()
    {
        // not used
    }
    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    @After
    public void tearDown()
    {
        // not used
    }
    /**
     * Test empty (default) constructor #1.
     */
    @Test
    public void testConstructor1()
    {
        WholeLifePolicy policy = new WholeLifePolicy();
        // verify an object reference was created
        assertNotNull(policy);
        // verify policy number is not null
        assertEquals("WLP9999999", policy.getPolicyNum());
        // verify face value set to default values
        assertEquals(0.0, policy.getFaceValue(), .001);
        // verify policy years set to default values
        assertEquals(0, policy.getPolicyYrs());
        // verify beneficiary is Unknown
        assertEquals("Unknown", policy.getBeneficiary());
    }
    /**
     * Test explicit constructor #2 (policy#, face value, years, name).
     */
    @Test
    public void testConstructor2()
    {
        // we test passing in values greater than zero only
        String pnum = "WLP1234567";
        double value = 50000.0;
        int years = 20;
        String name = "Paul Young";
        WholeLifePolicy policy = new WholeLifePolicy(pnum, value, years, name);
        // your code here, verify the object exists and the instance
        // fields contain the expected values
        assertEquals("WLP1234567", policy.getPolicyNum());
        assertEquals(50000.0, policy.getFaceValue(), .001);
        assertEquals(20, policy.getPolicyYrs());
        assertEquals("Paul Young", policy.getBeneficiary());


    }
    /**
     * Test explicit constructor #3 (duplicate object).
     */
    @Test
    public void testConstructor3()
    {
        // we test passing in values greater than zero only
        String pnum = "WLP1234567";
        double value = 50000.0;
        int years = 20;
        String name = "Paul Young";
        WholeLifePolicy policy1 = new WholeLifePolicy(pnum, value, years, name);

        // your code here, verify the object exists and the instance
        // fields contain the expected values
        assertEquals("WLP1234567", policy1.getPolicyNum());
        assertEquals(50000.0, policy1.getFaceValue(), .001);
        assertEquals(20, policy1.getPolicyYrs());
        assertEquals("Paul Young", policy1.getBeneficiary());
    }
    /**
     * Test the setGetFaceValue method.
     */
    @Test
    public void testSetGetFaceValue()
    {
        // test passing in values greater than zero only
        double value = 75000.0;
        WholeLifePolicy policy = new WholeLifePolicy();
        policy.setFaceValue(value);
        assertEquals(value, policy.getFaceValue(), .0001);
    }
    /**
     * Test the setGetPolicyYrs method.
     */
    @Test
    public void testSetGetPolicyYrs()
    {
        // your code goes here, test the set and get methods for the
        // policy years methods
        int years = 25;
        WholeLifePolicy policy = new WholeLifePolicy();
        policy.setPolicyYrs(years);
        assertEquals(years, policy.getPolicyYrs());


    }
     /**
     * Test the SetGetPolicyNum method.
     */
    @Test
    public void testSetGetPolicyNum()
    {
        // your code goes here, test the set and get methods for the
        // policy number methods
        String policyNumber = "WLP7654321";
        WholeLifePolicy policy = new WholeLifePolicy();
        policy.setPolicyNum(policyNumber);
        assertEquals(policyNumber, policy.getPolicyNum());
    }

    /**
     * Test the SetGetBeneficiary method.
     */
    @Test
    public void testSetGetBeneficiary()
    {
        // your code goes here, test the set and get methods for the
        // beneficiary methods
        String benefactor = "Beetlejuice! Beetlejuice!";

        WholeLifePolicy policy = new WholeLifePolicy();
        policy.setBeneficiary(benefactor);
        assertEquals(benefactor, policy.getBeneficiary());

    }
    /**
     * Test the surrenderVal method.
     */
    @Test
    public void testSurrenderVal()
    {
        // we test passing in values greater than zero only
        String num = "WLP1234567";
        double value = 50000.0;
        int years = 20;
        String name = "Elton John";
        double yearsHeld = 15.5;
        double amtBorrowed = 10000.00;
        WholeLifePolicy policy = new WholeLifePolicy(num, value, years, name);
        double surVal = policy.surrenderVal(yearsHeld, amtBorrowed);
        // your code here, test to make sure the returned surrender value
        // is what was expected

        assertEquals(surVal, 14937.5, .001);
        //your code here, test to make sure the returned surrender value
        // is what is expected if the borrowed amount is 0
        double surVal2 = policy.surrenderVal(yearsHeld, 0);
        assertEquals(surVal2, 24937.5, .001);
    }
    /**
     * Test the toString method.
     */
    @Test
    public void testToString()
    {
        String num = "WLP1234567";
        double value = 50000.0;
        int years = 20;
        String name = "Katie Perry";
        WholeLifePolicy policy = new WholeLifePolicy(num, value, years, name);
        String text = policy.toString();
        assertTrue(text.contains("Policy Information:"));
        assertTrue(text.contains("Policy #:"));
        assertTrue(text.contains(num));
        // your code here, finish the testing of the toString() method
        // checking for both the headings and face value, policy years
    }
}

共 (0) 个答案