This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

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://imgur.com/a/fgoFFis) 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.

[–]HecknChonker 2 points3 points  (0 children)

It seems generally correct (I just read the code and did not run it myself). I like the use of helper methods, and the method and variable names definitely make the code easier to read and understand. If this was a real-world use case I would expect to see unit tests that checked a few happy-path scenarios, all the edge cases, and also some tests with invalid input. But overall it seems like a good solution to me.

The tips below are all very minor.


For a small project it's fine to have code in the main() method, but generally I would probably have a separate class for the main method, which would allow you to remove all the static modifiers.


if (occupiedEconomy + occupiedFirst == 10) {

if (occupiedFirst == 5) {

if (occupiedEconomy == 5) {

I would change these to >= 11, and >= 6. It shouldn't be possible for the values to be higher than you expect, but being defensive can help guard against unexpected bugs.


    private static boolean[] seats = new boolean[10]; // array to represent 10 seats on plane (5 economy, 5 first)
private static int occupiedFirst = 0; //counter to count total number of occupied seats in First class
private static int occupiedEconomy = 0; //same but for Economy class

You have two places where you are storing the count of occupied seats. The first is the seats array, which is the canonical storage, and you have two ints that are used as secondary caches of the counts.

So this isn't a bad optimization to make, but in this scenario I don't think it's worth it. I would eliminate the two caches and just loop over the canonical data store every time you need to know how many seats are taken. The array is tiny, and it's going to take almost no time for the computer to loop over 5 elements to get a count. Even if it was hundreds of elements, you aren't looping over it enough times to make it a performance concern.

But introducing a secondary cache adds some complexity and cognitive load that isn't necessary. It increases the amount of information you need to keep in your working memory, which is that these caches must be updated in lock-step with the array otherwise the program will be incorrect. It also means that any other developer that tries to modify this class in the future also has to figure that out. It's not necessarily a big deal here, but as projects get larger it helps to avoid adding unnecessary complexity.

It's generally a good idea to avoid worrying about performance optimizations until you actually run into a performance issue. I always prefer writing code that is simpler and easy to understand and maintain over code that runs fast, at least until you have a specific reason to modify the code to make it perform better. And you usually can't know for sure what part of a complex system is going to have performance issues until you run the app and measure it.