私は学校の課題の一部としてアドレス帳をやっていますが、私はそれを大部分は理解していますが、私は一部に固執しています。ここに私のコードがあります。私は変数を含むAddressBookクラスを持っていて、本の中の各人物の情報を変更/返すための適切なgetterメソッドとsetterメソッドを持っています。次に、私は、本の作業に取り組む方法を示すメソッドmainを持つTestAddressBookクラスを持っています。私が立ち往生している唯一のことは、最終的な要件です。ここでは、2つの名前を比較し、それらが等しいかどうかを確認するよう求められます。ここにクラスのコードがあります。アドレス帳プログラムの名前の比較
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を使用して設定メソッドを使用して、比較メソッドcompareNames(name1、name2)を作成します。これは、strの最初、中、最後の名前ings name1とname2です。 name1とname2は、 "FirstName M. LastName"の形式に従うものとします。 "
この割り当てでは、2つの文字列を比較し、" Robert J. SmithとBruce A. Carterの行に沿って何かを返す必要があります。
追加されました。 Java。ごめんなさい! – ripp2k
なぜ学校は駄目なのですか*比較メソッドの作成compareNames(name1、name2)*このComparatorのインターフェースは、メソッド参照やComparator.comparingを使って、人々に間違ったやり方を教えるのをやめます... https: //docs.oracle.com/javase/8/docs/api/java/util/Comparator.html – xenoterracide
結合方法については、9 http://www.baeldung.com/java-8-sort-lambdaを参照してください。最初に文字列を連結して比較することができます。コンパレータは030を返します。 – xenoterracide