Capturing an object

Hi there, I have a node class, a stack class and an object class.

When using s.peek() on my stack, it return a type "object". Which holds a string value that I want..

I have tried making a new object then setting the value of that object to equal peek() this is demonstrated below:

Code:

String lol = new String("heyhey");

Object lookAtTop = new Object(lol);

lookAtTop = s.peek();

This is just example code, but I get this error:

incompatible types, found : java.lang.Object

required: Object

[553 byte] By [NewbForJavaa] at [2007-11-27 4:50:52]
# 1

> incompatible types, found : java.lang.Object

> required: Object

Don't call your class "Object" because Java has a class of this name and it will cause all sorts of unpleasantness from the compiler.

Better to call this class StackElement or something. (If you must have a special class for it.)

pbrockway2a at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 2
unfortunately I have to call it Object, as when my assignment gets marked the tutors use associated files which accept type Object
NewbForJavaa at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 3
Dasturdly!
Hippolytea at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 4

> unfortunately I have to call it Object,

Are you sure you were supposed to create your own Object class? Perhaps they meant your stack to consist of things that belonged to the built in Object class of Java.

Do the elements of the stack have any special behaviour associated with them (in the instructions for the assignment)? If not, throw away your implementation of Object and use the Java one.

(If you really must have your own class Object the solution lies in putting it in a package and always referring to it with its fully qualified name - the one with all the dots in it.)

pbrockway2a at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 5
And were you required to use this constructor:new String("heyhey")
Hippolytea at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 6

I don't know if I was supposed to create my own, it would make a lot of sense if I wasn't supposed to...

Whats this built in object class? How do I use that :/

I've tried commenting out my object class and the object constructor still works but I get same error message

Message was edited by:

NewbForJava

NewbForJavaa at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 7

> And were you required to use this constructor:

> new String("heyhey")

to create a String that would be used as an argument for the Object constructor which was rather pointlessly initialising the lookAtTop variable?

Really I think you need to have another long slow think about the instructions you were given, and ask here about something specific if you are uncertain.

pbrockway2a at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 8
> Whats this built in object class? How do I use that :/It's documented here: http://java.sun.com/javase/6/docs/api/java/lang/Object.html
pbrockway2a at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 9

I definitely understand what I need to do, I have all the files here

StackInterface

StackReferenceBased

Node Class

its all here... I just want to capture what this peek return

far out its annoying

whats the different between

java.lang.Object

and Object

NewbForJavaa at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...
# 10

right! I think I understand it better now, I am able to use:

String whatever = (String)s.peek();

But I have to first push a String value in..... which isn't working

I've tried making my own constructor for Object, so I could push and peek with type object then just run a getString() method for Object. but the values not there when i tried this.

NewbForJavaa at 2007-7-12 10:04:20 > top of Java-index,Java Essentials,New To Java...