我正在做一个地址簿作为学校作业的一部分,我主要是想通了一下,但我被困在一个部分。这是我的代码第一。我有一个AddressBook类,它包含变量和适当的getter和setter方法来更改/返回书中每个人的信息。然后我有一个TestAddressBook类和一个方法main来演示如何处理书籍的作品。我坚持的唯一的事情就是最终的要求,我被要求比较两个名字,看看它们是否相等。这是类的代码。比较地址簿程序中的名字
public class AddressBook {
private String firstName;
private String middleName;
private String lastName;
private String homeAddress;
private String homePhone;
private String cellPhone;
private String businessPhone;
private String skypeId;
private String facebookId;
private String personalWebSite;
public AddressBook(String firstName, String middleName, String lastName,
String homeAddress, String homePhone, String cellPhone,
String businessPhone, String skypeID, String facebookID,
String personalWebSite) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.homePhone = homePhone;
this.cellPhone = cellPhone;
this.businessPhone = businessPhone;
this.skypeId = skypeID;
this.facebookId = facebookID;
this.personalWebSite = personalWebSite;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public String getBusinessPhone() {
return businessPhone;
}
public void setBusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}
public String getSkypeId() {
return skypeId;
}
public void setSkypeId(String skypeId) {
this.skypeId = skypeId;
}
public String getFacebookId() {
return facebookId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
public String getPersonalWebSite() {
return personalWebSite;
}
public void setPersonalWebSite(String personalWebSite) {
this.personalWebSite = personalWebSite;
}
public static String compareNames(String name1, String name2) {
}
}
然后我的测试类...
public class TestAddressBook {
public static void main(String[] args) {
System.out.println("ENTRY 1");
AddressBook name1 = new AddressBook("Robert", "James", "Smith", "3 Fake St",
"222-321-8371", "222-423-2382",
"222-438-2918", "bob483", "bobfb493",
"http://www.freewebhost.com/bob848");
System.out.println("First Name: " + name1.getFirstName());
System.out.println("Middle Name: " + name1.getMiddleName());
System.out.println("Last Name: " + name1.getLastName());
System.out.println("Address: " + name1.getHomeAddress());
System.out.println("Home Phone: " + name1.getHomePhone());
System.out.println("Cell Phone: " + name1.getCellPhone());
System.out.println("Business Phone: " + name1.getBusinessPhone());
System.out.println("Skype ID: " + name1.getSkypeId());
System.out.println("Facebook ID: " + name1.getFacebookId());
System.out.println("Personal Website: " + name1.getPersonalWebSite());
System.out.println("\nENTRY 2");
AddressBook name2 = new AddressBook("Bruce", "Allan", "Carter", "56 Outtamy Way",
"564-342-8372", "564-283-9832",
"564-293-3489", "brucie392", "brucieface28",
"http://www.freewebhost.com/carteristheman");
System.out.println("First Name: " + name2.getFirstName());
System.out.println("Middle Name: " + name2.getMiddleName());
System.out.println("Last Name: " + name2.getLastName());
System.out.println("Address: " + name2.getHomeAddress());
System.out.println("Home Phone: " + name2.getHomePhone());
System.out.println("Cell Phone: " + name2.getCellPhone());
System.out.println("Business Phone: " + name2.getBusinessPhone());
System.out.println("Skype ID: " + name2.getSkypeId());
System.out.println("Facebook ID: " + name2.getFacebookId());
System.out.println("Personal Website: " + name2.getPersonalWebSite());
System.out.println("\nENTRY 3");
AddressBook name3 = new AddressBook("Susan", "Anne", "Peters", "6 Madeup Blvd",
"736-453-1238", "736-392-2385",
"736-926-2439", "anniep", "susananne",
"http://www.freewebhost.com/Susanspage");
System.out.println("First Name: " + name3.getFirstName());
System.out.println("Middle Name: " + name3.getMiddleName());
System.out.println("Last Name: " + name3.getLastName());
System.out.println("Address: " + name3.getHomeAddress());
System.out.println("Home Phone: " + name3.getHomePhone());
System.out.println("Cell Phone: " + name3.getCellPhone());
System.out.println("Business Phone: " + name3.getBusinessPhone());
System.out.println("Skype ID: " + name3.getSkypeId());
System.out.println("Facebook ID: " + name3.getFacebookId());
System.out.println("Personal Website: " + name3.getPersonalWebSite());
}
}
分配是要求这个...
“使用get和set方法,创建一个比较方法compareNames(name1,name2),它比较str的第一个,中间和最后一个名字名字1和名字2。假定名称1和名称2遵循以下格式:“名字M.姓氏”。“
该任务要求将两个字符串进行比较,并按照”罗伯特·J·史密斯和布鲁斯·A·卡特“等同的名字。“任何人都可以帮忙吗?
添加它。 Java的。抱歉! – ripp2k
为什么学校是废话*创建一个比较方法compareNames(name1,name2)*有一个接口为这个'比较器'停止教人们如何做错事的方式......用方法引用和Comparator.comparing ... https: //docs.oracle.com/javase/8/docs/api/java/util/Comparator.html – xenoterracide
请参阅9 http://www.baeldung.com/java-8-sort-lambda以获取组合方式,或你可以先连接字符串,然后比较那个...比较器将返回0,等于 – xenoterracide