I'm very new to programming and this is my first course. I'm having a very hard time understanding the whole concept and hope I could get some tips on how to proceed.
Essentially I need to write a simple Address Book program which lets the user input the information of 2 (or more?) users. I was given the following advice from my course aid:
In the method main of my application (demo or test) class - create two or more instances/objects of class AddressBook - use the instances (objects) to call instance methods of class AddressBook - use 2 instances/objects to compare their names
I currently have the following:
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
As you can see I'm getting constructor errors when trying to initialize the two instances of class AddressBook(). If I try to add an arg of 'null' I simply get a return of 'null' when I test the output after invoking the methods. I also created the compareName() method in the main however haven't called it yet since I can't even get the first part to work. Additionally I'm a bit confused how I'm supposed to get the values of name1.firstName and name2.firstName sent back to main (or should I simply store the value into another variable and create an incremental loop each time getFirstName() is called?).