• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about using getter and setter method  RSS feed

 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to do a homework question and i tried to figure this out all day and give up. I need to have a class named AddressBook and the various field names which I have. Suppose to use appropriate data types to store the values for these fields in AddressBook objects. Use get and set methods to retrieve and assign values to these names. Should be able to use the get and set methods to compare a persons first middle and last name. This is the code I have so far. Everything after class AddressBookTest wont compile, but the code above it will. I am lost on this. Please help and sorry If I put my code in here wrong. I get errors saying

AddressBook1.java:101: error: cannot find symbol
                AddressBookTest addressBookTest1 = new firstName();
                                                       ^
  symbol:   class firstName
  location: class AddressBook1.AddressBookTest
AddressBook1.java:103: error: cannot find symbol
                AddressBookTest addressBookTest2 = new firstName();
                                                       ^
  symbol:   class firstName
  location: class AddressBook1.AddressBookTest
AddressBook1.java:105: error: cannot find symbol
                addressBookTest1.firstName("Alice");
                                ^
  symbol:   method firstName(String)
  location: variable addressBookTest1 of type AddressBook1.AddressBookTest
AddressBook1.java:107: error: cannot find symbol
                addressBookTest2.firstName("Jenny");
                                ^
  symbol:   method firstName(String)
  location: variable addressBookTest2 of type AddressBook1.AddressBookTest
AddressBook1.java:109: error: cannot find symbol
                System.out.println("name " + person1.getName());
                                             ^
  symbol:   variable person1
  location: class AddressBook1.AddressBookTest
AddressBook1.java:99: error: Illegal static declaration in inner class AddressBook1.AddressBookTest
        public static void main(String[] args) {
                           ^
  modifier 'static' is only allowed in constant variable declarations
6 errors

public class AddressBook1 {

private String firstName;
private String middleName;
private String lastName;
private String homeAddress;
    private String businessPhone;
    private String homePhone;
private String cellPhone;
private String skypeId;
private String facebookId;
private String personalWebSite;



public String getfirstName() {
return firstName;
}

public void setfirstName( String newfirstName ) {
firstName = newfirstName;
}

public String getmiddleName() {
return middleName;
}

public void setMiddleName( String newmiddleName ) {
middleName = newmiddleName;
}

public String getlastName() {
return lastName;
}

public void setlastName( String newlastName ) {
lastName = newlastName;
}

public String gethomeAddress() {
return homeAddress;
}

public void sethomeAddress( String newhomeAddress ) {
homeAddress = newhomeAddress;
}

public String getbusinessPhone() {
return businessPhone;
}

public void setbusinessPhone( String newbusinessPhone ) {
businessPhone = newbusinessPhone;
}

public String gethomePhone() {
return homePhone;
}

public void sethomePhone( String newhomePhone ) {
homePhone = newhomePhone;
}

public String getcellPhone() {
return cellPhone;
}

public void setcellPhone( String newcellPhone ) {
cellPhone = newcellPhone;
}

public String getskypeId() {
return skypeId;
}

public void setskypeId( String newskypeId ) {
skypeId = newskypeId;
}

public String getfacebookId() {
return facebookId;
}

public void setfacebookId( String newfacebookId ) {
facebookId = newfacebookId;
}

public String getpersonalWebSite() {
return personalWebSite;
}

public void setpersonalWebSite( String newpersonalWebSite ) {
personalWebSite = newpersonalWebSite;
}



}



class AddressBookTest {
public static void main(String[] args) {

AddressBookTest addressBookTest1 = new firstName();

AddressBookTest addressBookTest2 = new firstName();

addressBookTest1.firstName("Alice");

addressBookTest2.firstName("Jenny");

System.out.println("name " + person1.getName());

}
}
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AddressBook1.java:101: error: cannot find symbol
                AddressBookTest addressBookTest1 = new firstName();
                                                                              ^
  symbol:   class firstName

The compiler can not find a definition for the class named: firstName
Make sure there is a definition for that class that the compiler can find.

What is the purpose of the above statement? 


Please post code wrapped in code tags to preserve formatting and to make the code easier to read.  Use the Code button above the input  box and the Preview button below before pressing the Submit button.
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I make the definition? I want to be able to get information for a person by searching them.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I make the definition?

The following line from your code defines the class: AddressBook1



See the tutorial for how to define a class: http://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what I do with that sorry
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"firstName" is not a class, it is a field. You can't do a "new" of a field. It does have a setter and a getter. So, you can create an instance of AddressBook1 and then use the setter to set the value of a field.

Note that your setters and getters don't quite follow the naming convention. The first letter after "get" or "set" needs to be in upper case, e.g. setFirstName().

 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Gareau wrote:I don't understand what I do with that sorry


Do you mean you want to test AddressBook class in AddressBookTest class?
In AddressBookTest you should declare AddressBook instance to test.
The code below in your code:]


should replace by:



Staff note (Knute Snortum):

In the Beginning Java forum, please be careful not to post "complete solutions," that is, something the OP can copy and paste into their code. Snippets are okay. This code comes close to being a complete solution. Instead, try to guide the OP to come up with their own solution.

 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I tried adding that code and i got some errors.

C:\Users\Administrator\Desktop\Comp 268>javac AddressBook1.java
.\AddressBook.java:18: error: ';' expected
                String firstName
                                ^
.\AddressBook.java:19: error: ';' expected
                String middleName
                                 ^
.\AddressBook.java:20: error: ';' expected
                String lastName
                               ^
.\AddressBook.java:21: error: ';' expected
                String homeAddress
                                  ^
.\AddressBook.java:22: error: ';' expected
                String businessPhone
                                    ^
.\AddressBook.java:23: error: ';' expected
                String homePhone
                                ^
.\AddressBook.java:24: error: ';' expected
                String cellPhone
                                ^
.\AddressBook.java:25: error: ';' expected
                String skypeId
                              ^
.\AddressBook.java:26: error: ';' expected
                String facebookId
                                 ^
.\AddressBook.java:27: error: ';' expected
                String personalWebSite
                                      ^
.\AddressBook.java:111: error: class, interface, or enum expected
}
^
.\AddressBook.java:117: error: duplicate class: AddressBookTest
class AddressBookTest {
^
.\AddressBook.java:51: error: cannot find symbol
                lastname = newlastName;
                ^
  symbol:   variable lastname
  location: class AddressBook
.\AddressBook.java:55: error: cannot find symbol
                return newhomeAddress;
                       ^
  symbol:   variable newhomeAddress
  location: class AddressBook
.\AddressBook.java:63: error: cannot find symbol
                return newbusinessPhone;
                       ^
  symbol:   variable newbusinessPhone
  location: class AddressBook
.\AddressBook.java:71: error: cannot find symbol
                return newhomePhone;
                       ^
  symbol:   variable newhomePhone
  location: class AddressBook
.\AddressBook.java:79: error: cannot find symbol
                return newcellPhone;
                       ^
  symbol:   variable newcellPhone
  location: class AddressBook
.\AddressBook.java:87: error: cannot find symbol
                return newskypeId;
                       ^
  symbol:   variable newskypeId
  location: class AddressBook
.\AddressBook.java:95: error: cannot find symbol
                return newfacebookId;
                       ^
  symbol:   variable newfacebookId
  location: class AddressBook
.\AddressBook.java:103: error: cannot find symbol
                return newpersonalWebSite;
                       ^
  symbol:   variable newpersonalWebSite
  location: class AddressBook
20 errors
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have "AddressBook1" and "AddressBook", which is it?

Please repost the code.

Also looks like you may be missing a semi-colon.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.\AddressBook.java:18: error: ';' expected
                String firstName

Statements need to end with a ;
Try adding a ; at the end of all the statements where the compiler says:   ';' expected
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still confused.  Line 1 says "AddressBook1" and line 106 says "AddressBook", which is it? Have you created a class "AddressBook" somewhere else?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have a naming convention issue.

The setter should be named "setFirstName" with an upper case 'F'. The letter following "set" (or "get") must be in upper case.
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the correction that you said but Im still getting error messages

C:\Users\Administrator\Desktop\Comp 268>javac AddressBook1.java
AddressBook1.java:23: error: cannot find symbol
                return FirstName;
                       ^
  symbol:   variable FirstName
  location: class AddressBook1
.\AddressBook.java:18: error: ';' expected
                String firstName
                                ^
.\AddressBook.java:19: error: ';' expected
                String middleName
                                 ^
.\AddressBook.java:20: error: ';' expected
                String lastName
                               ^
.\AddressBook.java:21: error: ';' expected
                String homeAddress
                                  ^
.\AddressBook.java:22: error: ';' expected
                String businessPhone
                                    ^
.\AddressBook.java:23: error: ';' expected
                String homePhone
                                ^
.\AddressBook.java:24: error: ';' expected
                String cellPhone
                                ^
.\AddressBook.java:25: error: ';' expected
                String skypeId
                              ^
.\AddressBook.java:26: error: ';' expected
                String facebookId
                                 ^
.\AddressBook.java:27: error: ';' expected
                String personalWebSite
                                      ^
.\AddressBook.java:111: error: class, interface, or enum expected
}
^
.\AddressBook.java:117: error: duplicate class: AddressBookTest
class AddressBookTest {
^
AddressBook1.java:112: error: incompatible types
           AddressBook1 addressBookTest1 = new AddressBook();
                                           ^
  required: AddressBook1
  found:    AddressBook
AddressBook1.java:114: error: incompatible types
        AddressBook1 addressBookTest2 = new AddressBook();
                                        ^
  required: AddressBook1
  found:    AddressBook
AddressBook1.java:116: error: cannot find symbol
        addressBookTest1.setfirstName("Alice");
                        ^
  symbol:   method setfirstName(String)
  location: variable addressBookTest1 of type AddressBook1
AddressBook1.java:118: error: cannot find symbol
        addressBookTest2.setfirstName("Jenny");
                        ^
  symbol:   method setfirstName(String)
  location: variable addressBookTest2 of type AddressBook1
AddressBook1.java:120: error: cannot find symbol
        System.out.println("name " + addressBookTest1.getfirstName());
                                                     ^
  symbol:   method getfirstName()
  location: variable addressBookTest1 of type AddressBook1
.\AddressBook.java:51: error: cannot find symbol
                lastname = newlastName;
                ^
  symbol:   variable lastname
  location: class AddressBook
.\AddressBook.java:55: error: cannot find symbol
                return newhomeAddress;
                       ^
  symbol:   variable newhomeAddress
  location: class AddressBook
.\AddressBook.java:63: error: cannot find symbol
                return newbusinessPhone;
                       ^
  symbol:   variable newbusinessPhone
  location: class AddressBook
.\AddressBook.java:71: error: cannot find symbol
                return newhomePhone;
                       ^
  symbol:   variable newhomePhone
  location: class AddressBook
.\AddressBook.java:79: error: cannot find symbol
                return newcellPhone;
                       ^
  symbol:   variable newcellPhone
  location: class AddressBook
.\AddressBook.java:87: error: cannot find symbol
                return newskypeId;
                       ^
  symbol:   variable newskypeId
  location: class AddressBook
.\AddressBook.java:95: error: cannot find symbol
                return newfacebookId;
                       ^
  symbol:   variable newfacebookId
  location: class AddressBook
.\AddressBook.java:103: error: cannot find symbol
                return newpersonalWebSite;
                       ^
  symbol:   variable newpersonalWebSite
  location: class AddressBook
26 errors
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AddressBook1.java:23: error: cannot find symbol
                return FirstName;
                       ^
  symbol:   variable FirstName

The compiler can not find a definition for the variable: FirstName.  Make sure it is spelled correctly or that there is a definition with that name.


.\AddressBook.java:18: error: ';' expected
                String firstName

I thought this problem was posted earlier and fixed then???

Statements need to end with a ;
Try adding a ; at the end of all the statements where the compiler says:   ';' expected


Note:  When entering code, compile often so that you get fewer error messages.  Enter a few lines and then compile. Fix the errors and do it again.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I'm still confused.  Line 1 says "AddressBook1" and line 106 says "AddressBook", which is it? Have you created a class "AddressBook" somewhere else?

!!!

Delete all of your .class files and then recompile.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AddressBook1.java:23: error: cannot find symbol
                return FirstName;
                       ^
  symbol:   variable FirstName

I didn't say to rename the variable, only the method name.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Carey Brown wrote:I'm still confused.  Line 1 says "AddressBook1" and line 106 says "AddressBook", which is it? Have you created a class "AddressBook" somewhere else?

!!!

Delete all of your .class files and then recompile.


I highly suspect that you have two files in your directory: AddressBook1.java, and AddressBook.java.
You should only have one of these.
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured some things out now with your help thank you. I have a new problem though, I need to compare 2 names in my address book to see if they are the same or different. I wacthed a few videos on how to compare strings and I understand that concept. I am totally lost on how to compare strings using get and set and I tried for hours now. Please help I can't find anything online that explains this. Thank you very much.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now if instead of "first1" you substitute "addr1.getFirstName()" and instead of "first2" you use "addr2.getFirstName()", what do you get?
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I'm not sure what you mean.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the variable named "first1" from the equals() expression and replace it with "addr1.getFirstName()". I mean literally replace it. Character by character.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Gareau wrote:I figured some things out now with your help thank you. I have a new problem though, I need to compare 2 names in my address book to see if they are the same or different. I wacthed a few videos on how to compare strings and I understand that concept. I am totally lost on how to compare strings using get and set and I tried for hours now. Please help I can't find anything online that explains this. Thank you very much.


Assume that you have 2 object of AddressBook1

you set name for each object above

then you get name of two AddressBook1 objects and compare name of them by using equals() method like the code below:

Finally, you can print the result of comparing:

 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you that clarifies a lot !
I have 2 .java files for this program. One
Is AddressBook1.java and the other is AddressBookTest1.java , which file so I add that information into? One had the main and the other doesn't . Thank you.
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Gareau wrote:Thank you that clarifies a lot !
I have 2 .java files for this program. One
Is AddressBook1.java and the other is AddressBookTest1.java , which file so I add that information into? One had the main and the other doesn't . Thank you.


The AddressBookTest1.java file, because it contains main() method which is start your program
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i tried what you said, I must of done something wrong I got these errors.





 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Gareau wrote:Hello, i tried what you said, I must of done something wrong I got these errors.







You should try it

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post the code for AddressBook1.java?  This error message...

...is telling us that the class AddressBook1 has a constructor with 10 parameters.  But the code you posted doesn't indicate this.  Try compiling AddressBook1.java first, then AddressBookTest1.java.
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the file AddressBook1.java


this is AddressBookTest1.java




error messages are




 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With error messages it's always best to start at the first one, fix that, then work your way down.

Line 2 is showing you that you tried to invoke the constructor AddressBook1() with no arguments. This conflicts with the constructor you made in the class AddressBook1 which takes 10 String arguments. So, you have to pass in 10 Strings in order for the call to the constructor to compile.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly the code that would benefit from AddressBook1 having a toString() method.
Once you have that then in your main() method you can print the state of the object using a single line, like:

PLEASE start variable names with a lower case letter.
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






Thank you very much for all your help so  far, everything is working good minus this one error? Do you know what is causing this???
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AddressBookTest1.java:12: error: cannot find symbol
        System.out.println( Contact1);
                                    ^
  symbol:   variable Contact1

The compiler can not find a definition for Contact1.  Where is that variable defined?

Note: variables need to be defined BEFORE they are used.  Move line 12 to after line 19
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much everyone for helping me. The program complies and works now, but I am unable to get a specific field to printout, it just displays the whole contact info.

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Gareau wrote:Thank you so much everyone for helping me. The program complies and works now, but I am unable to get a specific field to printout, it just displays the whole contact info.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When calling a method, you always need () on the end of the identifier, even if there are no arguments.

The same is true if you are calling a method on an object reference.
 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all so much everything is working very well, I appreciate all the help. I learned a few new things from this experience. Thanks again.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!