• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Contact List Program

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to programming and object oriented design. This is my last requirement to finish my bachelors degree (not in programming). I am a bit confused with how to make object oriented work, and nothing I look at seems to help. The assignment is to create a contact list that uses inheritance, polymorphism,and collections. I need a contact list that stores two types of contacts: business and personal. I need prompt 1 to add a contact and then ask 1 for personal or 2 for business. Prompt 2 will allow user to display the output of a chosen contact and prompt 3 will quit.

I have the following class and subclasses built. I am pretty sure the classes are built right but after adding either type of contact, when I choose 2 in order to view I can't see anything it just returns me [contactlist.PersonalContact@55f96302]. Any help would be awesome, I just need to get through this, then I will gladly leave programming to those that know what they are doing.

I apologize for the long post but thought I should show everything.

Here is my main:



Contact Class:



Business Contact Subclass:



Personal Contact Subclass:

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Davis wrote:... it just returns me [contactlist.PersonalContact@55f96302].


That's the kind of output you get when you don't override the toString() method.

Your class PersonalContact does not have a toString() method. Java doesn't know automatically how you want a contact to be converted to a string. You should override the toString() method in class PersonalContact.
 
Tim Davis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I put my overrides in as follows:

Personal Subclass:



Business Subclass:



I am still throwing the same message. This is all I need to override correct?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you write something like this:



...Java automatically looks for a method called toString(). If you don't override it, it will fall back to Object's toString() method, which is what you're seeing. Add this:



..to your two Contact subclasses.
 
Tim Davis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is very basic for you guys here but it I just don't seem to be grasping it. I have change my subclass code as follows:

When I do this it tells me a return statement is needed. Isn't that what I am doing by saying System.out.println(this.dateOfBirth)? If I include the line return null:

Now I get the following on my output:
[1] Add contact
[2] View all contacts
[3] Quit
Choose : 1
Please enter Specify the contact type (1) Personal or (2) Business:
1
First Name: John
Last Name: Doe
Address: 123 Anywhere St. Anywhere, AL 12345
E-mail address: jdoe@live.com
Phone number: 123-456-7890
Date of Birth (MM/DD/YYYY): 01/01/1980

[1] Add contact
[2] View all contacts
[3] Quit
Choose : 2
John
Doe
123 Anywhere St. Anywhere, AL 12345
jdoe@live.com
123-456-7890
01/01/1980
[null]

[1] Add contact
[2] View all contacts
[3] Quit
Choose : 3
BUILD SUCCESSFUL (total time: 1 minute 12 seconds)

It allows me to put in the information but then when I choose 2 to view it returns the info but includes [null] at the end.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toString() needs to return a String formatted the way you'd like to see it. Do not put print()'s inside of the toString() method.
 
Tim Davis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when I add a business contact and go to view it I want to see everything from the main Contact class as well as jobTitle and organization from the business contact subclass. Then when I write my code as follows:


I can return a single value such as jobTitle and will get it to display properly but when I try return String(BusinessContact) it tells me can not find symbol.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Davis wrote:So when I add a business contact and go to view it I want to see everything from the main Contact class as well as jobTitle and organization from the business contact subclass. Then when I write my code as follows:


I can return a single value such as jobTitle and will get it to display properly but when I try return String(BusinessContact) it tells me can not find symbol.


Don't call view Contacts as this calls println() which I said you don't want to do in toString().
More like

And you should use getFirstName() and not getfirstName(), the 'F' should be capitalized.
 
Tim Davis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That option definitely works but I was looking to have it output in a list not a line across.

Right now I am getting first name = John, last name = doe, etc....

I was looking for

first name = John
last name = Doe
etc....

That is why I was using System.out.println(this.firstName), etc... ao I could list it out.

How can I do this using return?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Davis wrote:That option definitely works but I was looking to have it output in a list not a line across.

Right now I am getting first name = John, last name = doe, etc....

I was looking for

first name = John
last name = Doe
etc....

That is why I was using System.out.println(this.firstName), etc... ao I could list it out.

How can I do this using return?


Simply add some new-line characters into the mix where you want line breaks.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which case you should not simply add the line separator characters. Try this sort of thing:-
return String.format("First name: %s%nLast name: %s", firstName, lastName);
You can read about the % tags here (comprehensive but not easy) here and here (only simple examples but easier to read).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic