2015-11-03 28 views
0

我正在開發程序,我得到了一切工作,但我無法通過我的教授給我們測試我們的程序的測試的一部分。它告訴我,運行setName和getName與空數據後,我的名稱字段爲空。確保mutator將空名稱設置爲空數據的空字符串。確保訪問者沒有錯誤地返回空值。它並沒有說我的setLicense和getLicense是否有問題,我有一種感覺,它也會有同樣的問題,因爲它們非常相似。我認爲這個問題以前沒有問過,但是抱歉。運行setName和getName後,爲什麼我的name字段爲空?

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); 
} 

}

回答

0

確保突變設置名稱上的空數據空字符串。

你在哪裏實現這個邏輯?

if (name == null) 
     { 
     name = String.Empty; 
     } 
相關問題