• Post Reply Bookmark Topic Watch Topic
  • New Topic

static print method that accepts an object  RSS feed

 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have recently finished an assignment that deals with inheritance and abstract classes. There is an optional part to assignment that im having trouble with. The optional part ask that I code a static print method that accepts a person object to print out all my data. (there is a Person class). I coded one but it doesn't work, it does not respond at all when i run my application.

This is my main method and print method, can anyone tell me why it wont print out? (i did add print code in my main method, this is currently how im getting my data to print.) The assignment calls for a static print method that accepts a person object. I need to be able to print either the data for customer or employee based on the users selection. additionally p.getDisplayText() is the inherited method, and Person is the super/parent class, which i did not post here for the sake of space. All i want to know is how to print this data in the separate print method below my main method.

 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't extended the person class.. so where is the inheritance?
and where is this print method called from??
print() only takes one Person object as you said. but here it takes two Person objects.
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i did not want to post all of my code, because it is rather lengthy. so my confusion is (my print method is obviously flawed) how to properly call on the person class to print the right data, i have never coded a static print method before. my only experience is printing from the main method (as i have done in this case. when i included this print method i did not get any data to print. what i want to print is em.getDisplayText(); or p.getDisplayText(), depending on if i need to print customer or employee data.

anyway this is all of the code:

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:what i want to print is em.getDisplayText(); or p.getDisplayText(), depending on if i need to print customer or employee data.

Well it would seem that your classes have already dealt with that by overriding getDisplayText() based on subtype. So all you should need is:
Winston
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i coded that and it's still not printing anything.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:i coded that and it's still not printing anything.

Can you show us an example of that code? Because so far I can't see any place that you actually call that method (mind you, there's an awful lot to go through, so I might have missed it).

And please don't repeat all the code you've already supplied. Simply give us an example of the call that isn't working.

Winston
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so, the program prompts the user to select customer or employee. if the user selects customer the user is prompted to enter name email, and customer number. if employee is selected the program asks for name, email and social security number.

so based on that selection the program prints out either (for customer ):




OR





so as you can see i can print in the main method but i need this data to print in the static print() method instead.

using this:



doesent do it, and i dont know why. do i need to declare my person objects in this method?
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw thank you for taking the time.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:using this:
...
doesent do it, and i dont know why. do i need to declare my person objects in this method?

No. I understand what you want to do; I just don't see any evidence that you're doing it.

For example, in your first code snippet, I would expect to see
CH08PR82App.print(c);
on line 14.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!