【问题标题】:Calling & comparing multiple instance methods调用和比较多个实例方法
【发布时间】:2016-05-31 09:04:30
【问题描述】:

我对编程很陌生,这是我的第一门课程。我很难理解整个概念,希望我能得到一些关于如何继续的提示。

基本上我需要编写一个简单的地址簿程序,让用户输入 2 个(或更多?)用户的信息。我的课程援助给了我以下建议:

在我的应用程序(演示或测试)类的 main 方法中 - 创建两个或多个 AddressBook 类的实例/对象 - 使用实例(对象)调用类AddressBook的实例方法 - 使用 2 个实例/对象来比较它们的名称

我目前有以下:

AddressBook.java

import java.util.Scanner;

public class AddressBook {

    Scanner sc = new Scanner(System.in);

    //Declare method variables and initial value (empty)
    String firstName = "";
    String middleName = "";
    String lastName = "";
    String homeAddress = "";
    String businessPhone = "";
    String homePhone = "";
    String cellPhone = "";
    String skypeId = "";
    String facebookId = "";
    String personalWebSite = "";

    public AddressBook(String firstName, String middleName, 
            String lastName, String homeAddress, String businessPhone,
            String homePhone, String cellPhone, String skypeId,
            String facebookId, String personalWebSite) {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.homeAddress = homeAddress;
        this.businessPhone = businessPhone;
        this.homePhone = homePhone;
        this.cellPhone= cellPhone;
        this.skypeId = skypeId;
        this.facebookId = facebookId;
        this.personalWebSite = personalWebSite;
    }

    //Setters
    void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    void setLastName(String lastName){
        this.lastName = lastName;
    }

    void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    void setBusinessPhone(String businessPhone) {
        this.businessPhone = businessPhone;
    }

    void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
    }

    void setCellPhone(String cellPhone) {
        this.cellPhone = cellPhone;
    }

    void setSkypeId(String skypeId) {
        this.skypeId = skypeId;
    }

    void setFacebookId(String facebookId) {
        this.facebookId = facebookId;
    }

    void setPersonalWebSite(String personalWebSite) {
        this.personalWebSite = personalWebSite;
    }


    //Getters
    public String getFirstName() {
        System.out.print("First name: ");
        String firstName = sc.nextLine();
        return firstName;
    }

    public String getMiddleName() {
        System.out.print("Middle name: ");
        String middleName = sc.nextLine();
        return middleName;
    }

    public String getLastName() {
        System.out.print("Last name: ");
        String lastName = sc.nextLine();
        return lastName;
    }

    public String getHomeAddress() {
        System.out.print("Home address: ");
        String homeAddress = sc.nextLine();
        return homeAddress;
    }

    public String getBusinessPhone() {
        System.out.print("Business phone number: ");
        String businessPhone = sc.nextLine();
        return businessPhone;
    }

    public String getHomePhone() {
        System.out.print("Home phone number: ");
        String homePhone = sc.nextLine();
        return homePhone;
    }

    public String getCellPhone() {
        System.out.print("Cell phone number: ");
        String cellPhone = sc.nextLine();
        return cellPhone;
    }

    public String getSkypeId() {
        System.out.print("Skype ID: ");
        String skypeId = sc.nextLine();
        return skypeId;
    }

    public String getFacebookId() {
        System.out.print("Facebook ID: ");
        String facebookId = sc.nextLine();
        return facebookId;
    }

    public String getPersonalWebSite() {
        System.out.print("Personal Website: ");
        String personalWebSite = sc.nextLine();
        return personalWebSite;
    }

    public String compareNames() {
            String comp1 = name1.getFirstName;
            String comp2 = name2.getFirstName;

            if (comp1.equalsIgnoreCase(comp2)) {
                System.out.println("The names match!");
            } else {
                System.out.println("The names don't match!");
            }
    }


} // end of class AddressBook

AddressBookDemo.java

public class AddressBookDemo {

    public static void main(String[] args) {

        // Create two AddreeBook objects
        AddressBook name1 = new AddressBook();
        AddressBook name2 = new AddressBook();

        // Invoke methods on those objects
        System.out.println("Enter the first person's info");
        name1.getFirstName();
            //Testing input/output
            System.out.println(name1.firstName);
//      name1.getMiddleName();
//      name1.getLastName();
//      name1.getHomeAddress();
//      name1.getBusinessPhone();
//      name1.getHomePhone();
//      name1.getCellPhone();
//      name1.getSkypeId();
//      name1.getFacebookId();
//      name1.getPersonalWebSite();

        System.out.println("Enter the second person's info");
        name2.getFirstName();
            //Testing input/output
            System.out.println(name2.firstName);
//      name2.getMiddleName();
//      name2.getLastName();
//      name2.getHomeAddress();
//      name2.getBusinessPhone();
//      name2.getHomePhone();
//      name2.getCellPhone();
//      name2.getSkypeId();
//      name2.getFacebookId();
//      name2.getPersonalWebSite();


    } // end of Main

} // End of class AddressBookDemo

如您所见,在尝试初始化类 AddressBook() 的两个实例时,我遇到了构造函数错误。如果我尝试添加一个 'null' 的 arg,当我在调用方法后测试输出时,我只会得到一个返回 'null'。我还在 main 中创建了 compareName() 方法,但还没有调用它,因为我什至无法让第一部分工作。此外,我有点困惑我应该如何将 name1.firstName 和 name2.firstName 的值发送回 main (或者我应该简单地将值存储到另一个变量中并在每次 getFirstName() 时创建一个增量循环叫什么?)。

【问题讨论】:

  • 构造函数用于构造对象。您似乎没有传递任何有意义的东西来构建地址簿。
  • Getter 不应该获得任何用户输入——他们应该只返回对象中已经存在的内容。您的主要方法应该使用扫描仪完成所有操作,然后调用设置器以适当地设置字段,或者更好的是,将参数传递给 AddressBook 构造函数。
  • 最后,String middleName = sc.nextLine();notAddressBook 对象中设置middleName 字段;它创建了一个恰好具有相同名称的新变量。即使对于你想要做的事情,你也应该只写middleName = sc.nextLine() 而不要写String
  • 我想我明白你们的意思了。我正在修改我的代码以解决 set/get 的不当使用。还要感谢您让我知道我只是在为每个字符串创建一个新实例;现在看起来很明显......叹息

标签: java class methods encapsulation instances


【解决方案1】:

在您的主类中,您需要传入要存储在地址簿中的值,即

AddressBook name1 = new AddressBook("firstName", "middleName", 
        "lastName", "homeAddress", "businessPhone",
        "homePhone", "cellPhone", "skypeId",
        "facebookId", "personalWebSite");

这将使用上述信息设置AddressBook 类型的新name1。您可能还想考虑创建一个AddressBook 类型的ArrayList,它将存储AddressBook 类的许多实例。

ArrayList<AddressBook> myAddressBook = new ArrayList();

然后你需要在 ArrayList 中添加一个对象:

myAddressBook.add(new AddressBook(("firstName", "middleName", 
        "lastName", "homeAddress", "businessPhone",
        "homePhone", "cellPhone", "skypeId",
        "facebookId", "personalWebSite"));

【讨论】:

  • 谢谢,但我被告知要避免使用数组来分配,因为它在后面的章节中进行了审查。老实说,我不得不向我的帮助发送一封电子邮件,以了解他们希望我如何在不使用数组的情况下做到这一点......
  • 那么您将不得不创建多个地址簿对象。您可以使用扫描仪获取一个数字,即要创建的联系人数量,然后使用循环创建必要数量的联系人
【解决方案2】:

这就是我最终使用这里给出的一些建议的结果。我将只保留 2 个联系人,因为这是任务要求的;

地址簿.java

import java.util.Scanner;

public class AddressBook {

    Scanner sc = new Scanner(System.in);

    //Declare method variables and initial value (empty)
    String firstName = "";
    String middleName = "";
    String lastName = "";
    String homeAddress = "";
    String businessPhone = "";
    String homePhone = "";
    String cellPhone = "";
    String skypeId = "";
    String facebookId = "";
    String personalWebSite = "";

    public String getFirstName() {
        System.out.print("First name: ");
        firstName = sc.nextLine();
        return firstName;
    }

    public String getMiddleName() {
        System.out.print("Middle name: ");
        middleName = sc.nextLine();
        return middleName;
    }

    public String getLastName() {
        System.out.print("Last name: ");
        lastName = sc.nextLine();
        return lastName;
    }

    public String getHomeAddress() {
        System.out.print("Home address: ");
        homeAddress = sc.nextLine();
        return homeAddress;
    }

    public String getBusinessPhone() {
        System.out.print("Business phone number: ");
        businessPhone = sc.nextLine();
        return businessPhone;
    }

    public String getHomePhone() {
        System.out.print("Home phone number: ");
        homePhone = sc.nextLine();
        return homePhone;
    }

    public String getCellPhone() {
        System.out.print("Cell phone number: ");
        cellPhone = sc.nextLine();
        return cellPhone;
    }

    public String getSkypeId() {
        System.out.print("Skype ID: ");
        skypeId = sc.nextLine();
        return skypeId;
    }

    public String getFacebookId() {
        System.out.print("Facebook ID: ");
        facebookId = sc.nextLine();
        return facebookId;
    }

    public String getPersonalWebSite() {
        System.out.print("Personal Website: ");
        personalWebSite = sc.nextLine();
        return personalWebSite;
    }

} // end of class AddressBook

AddressBookDemo.java

public class AddressBookDemo {

    public static void main(String[] args) {

        // Create two AddreeBook objects
        AddressBook name1 = new AddressBook();
        AddressBook name2 = new AddressBook();

        // Invoke methods on those objects
        System.out.println("Enter the first person's info");
        name1.getFirstName();
        name1.getMiddleName();
        name1.getLastName();
        name1.getHomeAddress();
        name1.getBusinessPhone();
        name1.getHomePhone();
        name1.getCellPhone();
        name1.getSkypeId();
        name1.getFacebookId();
        name1.getPersonalWebSite();

        System.out.println("Enter the second person's info");
        name2.getFirstName();
        name2.getMiddleName();
        name2.getLastName();
        name2.getHomeAddress();
        name2.getBusinessPhone();
        name2.getHomePhone();
        name2.getCellPhone();
        name2.getSkypeId();
        name2.getFacebookId();
        name2.getPersonalWebSite();

        // Compare names 
        String comp1 = name1.firstName + " " + name1.middleName.toUpperCase().charAt(0) + ". " + name1.lastName;
        String comp2 = name2.firstName + " " + name2.middleName.toUpperCase().charAt(0) + ". " + name2.lastName;
        if (comp1.equalsIgnoreCase(comp2)) {
            System.out.println("The names match!");
        } else {
            System.out.println("The names don't match!");
        } // end if/else statement

    } // end of Main

} // End of class AddressBookDemo

感谢大家的帮助!非常感谢。

【讨论】:

  • 在您的 AddressBook 类中,您可能希望拆分 getter 和 setter。 public String getFirstName() { return firstName; } public void setFirstName(){ this.firstName = sc.nextLine(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 2022-01-18
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多