我的程序看起来像这样 -
package sysutilities;
public class Name {
// Declaring Instance Variables/////////////////////////////////////////////
String first;
String middle;
String last;
String nick;
char sep;
private static int nameObjsCount = 0;
// Verifying the validity of a separator////////////////////////////////////
private boolean validSeparator(char a) {
if (a == ',' || a == '-' || a == '#') {
return true;
}
else {
return false;
}
}
// Name constructor used to assign certain values to the instance variables/
public Name() {
this.first = "NOFIRSTNAME";
this.middle = "NOMIDDLENAME";
this.last = "NOLASTNAME";
this.sep = '#';
nameObjsCount++;
}
// Name constructor with two parameters used to assign first and last name//
public Name(String first, String last) {
this.first = first;
this.last = last;
this.middle = "";
this.sep = ',';
nameObjsCount++;
}
// Name constructor used to assign first, middle and last name//////////////
public Name(String first, String middle, String last) {
this.first = first;
this.middle = middle;
this.last = last;
this.sep = ',';
nameObjsCount++;
}
// Name constructor used to assign all values///////////////////////////////
public Name(String first, String middle, String last, char sep) {
this.first = first;
this.middle = middle;
this.last = last;
// If invalid separator, the separator defaults to comma////////////////
boolean result = this.validSeparator(sep);
if (result == false) {
this.sep = ',';
}
else {
this.sep = sep;
}
nameObjsCount++;
}
// Get and Set methods//////////////////////////////////////////////////////
public String getFirstname() {
return first;
}
public String getMiddlename() {
return middle;
}
public String getLastname() {
return last;
}
public void setNickname(String nick) {
this.nick = nick;
}
public String getNickname(String nick) {
if (nick == null) {
nick = "";
}
return nick;
}
public void setSeparator(char sep) {
Name data = new Name();
boolean result = data.validSeparator(sep);
if (result == true) {
this.sep = sep;
}
}
public char getSeparator(char sep) {
return sep;
}
public boolean equals(Name input) {
Name equation = new Name();
equation.first = this.first;
equation.middle = this.middle;
equation.last = this.last;
boolean firstTest = (equation.first).equals(input.first);
boolean middleTest = (equation.middle).equals(input.middle);
boolean lastTest = (equation.last).equals(input.last);
if (firstTest == true && middleTest == true && lastTest == true) {
return true;
}
else {
return false;
}
}
public String toString() {
String output;
if (nick != null) {
if (this.middle.equals("")) {
output = last + sep + first + "(" + nick + ")";
}
else {
output = last + sep + first + sep + middle + "(" + nick + ")";
}
}
else {
if (this.middle.equals("")) {
output = last + sep + first;
}
else {
output = last + sep + first + sep + middle;
}
}
return output;
}
// Check validity
public int compareTo(Name y) {
int firstComp = this.first.compareTo(y.first);
int middleComp = this.middle.compareTo(y.middle);
int lastComp = this.last.compareTo(y.last);
if (lastComp >= 0) {
return 1;
}
else if (lastComp <= 0) {
return -1;
}
else {
if (firstComp >= 0) {
return 1;
}
else if (firstComp <= 0) {
return -1;
}
else {
if (middleComp >= 0) {
return 1;
}
else if (middleComp <= 0) {
return -1;
}
else {
return 0;
}
}
}
}
public static int getNumberOfNameObjects() {
return nameObjsCount;
}
// NORMALIZE//// Check middle
public static Name normalize(Name x, boolean test) {
Name norm = x;
norm.nick = null;
if (test) {
norm.first = norm.first.toUpperCase();
norm.middle = norm.middle.toUpperCase();
norm.last = norm.last.toUpperCase();
}
else {
norm.first = norm.first.toLowerCase();
norm.middle = norm.middle.toLowerCase();
norm.last = norm.last.toLowerCase();
}
norm.sep = ',';
return norm;
}
}
正在运行的程序是这个 -
package tests;
import static org.junit.Assert.*;
import org.junit.Test;
import sysutilities.Name;
public class PublicTests {
/* We use this string to prevent any hardcoding of results. */
/* The submit server uses a different value for TESTS_TAG */
public static final String TESTS_TAG = "\nEndTest";
@Test
public void test1() {
String answer = "";
Name name1 = new Name("Claudia", "I.", "Smith");
answer += name1 + "\n";
Name name2 = new Name("Rachel", "I.", "Green", '#');
answer += name2 + "\n";
Name name3 = new Name("Joseph", "K.", "Falk");
name3.setNickname("Joe");
answer += name3 + "\n";
Name name4 = new Name();
answer += name4 + "\n";
answer += "Same: " + name1.equals(name2) + "\n";
answer += "Number of objects: " + Name.getNumberOfNameObjects();
answer += TESTS_TAG;
assertTrue(TestsSupport.isCorrect("pubTest1.txt", answer));
}
}
我的问题是,根据正在运行的程序,我的计数器显然是读取5,它应该只读4。 我不知道为什么会这样。提前谢谢。
答案 0 :(得分:0)
如果没有必要,您的某些其他方法似乎正在运行nameObjsCount++;
。请注意,创建临时Name
对象也会增加计数器。尝试更改这些方法:
public void setSeparator(char sep) {
//Name data = new Name(); //remove implicit nameObjsCount++
boolean result = this.validSeparator(sep); //see below
if (result == true) {
this.sep = sep;
}
}
public boolean equals(Name input) {
// Name equation = new Name(); //remove implicit nameObjsCount++
// equation.first = this.first; //see below
// equation.middle = this.middle;
// equation.last = this.last;
boolean firstTest = (this.first).equals(input.first);
boolean middleTest = (this.middle).equals(input.middle);
boolean lastTest = (this.last).equals(input.last);
if (firstTest && middleTest && lastTest) {
return true;
}
else {
return false;
}
}
请注意,实际上没有理由创建这些临时Name
,因为您只能将它们用于this
实例可用的数据和方法。你也可以使你的validSeparator()
方法保持静态,因为它做了一些通用的东西,但这是一个样式问题。
答案 1 :(得分:0)
您正在Name
和equals
方法中创建setSeparator
的其他实例。
您应该将这些方法更改为更像
的方法public void setSeparator(char sep) {
if (validSeparator(sep)) {
this.sep = sep;
}
}
和
public boolean equals(Name input) {
//Name equation = new Name();
boolean firstTest = first.equals(input.first);
boolean middleTest = middle.equals(input.middle);
boolean lastTest = last.equals(input.last);
return (firstTest && middleTest && lastTest);
}