私はプログラムに取り組んでおり、すべてが機能するようになりましたが、教授が私たちのプログラムをテストするために私たちに与えるテストの一部に合格できません。null データで setName と getName を実行した後、名前フィールドが null であることがわかります。mutator が null データの名前を空の文字列に設定していることを確認してください。アクセサーが誤って null 値を返していないことを確認してください。私の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);
}
}