wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Declaring string variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaring string variables" Watch "Declaring string variables" New topic
Author

Declaring string variables

Michael Roslansky
Greenhorn

Joined: Mar 12, 2013
Posts: 10
Hi, I'm working my way through an AP computer science course on Java and I have some pretty scrubby questions.
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Welcome to the Ranch, Michael!

When posting code, please UseCodeTags(⇐click) so it will be readable. I've added them this time for you. I also:

  • Moved the call to super() to its own line, rather than on a line following the opening { brace.
  • Added braces around your for loop body. You should ALWAYS do that with if/while/for, even when it's not strictly necessary.
  • Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    And that for loop is very confusing. What is it supposed to do? What happens if you enter -1500 for minutes? Will you get -1o’clock?
    Michael Roslansky
    Greenhorn

    Joined: Mar 12, 2013
    Posts: 10
    Thank you for replying to my thread!
    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:
    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.
    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.

    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.

    Thanks again.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    Michael Roslansky wrote: . . .

    Campbell, I didn't write the Time constructor. . . . changing that For statement . . .
    Thank goodness for that!

    And, “you’re welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Declaring string variables