• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaring string variables

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Marshal
    Posts: 79180
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79180
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Michael Roslansky wrote: . . .

    Campbell, I didn't write the Time constructor. . . . changing that For statement . . .

    Thank goodness for that!

    And, “you’re welcome
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic