Forums Register Login

Not getting the output expected

+Pie Number of slices to send: Send
When I run my program and choose option 2 to get contact details I am expecting it to display as follows (First Name, Last Name):

Contacts how have been entered:
0) John Doe
1) George Smith
2) Nancy Davis
Please enter the number corresponding to the contact you would like to view:

Instead for a personal contact it is displaying as follows:
Contacts who have been entered:
0) Doe 1 F St. (last name, address)
Please enter the number corresponding to the contact you would like to view:

Instead for a business contact it is displaying as follows:
Contacts who have been entered:
0) 1 F St. jd@gmail.com (address, email)
Please enter the number corresponding to the contact you would like to view:

Then when I enter the number to display the contact for personal it is returning me only first name and business is only returning me first and last name. It should be returning the full contact info that was put in during the add contact step. I thought I programmed everything properly but it isn't displaying what I want to see. Any help would be greatly appreciated. My code is listed below.

ContactList



Contact



Personal



Business

+Pie Number of slices to send: Send
Unlike constructors, methods do not have an implicit call to the superclass method that was overriden. You have to explicitly call them with super.whatever(). In this case, you have to call super.toString() and use the returned value along with whatever else you want to tack on to it. If you don't call super.toString, then only what you wrote is what you're going to get.

BTW, I'm not implying that constructors are overridden, because they're not. The point is that you have to explicitly call the overridden super method if you want to execute the code there, otherwise, only the overriding code gets executed.
+Pie Number of slices to send: Send
Maybe I am missing something but you are saying in my Personal and Business subclasses I need to add to my return statement super.toStirng() along with any additional info I would like to see?
+Pie Number of slices to send: Send
Tim ,
I am curious to know few things on your code :
1) Why your have defined the class Contact as abstract , (still there is no abstract method !!!)
2)Why you override the toString() of Business and Personal like this :




why didnt you take other inherited properties here ? like firstName , lastName etc ..

and finally the last question :
3)why you have defined all properties (in Contact ,Personal and Business) as String ? Probably this is the most important question .Why am I asking this because you can find the solution of your problem in here.
You have done silly mistakes of calling constructor in Personal and Business class .It is showing you are passing different values from ContactList class to the respective constructor. Change constructor of Personal and Business accordingly , you can get the desire result .

Hope you get the point ..
Satya
+Pie Number of slices to send: Send
 

S Majumder wrote:
You have done silly mistakes of calling constructor in Personal and Business class .It is showing you are passing different values from ContactList class to the respective constructor. Change constructor of Personal and Business accordingly , you can get the desire result


To clarify this, check the parameters you are passing to your constructors; Java doesn't care about the names, it only cares about the positions and types. That is, the first value passed will be assigned to the first parameter declared and so on. The names are for your benefit, so you know what the value you are passing means or what it's for.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 649 times.
Similar Threads
Runtime Error on View Contacts
Contact List Program
ArrayList Value
Printing values of an array list
Help printing output for Contact Manager assignment
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:21:13.