• 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

Having trouble invoking methods from another class?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the subclass, everything checks out,




now when I try to invoke the methods here to generate a list of random characters, the Class.method(); syntax doesn't work, culprits are on line 21 and 22, the forloop is red and so is the method call


 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure about exact error message you get, but it seems these two classes are in different packages.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your classes are in different packages.

You need to add some import statements.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And your for loop is missing an i.
 
Jeffrey Gutierrez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you!
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest that using Math#random like that is a very error‑prone way to get those letters. Have you considered using an array of letters and using Random#nextInt?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeffrey Gutierrez wrote:This is the subclass, everything checks out


For the sake of clarity: RandomClass is not a subclass of RandomProgram class (I presume this is what you meant), in fact, RandomClass is a subclass of class Object.
You could read about it here < link
 
Jeffrey Gutierrez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Jeffrey Gutierrez wrote:This is the subclass, everything checks out


For the sake of clarity: RandomClass is not a subclass of RandomProgram class (I presume this is what you meant), in fact, RandomClass is a subclass of class Object.
You could read about it here < link

I understand the concept that classes in the java api are inherited, but what about classes that you make from scratch? can you make them inheritable? Say I create 7 classes and I want them all working and communicating with eachother, is there a specific way of putting them together?

forgive me from being vague... I promise I'll get better at that <3

Campbell Ritchie wrote:I would suggest that using Math#random like that is a very error‑prone way to get those letters. Have you considered using an array of letters and using Random#nextInt?



this is the perfect opportunity for me to learn how to do that as I just started my first lesson on arrays today, can you explain how to do it? and also, why are you using Math#Random and not Math.Random?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeffrey Gutierrez wrote:I understand the concept that classes in the java api are inherited, but what about classes that you make from scratch? can you make them inheritable? Say I create 7 classes and I want them all working and communicating with eachother, is there a specific way of putting them together?


I'm not sure I fully understand you question.
- Yes, you can inherit attributes (variables), operations (methods) from your classes created from scratch (it does "extend").

- To make classes working together and communicating with each other - this situation probably in a best way describes UML term "coupling". Degree at which classes are interconnected with each other.
Communication happens by sending messages (invoking methods) usually on particular instances of the class object.

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Say I create 7 classes and I want them all working and communicating with each other, is there a specific way of putting them together?



There isn't a specific way, but one way is to pass some variable around to the methods:

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will find a long discussion about ”random“ numbers here. I hope you will find it helpful
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic