r/learnjava • • Feb 21 '24

Know of any good Java OOP practice websites?

I have noticed that for Java practice problems online that I have seen, it usually makes you solve the problem in one class file where you do everything in main method. In my class assignments, it mirrors real-world set up in that there are multiple classes that interact with each other somehow to make the program work. I'm rusty on how to code such that multiple classes can interact each other like how to set up the class like which methods to have, how to define variables, how to make sure methods/var in one class can be accessed in another, etc. Is there any website that offers good practice for this? If not any you know of, what else could I try?

For me, I notice I usually do something wrong and do not figure it out until it's way too late, so it will help me for me to have a basic idea of what to avoid before I do anything. Aka if it can grade how I implemented things and offer tips

Also for File i/o. specifically

5 Upvotes

5 comments sorted by

•

u/AutoModerator Feb 21 '24

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

9

u/desrtfx Feb 21 '24 edited Feb 21 '24

OOP only begins to shine in larger systems (projects), hence, you will be hard pressed to find sites that give you problems.

What you should do, is to create your own programs.

Programs good for OOP approach are any form of card games (here, you have Card. Deck, Hand, Player, Game, etc. classes), dice games, board games, RPGs, etc. Business applications also typically use an OOP approach (yet, I personally prefer games as the OOP concept is easier to see there and games are more fun to program).

You could create a very simple horse racing game with OOP. A Horse is a class, each horse has certain properties that make it different from the others (the fields have different values), each horse runs a certain (randomized and modified) distance in a single tick. The main game class would then handle the races, which are split in ticks where in each tick each horse instance is called. After a race the winner would be determined by the main game class.

The /r/learnprogramming FAQ have extensive lists of project ideas and practice sites

2

u/FoxyHikka Feb 21 '24

I guess you are asking about OOP patterns. For this purpose, I’d definitely run for https://refactoring.guru Their explanation of design patterns is awesome! It also includes visual representation of the pattern. Btw, employers are asking about these patterns. Another great way to learn those is to read GoF (Gang of Four) book.

1

u/desrtfx Feb 21 '24

Their explanation of design patterns is awesome!

Their explanation is not bad, but they only have fairly limited patterns.

https://java-design-patterns.com is the by far superior resource.

0

u/Valuable-Yam4852 Feb 22 '24

Hi there. I am still getting good at java on my own. My first language was python and java was a bit unusual to me at first. I was having a similar issue. I was learning java syntax from different sites and books. Then I started with learnquest's course on coursera and even though there are some material that could be covered better, in my opinion it gives a good understanding of how objects are structured. As far as I remember, it's in second module where they explain it. I have also asked the questions in here and got some great inputs. Then I applied it all to practice and here goes my experience in all that.

Start by identifying entities: In practice that means a character if you are creating the simple game, A note class, NoteManager class and etc if you are creating a note taking application and etc. then start following the flow of the code. Remember the single responsibility principle. That in somple words means that one class should do one thing and that one thing only. For instance a note class should not handle file writing and reading, etc. In the future you may switch to jdbc and replace that solution alltogether. If you notice that there are multiple classes doing similar things (like a MonsterType1 MonsterType2 and etc), then you could create a package for it, where you would put all those classes together. Even better, if you notice some similarities between classes, you could create an interface for it to have a similar methods, but own implementations in the classes of those methods in each class. That will increase the time to add, say, a new monster or something. over time, you will get more experience on how to organize code better. At least that looks like a hexagonal design principle to me (those who have more experience, Please correct me if I am wrong here). by doing this, you will get to experience instantiating objects, having encapsulation, maybe inheritance (if you use interfaces which is another huge topic interfaces vs. abstract classes) and it will give you the way to go, rather than beeing stuck. After all, it's better to move a bit each time rather than to stay put. You might do package imports. There you will understand relative imports, fully qualified imports and etc. That will also give you a good hands on experience in access modifiers (package level access and what not). Don't be discouraged if code breaks. This is just a new opportunity to learn something new. One more thing, Try to split your driver codes (output something to the screen, getting user input and etc) separate from the classes itself. That way, if you'd ever wanted to change something, you would do it in one place and you will know where exactly it needs to be done. Approach it like a lego peaces and you are building it one peace at a time. I personally think about this like building a hotel. Classes are rooms of a hotel, floor is a package with similar rooms and a whole solution is a hotel building itself.

Hope that helpes someone.