Home > front end >  How can I fix this function in java?
How can I fix this function in java?

Time:08-29

Prompt:

Peter the postman became bored one night and, to break the monotony of the night shift, he carried out the following experiment with a row of mailboxes in the post office. These mailboxes were numbered 1 through 150, and beginning with mailbox 2, he opened the doors of all the even-numbered mailboxes, leaving the others closed. Next, beginning with mailbox 3, he went to every third mail box, opening its door if it were closed, and closing it if it were open. Then he repeated this procedure with every fourth mailbox, then every fifth mailbox, and so on. When he finished, he was surprised at the distribution of closed mailboxes. Write a program to determine which mailboxes these were.

Code:

public static boolean[] experiment(boolean[] mailboxes) {
    for(int i = 1; i < mailboxes.length; i  = 2){
        mailboxes[i] = true;
    }
    for(int z = 3; z < mailboxes.length; z  ) {
        for(int y = z; y < mailboxes.length; y  = z)           
            if(mailboxes[y - 1] == false) {
                mailboxes[y] = true;
            }
    }
    return mailboxes;
}

The issue I am coming across with is that the output is incorrect. It is in increments of 2 when it should be 3, 4, 5, and so on. I have tried to change some numbers around in the experiment function but I have had no luck.

CodePudding user response:

A number of issues with your code.

Index confusion

Your first loop starts at i = 1, and this is to represent the second mailbox. Even though 'second' makes you think '2', 1 is in fact correct: index [1] is indeed the second mailbox. Java, just like computers and most other programming languages, is 0-indexed; english (which is the 'language' the problem is stated in) is usually 1-indexed. This explains why mailbox[1] refers to the second mailbox.

However, in your second loop, you then start with z=3, which is the fourth mailbox. The problem description wants the third.

Confusion about the problem

The second part ends up setting mailboxes[y] to true if mailboxes[y-1] is false, which is bizarre. Nothing in the problem domain describes either act. Specifically, no part of the problem domain says: "If a mailbox is closed, then open the mailbox to the right of it", nor is there any part of the domain that says: "If a mailbox is closed, then open it, but do nothing if it is open".

Needlessly complicated

You just wrote down what the problem says (incorrectly) without thinking it through, perhaps. There is no actual difference between the problem's first paragraph and its second. It's all the same thing: Swap the open/close state of every Xth mailbox, where X goes from 2 up to 150, and all mailboxes begin in a closed state.

In other words, the first part of your code (where int i = 1 is how the loop starts) is not neccessary.

Some hints

This is clearly a coding exercise so there's not much point just serving up the answer on a silver platter. So, some hints:

  • Remember, the Xth mailbox is represented by [x-1]. mailbox[1] is the second mailbox, mailbox[2] is the third, etc. mailbox[149] is the 150th, and last. mailbox[150] would throw an ArrayIndexOutOfBoundsException, as that would refer to the 151st mailbox which isn't a thing.
  • The mission is to FLIP the statea the mailbox. If it is open, then close it; if it is closed, then open it. ! flips a boolean. mailboxes[y] = !mailboxes[y] is the java version of "close it if it is open, open it if it is closed". No need to involve an if in the first place.

CodePudding user response:

There're several errors in the snippet.

for(int i = 1; i < mailboxes.length; i  = 2){
    mailboxes[i] = true;
}

is the special case and the others are the general cases. Hence you can write just the nested cycle, bypassing that one and have z start from 2.

The other 2 problems lies in the internal cycle (for y).

as i starts from 1 (meaning 2-1), so y should start from z-1, not z, or you will be shifted by 1 box, risking to miss the last box (or you should correct with y <= mailboxes.lenght). Because arrays in java starts with 0, meaning that the second element is 1, the third one is 2 and fourh one is 3...

The thrid error is the one @RealSkeptic pointed at (kudos to him). You're not closing the boxes, just opening them.

The forth problem lies in the if.

if(mailboxes[y - 1] == false) mailboxes[y] = true;

You check with index y-1 but change index y. You should change the same you are testing, not the next one.

Also, for the purposes of your program, you need a toggle function, not an if function like mailboxes[y] = !mailboxes[y]; (this closes the mailbox if opened and opens it if closed).

If you require it I'll provide the full code, but I understand this is an exercise, thus I suggest you to first try to rewrite it by yourself.

  • Related