Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 电话及;比较多实例方法_Java_Class_Methods_Encapsulation_Instances - Fatal编程技术网

Java 电话及;比较多实例方法

Java 电话及;比较多实例方法,java,class,methods,encapsulation,instances,Java,Class,Methods,Encapsulation,Instances,我对编程非常陌生,这是我的第一门课程。我很难理解整个概念,希望我能得到一些关于如何继续的建议 基本上,我需要编写一个简单的地址簿程序,让用户输入2个(或更多?)用户的信息。我的课程援助给了我以下建议: 在我的应用程序(演示或测试)类的主方法中 -创建类AddressBook的两个或多个实例/对象 -使用实例(对象)调用类AddressBook的实例方法 -使用2个实例/对象来比较它们的名称 我目前有以下情况: AddressBook.java import java.util.Scanner;

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

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

在我的应用程序(演示或测试)类的主方法中 -创建类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
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()的两个实例时,我遇到了构造函数错误。如果我尝试添加一个参数'null',那么在调用方法后测试输出时,我只会得到一个返回'null'。我还在main中创建了compareName()方法,但是由于我甚至无法让第一部分正常工作,所以还没有调用它。此外,我还对如何将name1.firstName和name2.firstName的值发送回main感到有点困惑(或者我应该简单地将该值存储到另一个变量中,并在每次调用getFirstName()时创建一个增量循环?)。

在main类中,需要传入要存储在通讯簿中的值,即

AddressBook name1 = new AddressBook("firstName", "middleName", 
        "lastName", "homeAddress", "businessPhone",
        "homePhone", "cellPhone", "skypeId",
        "facebookId", "personalWebSite");
这将使用上述信息设置一个新的
name1
,类型为
AddressBook
。您还可以考虑创建类型<代码> AddiScript Boo/代码>的数组,它将存储<代码>地址簿< /代码>类的许多实例。

ArrayList<AddressBook> myAddressBook = new ArrayList();

这就是我使用这里给出的一些建议的结果。我将坚持只与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 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();
            //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
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

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

构造函数用于构造对象。您似乎没有传递任何有意义的内容来构造通讯簿。getter不应该获取任何用户输入——它们应该只返回对象中已有的内容。您的主要方法应该是使用扫描器执行所有操作,然后调用setter来适当地设置字段,甚至更好,将参数传递到
AddressBook
构造函数中不在
通讯簿
对象中设置
中间名
字段;它创建一个新变量,该变量恰好具有相同的名称。即使你想做什么,你也应该只写
middleName=sc.nextLine()
而不写
字符串。我想我理解你们的意思。我正在修改我的代码以解决set/get的不当使用。还感谢您让我知道,我只是在为每个字符串创建一个新实例;很明显现在看着它。。。谢谢,但是有人告诉我不要在作业中使用数组,因为后面的章节会对它进行回顾。老实说,我必须给我的助手发送一封电子邮件,以了解他们希望我在不使用数组的情况下如何做到这一点……然后你必须创建多个AddressBook对象。您可以使用扫描器获取一个数字,即要创建的联系人数量,然后使用循环在AddressBook类中创建必要数量的联系人,您可能希望拆分getter和setter<代码>公共字符串getFirstName(){return firstName;}public void setFirstName(){this.firstName=sc.nextLine();}