Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

calling output from other classes and methods to main for outpout  RSS feed

 
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize for improper terminology, as I'm struggling with getting the "lingo" down.

I have also read more threads than I can count here, as well as searched the net for a solution.

Here is what I have going on. I have two classes (classes are called MonitorAnimals which contains the method monitorAnimals() and MonitorHabitats contains the method monitorHabitats()) that perform a read file (using BufferedReader and FileReader), then I am outputting the data from an array based on user input (using Scanner).

I am attempting to call the data from the two classes back into main(). This, in turn, will be using a switch for user input to go in a particular direction. I can post the code for the two other classes if need be,
but I think they're OK, My "primary" class that includes main() is listed below.



I think once I can get the other methods from the other classes, I should be OK. I realize that there are probably better ways to go about this, but I'm trying to just make it work
I can work on more advanced techniques as I gain more knowledge.

thanks again

Bill
 
Ranch Hand
Posts: 1325
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of adding logic to the main, it is recommendable to write a run method. The code becomes much more robust and clean. You can have a look at this Main - Pain article.

https://coderanch.com/wiki/660020/Main-Pain  
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:Instead of adding logic to the main, it is recommendable to write a run method. The code becomes much more robust and clean. You can have a look at this Main - Pain article.

https://coderanch.com/wiki/660020/Main-Pain  



I can see your point, and I may go that route once I get everything working.

It doesn't, however, address my original point. What am I missing to successfully call my methods from my other two classes to make everything work?

If if I were to use a run() method, I'd still be in the same boat.

Bill
 
Angus Ferguson
Ranch Hand
Posts: 1325
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look at this piece of code.



You were trying to call the methods by using the instance of your classes. The instance of the class MonitorAnimal, for example, allows you to call the methods written in that class by adding a dot at the end of the instance. So, in this case, we are supposing that  .method1() has been written within the  MonitorAnimal class.
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:Please have a look at this piece of code.



You were trying to call the methods by using the instance of your classes. The instance of the class MonitorAnimal, for example, allows you to call the methods written in that class by adding a dot at the end of the instance. So, in this case, we are supposing that  .method1() has been written within the  MonitorAnimal class.




Thank you Angus!

That did the trick. I got the program working, now I can concentrate on cleaning it up and perhaps looking into the run()

thanks again

Bill
 
Marshal
Posts: 63837
209
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Platt wrote:. . . I can see your point, and I may go that route once I get everything working. . . .

I have heard that one before. We often see people posting poor style code who say they will sort out the style when they have finished coding, but that isn't how it works. You sort out the style and the design as you go. Otherwise you will probably leave the poor quality code uncorrected and lose marks for it.
Start with names, which you can read about here. That link might be old, but it is still relevant. A class name like MonitorAnimal tells me that you are looking for monitor lizards, not that you want to do any monitoring of animals. You would want to call the class AnimalMonitorer (only that is unpronounceable); monitorAnimal() might however be good name for a method. The act that you used those names in your code shows how much better they are as names for methods.
LInes 12‑13 show up a problem. You are not calling any methods there, but only creating objects. Yes, you need objects, but making them is only a part of the whole process. You need to know the difference between creating objects and calling methods on them. You would want something like animalWatcher.observeAnimal(a); where a is a variable of type Animal. So you need to decide what such a method is going to do, and then write the method to do that.
Lines 33 and 40 show you writing something you know will cause your code to ail to compile, so what's the point of that?
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have heard that one before. We often see people posting poor style code who say they will sort out the style when they have finished coding, but that isn't how it works. You sort out the style and the design as you go. Otherwise you will probably leave the poor quality code uncorrected and lose marks for it.



This may be well and good for someone who is experienced and knows where the pitfalls and tripwires may be located. I'm not there yet; I'm still at the stage of trying to get my code to work, then I can go back and make it pretty and/ or clean it up. I wouldn't even classify myself as a beginner yet, I'm still a very basic student who is only 6-7 weeks into my first Java class. I have taken a similar class that dealt with Python, but that is like comparing apples to oranges, true they're both fruits but are significantly different. I am learning, but it's a slow process.



Campbell Ritchie wrote:
Start with names, which you can read about here. That link might be old, but it is still relevant. A class name like MonitorAnimal tells me that you are looking for monitor lizards, not that you want to do any monitoring of animals. You would want to call the class AnimalMonitorer (only that is unpronounceable); monitorAnimal() might however be good name for a method. The act that you used those names in your code shows how much better they are as names for methods.
LInes 12‑13 show up a problem. You are not calling any methods there, but only creating objects. Yes, you need objects, but making them is only a part of the whole process. You need to know the difference between creating objects and calling methods on them. You would want something like animalWatcher.observeAnimal(a); where a is a variable of type Animal. So you need to decide what such a method is going to do, and then write the method to do that.
Lines 33 and 40 show you writing something you know will cause your code to ail to compile, so what's the point of that?



Again, I can see your point, though I don't fully agree. I felt that the choice of monitorAnimal was both simple and descriptive. I would not be surprised if my choices for method and class names change as I gain more experience. Once again, I'm not there yet.

As for lines 11-12 and 33 and 40.

I inserted them for the purpose of this post for clarity; my intent was to show where I suspected the errors may be. It is not something that I planned to include in my program.

Also, may I draw your attention to the final line in my original post?

" I realize that there are probably better ways to go about this, but I'm trying to just make it work I can work on more advanced techniques as I gain more knowledge."



I do not have years or even months of experience. I have not written multiple programs, nor do I fully understand most of the available techniques associated with writing code in any programming language. I am trying to learn how to do things correctly, and I understand that there are certain best practice techniques, but one must also consider personal style; what works for one may not be acceptable to another, etc. With experience comes the wisdom to do things better, quicker, and with less effort. Someone recently told me that it takes at least several years of intense study to master the art of programming; I believe it, as every time I attempt to do something I seem to be being exposed to something new.
 
Campbell Ritchie
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Platt wrote:. . . This may be well and good for someone who is experienced and knows where the pitfalls and tripwires may be located. I'm not there yet; . . .

No, you aren't there yet. You are at the stage where you need all the help you can get to see the pitfalls, and poor naming and formatting hide the pitfalls from you.

. . . I felt that the choice of monitorAnimal was both simple and descriptive. . . .

Agree; it is an good name for a method. It tells you what the method does.But not for a class; there you want a name that will tell you what the class (or its instance) is.

As for lines 11-12 and 33 and 40.

I inserted them for the purpose of this post for clarity . . .

Well, lines‑12 don't say what you are actually doing, so they denote confusion to be sorted out. And kindly tell us how stopping your code from compiling is going to make anything clearer. A far better placeholder would be System.out.println("Option 3");

I do not have years or even months of experience.

That is no reason to be dismissive of people who are trying to help you.

. . . personal style; what works for one may not be acceptable to another . . .

You will not be working programming on your own; you will have to use a style acceptable to those you work with.
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poor formatting is a bug, not something extra to add at the end.  In fact, it's much easier to format correctly as you code.  See HowToFormatCode (that's a link).
 
I am Arthur, King of the Britons. And this is a tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!