First up, I'm working on encapsulation and inheritance right now. However, the way that the book has taught the material, strings and variables are still a new concept to me. So, I've been cruising along until I hit Instance Variables. My one assignment wants me to create a constructor that contains two integers and a string. When I create a new object, I can enter integers just fine, but I get an error when entering the string saying that I need to declare the variable. I'm pretty sure this involves the use of an instance method, but I'm failing to make a connection. Here's the code if I'm not explaining correctly.
So, I realize I need to create an instance method to define the string, but how would I go about doing that? Every attempt I've made at it left me with a "cannot find symbol - constructor Time()". I hope you guys can help me, and I would be delighted to use this as a place to find resolutions to my many problems with Java.
When posting code, please UseCodeTags(⇐click) so it will be readable. I've added them this time for you. I also:
Michael Roslansky wrote:
"cannot find symbol - constructor Time()".
Read that message carefully. Now look at the line it's complaining about, the one where you're trying to create a Time object. (In the future, please copy/paste the exact, complete error message, and indicate clearly which line it's talking about.) Now look at the constructor for Time that you have written. Do you see a disconnect between how you're trying to create a Time and how you've said a Time has to be created?
Jeff, I should specify. The error message I was getting was from trying to compile an instance method. I'll paste that code into here.
I realized that by removing "extends Time" it allowed my program to run correctly. Why is that? I still have my original problem though. The assignment says:
I'm pretty sure I was able to do part a and b, but c is what seems to be messing me up. When I try to create a new Time object it keeps telling me "Error: cannot find symbol-variable x". That's why I thought you would have to define a string variable in a instance method, but isn't that just using the constructor anyway? It's just tough wrapping my head around it.
If you wanted Time objects to have a third attribute, the name of the day of the week, then (a) What instance variable declaration would you add? (b) What change would you make in the constructor? (c) What instance method would you have that tells the caller the object's day of the week? Write and compile the revised class.
Campbell, I didn't write the Time constructor. It's straight out of my textbook and changing that For statement is probably going to be an assignment.