Eirik Lauritsen wrote:Do you have any tips on how to practise methods or should i just write programs and then restructure them so it includes different methods?
I believe practice is important and key here, and better quality comes with extensive amount of experience. I'm unsure you formulating second half of the sentence correctly. To me sounds like, you write program in whenever way it goes, then you just adding different methods because: you just have to, somebody told that, heard of them they are useful...
Methods should come I believe from common sense. When you get a task to slice the bread, you know exactly what you need - a knife (maybe a special kind of it). If you were given piece of raw tin or metal material you might would be able to achieve the task too, but would be that comfortable? Would make sense?
Now you have knife. You don't stuff methods just because you have to, you create them and re-use them whenever you see they make sense. What they do, they hide from you the raw tin or raw metal material, and instead, they give you a name "knife", which on its own gives you a pretty good idea what you can do with it without even seeing him.
Same with your code. Your code currently is pretty simple, and at the same time pretty confusing, because it isn't straight forward for a regular human eye.
To be honest, I don't even know the whole goal of your program, because your class is named
"MojangDel4". Class represents an idea of a Kitchen, where knife could be found. So when you hear somebody saying Kitchen, you would be pretty confident you'd find the knife there if needed, plates, cooking machine and similar. Now, what's about MojangDel4? By the way, I thought there is 14 at the end, it seems only 4 with a prefix of l.
So first - name class properly, that is the starting point in your case I'd say. What this class represents, some kind of game? Name it SharkGame, it is still tells nothing - but we know it is a game and might be even fun! Write nice comment above the class name in a format:
So you and the reader know the rules and gets primary idea what is the game about.
Then look down into methods. You have lots of if/else if, maybe there would make sense having a method
move()?
moveEast(), moveWest(), moveNorth(), moveSouth(). Noticed? move move move... When you see something like that, you can get an idea, that might addition of parameter would allow having only
moveTo(Location). And then we go back to object-oriented programming.
Have you been mentioned about objects in your course yet? Which year of course you are in? This exercise is actually quite difficult to solve in procedural way, because you can't wire separate pieces properly as they don't have much meaning.
Will continue later... got to go now..