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.
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.
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.
Bill Platt wrote:. . . I can see your point, and I may go that route once I get everything working. . . .
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.
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?
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.
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; . . .
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.
. . . I felt that the choice of monitorAnimal was both simple and descriptive. . . .
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");
As for lines 11-12 and 33 and 40.
I inserted them for the purpose of this post for clarity . . .
That is no reason to be dismissive of people who are trying to help you.
I do not have years or even months of experience.
You will not be working programming on your own; you will have to use a style acceptable to those you work with.
. . . personal style; what works for one may not be acceptable to another . . .