0

我正在编写程序并且我已经完成了所有工作,但是我无法通过我的教授给我们测试程序的部分测试。它告诉我,在使用空数据运行 setName 和 getName 后,我的名称字段为空。确保 mutator 在 null 数据上将 name 设置为空字符串。确保访问器没有错误地返回空值。它并没有说我的 setLicense 和 getLicense 是否有任何问题,我感觉它也会有同样的问题,因为它们非常相似。我认为这个问题以前没有被问过,但是很抱歉。

public class Pilot
{
private String name;
private String license;

/** Parameterized constructor for Pilot.
 * 
 * Sets the fields using the parameter values.
 */
public Pilot(String name, String license)
{
    this.name = name;
    this.license = license;
}  

/** No-arg Constructor for Pilot.
 * 
 * Sets the fields name and license to blank instead of them being null. 
 * This prevents the program from crashing if another method is called 
 * before the name and license objects' fields are made to reference 
 * the String objects.
 */
public Pilot()
{
    name = "";
    license = "";
}    

//I have clue if this getLength() is needed. I was just trying 
//code from my textbook
public int getLength()
{
    int len = 0;

    if (name != null)
        len += name.length();

    if (license != null)
        len += license.length();   

    return len;

}    

/** Mutator for name.
 * 
 * @param {String} name - Somebody's name.
 */
public void setName(String name)
{
    this.name = name;
}

/** Mutator for license.
 * 
 * @param {String} license - Somebody's license.
 */
public void setLicense(String license)
{
    this.license = license;
}

/** Accessor for name.
 * 
 * @return Returns name
 */
public String getName()
{
    return name;
}

/** Accessor for license.
 * 
 * @return Returns license
 */
public String getLicense()
{
    return license;
}    

/** copy() method for Pilot class.
 * 
 * @return Returns p A new object that is an object of Pilot 
 * @param {Object[]} - copy method of Pilot
 */    
public Pilot copy()
{
    // Create a new Pilot object and initialize it
    // with the same data held by the calling object.
    Pilot pilot = new Pilot(name, license);

    // Return a reference to the new object.
    return pilot; 

    // book code, may need as reference
    // Pilot copyObject = new Pilot(name, license);
    // return copyObject;        
}

/**
 * toString() method for Pilot class.
 * 
 * @return Returns String.format which determines the print format of the 
 * pilot later in the class.
 * @param {String} - toString() method 
 */    
public String toString()
{
    return String.format("%s %1s %1s %1s %1s", 
                         "Name:",name, "-", 
                         "License:", license);
}    

/** 
 * equals() method for Pilot class.
 * 
 * @return Returns status
 * @param {Object[]} pilot 
 */    
public boolean equals(Pilot pilot)
{   
    boolean status;
    // Determine whether this object(pilot) name and
    // license fields are equal to Pilot's 
    // name and license fields.
    if (this.name.equals(pilot.name) &&
        this.license.equals(pilot.license))
        status = true;  // Yes, the objects are equal.
    else
        status = false; // No, the objects are not equal.

    // Return the value in status.
    return status;
}

/**
 * The main method.
 * 
 * @param {args} Runs the main method. A new Pilot object is created(pilot) 
 * with values for the name and license Strings. The new Pilot object(pilot)
 * is then printed as "Name: Tom - License: 12345-2" based off the 
 * formatting in the toString() method.
 */    
public static void main(String[] args)
{
    Pilot pilot = new Pilot("Tom", "12345-2");  
    System.out.println(pilot);
}

}

4

1 回答 1

0

确保 mutator 在 null 数据上将 name 设置为空字符串。

你在哪里实现这个逻辑?

   if (name == null)
       {
         name = String.Empty;
       }
于 2015-11-03T18:32:49.173 回答