aspose file tools*
The moose likes Beginning Java and the fly likes Printing memory address instead of string value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing memory address instead of string value." Watch "Printing memory address instead of string value." New topic
Author

Printing memory address instead of string value.

Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Ok, so I have a project i'm working on for school and i'm stumped at a particular point in this project. I think it may have to do with my parent class called "Student" and it being an abstract class, however i'm not sure. What is happening, is my program is displaying the following output; which appears to be the memory address of where the string is located:

init:
deps-jar:
compile:
run:
GradStudent@e48e1b
PhdStudent@1ad086a
BUILD SUCCESSFUL (total time: 0 seconds)
-------------------------------------------------------------------
here is my assignment:

Description - Create your own exception class and throw it based on the ID number in your Student class. Also create a ToString method and inherit it into the sub classes.

Step 1 - Create a new project.

Step 2 - Create your own exception class named "IdNumberException.

Step 3 - Create an abstract class named "Student" that has two private fields, name and Id. Create a constructor with the appropriate arguments. In the constructor check for valid ID numbers (between 1-9999). If the Id is invalid, throw your IdNumberException. Add a toString() method that returns a string with the name and IDnumber.

Step 4 - Derive a sub class named "GradStudent" from Student. Add a private field named "underGradDegree" for undergraduate degree (ex. BBA, BSCS, BSEE ...). Add a constructor that takes the 3 arguments. Override the inherited toString() method to also return "Graduate Student: Id, Name, under graduate degree".

Step 5 - Derive a second sub class named "PhdStudent" from GradStudent. Add a private field named "mastersDegree" for the master degree (ex. MBA, MSCS, MSEE ...). Add a constructor that takes the 4 arguments. Override the inherited toString() method to return "PhD Student: Id, Name, under graduate degree, masters degree"

Step 6 - Last add a class named "TestStudent" to the project. In the main method use the following code:



Here is my code:

TestStudent Class:




Student Class:



GradStudent Class:



PhdStudent Class:



IdNumberException Class:


-------------------------------------------------------------------
Any assistance would be greatly appreciated. Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Your assignment wrote:Add a toString() method... Override the inherited toString() method...


You haven't done that.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Yeah I was a little confused as to what they were asking for there. I thought thats what I was doing by creating these:

Student (original string method):



GradStudent (overridden string method):



PhdStudent (overridden string method):



or, are they asking to override the actual object.toString() method? not sure how to do that...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Well, no. This:

doesn't create a toString() method. It creates a returnString() method.

Mike Sabo wrote:or, are they asking to override the actual object.toString() method? not sure how to do that...

Of course you know how to override a method. Your post contains an example of how to override a method. You wrote that code which overrides a method, so why do you now say you don't know how to override a method?
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
You were 100% correct. I changed the method name in all occurances to toString() instead of the returnString() and it worked; however i'm curious. Why did it make a difference as to what my method name was?

Thank you very much!
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Oh and one last question for anybody... Why did my Student class need to be abstract? I didn't have any abstract methods.
Mykhailo Kozik
Greenhorn

Joined: May 12, 2011
Posts: 16

Mike Sabo wrote:You were 100% correct. I changed the method name in all occurances to toString() instead of the returnString() and it worked; however i'm curious. Why did it make a difference as to what my method name was?

Because every your class implicitly extends Object, and toString() - its an Object's method.
If you call:

it means that you call:

But as second way you can call:

So, choose appropriate for your goals. First way is preferable.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Mike Sabo wrote:Oh and one last question for anybody... Why did my Student class need to be abstract? I didn't have any abstract methods.


Because the instructions said to make it abstract.

If you want to know why the instructions said that, you should ask the people who wrote the instructions.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
great thanks everybody!
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Yeah the question may have sounded silly, but I thought maybe there was a reason for it, and that I was doing something incorrectly by not using an abstract method when I should have been. I'm closing the post. Thank you both for your help!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Mike Sabo wrote:Yeah the question may have sounded silly, but I thought maybe there was a reason for it...


Well, yeah, in real life there would (or should) be a reason for using an abstract class. And there are real-life reasons why you would want to do that, but I'm not going to try to read the minds of your instructors. In this case I think the reason could be "So that the students learn about abstract classes".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing memory address instead of string value.