0

I am working on a classes challenge in school. We have to great a Boat class and Harbor class. I have gotten every test to work except the final test of where boats are parked in the Harbor. The Harbor constructor contains an array for the Harbor.

The test we have to run is below and the code I have written (for the Harbor class) is below that. Fresh eyes are helpful!

@Test
    void testboatStock()
    {
        Boat boat1 = new Boat("BMC", Color.GREEN);
        Boat boat2 = new Boat("BMX", Color.RED);
        Boat boat3 = new Boat("UXB", Color.YELLOW);
        
        Harbor stock = new Harbor(5);
        assertEquals(null, stock.getBoatAt(0));
        assertEquals(null, stock.getBoatAt(1));
        assertEquals(null, stock.getBoatAt(2));
        assertEquals(null, stock.getBoatAt(3));
        assertEquals(null, stock.getBoatAt(4));
        
        // Hint: parkBoatAt is not just a accessor, and not just a mutator
        assertEquals(null, stock.parkBoatAt(boat1, 3));
        Boat retrievedBoat = stock.parkBoatAt(boat2, 3);
        assertEquals(boat1, retrievedBoat);
        retrievedBoat = stock.parkBoatAt(boat3, 3);
        assertEquals(boat2, retrievedBoat);
        Boat[] inventory = stock.getInventory();
        assertArrayEquals(new Boat[]{null, null, null, boat3, null}, inventory);
        stock.parkBoatAt(boat2, 1);
        assertArrayEquals(new Boat[]{null, null, null, boat3, null}, inventory); // this is correct!
        assertArrayEquals(new Boat[]{null, boat2, null, boat3, null}, stock.getInventory());
    }
public class Harbor
{
    private int slipNumber = 0;
    private Boat[] boats = new Boat[slipNumber];
    
    public Boat parkBoatAt(Boat boat, int slipNumber)
    {
        boat = boats[slipNumber];
        if(boats[slipNumber]== boat)
        {
            
            return boat;
        }
        else
        {
            return null;
        }
    }
    public Boat getBoatAt(int slipNumber)
    {
        if(boats[slipNumber]==null)
        {
            return null;
        }
        return boats[slipNumber];
    }
    
    public Boat[] getInventory()
    {
        return boats;
    }
    public Harbor(int numberOfSlips)
    {
         boats = new Boat[numberOfSlips];
    }
    public Harbor()
    {
        
    }

}
3
  • Why doesn't your parkBoatAt actually park a boat? Doesn't the method name say it should do that? Commented Mar 3, 2021 at 2:57
  • I thought I did that with boat=boats[slipNumber]; Commented Mar 3, 2021 at 3:19
  • The tests confused me, but the basis is that the Harbor is initially empty. When going to park a boat, it will return if that spot is available or taken. So the initial parking of the boat, returns that the spot is empty. Then the next test shows that the boat parked in the test before is there and so on. Commented Mar 3, 2021 at 3:20

2 Answers 2

1
public Boat parkBoatAt(Boat newBoat, int slipNumber) {   // 1
  Boat oldBoat = boats[slipNumber];
  boats[slipNumber] = newBoat;
  return oldBoat;
}

public Boat[] getInventory() {
  return Arrays.copyOf(this.boats, this.boats.length);   // 2
}
  1. You need to return the previous value of the element in array.
  2. You need to copy the array to make sure it is unchanged.
Sign up to request clarification or add additional context in comments.

Comments

0

It is written as a comment in the code for method testboatStock() in your question. Namely...

Hint: parkBoatAt is not just a accessor, and not just a mutator

The requirements are not completely clear to me but I presume that method parkBoat should check whether the element in array boats at index slipNumber is null. If it is, then put the boat into the array at that index and return the boat, thus indicating that the boat was successfully parked. Otherwise, i.e. if the array index already contains a boat, that means the spot is taken and you can't park two boats in the same spot.

public Boat parkBoatAt(Boat boat, int slipNumber)
{   
    if(boats[slipNumber]== null)  // Change here.
    {
        boats[slipNumber] = boat;  // Added this line.
        return boat;
    }
    else
    {
        return null;
    }
}

Basically I just added one line to your code and changed the if condition. Also note that it would probably be a good idea to first check the method arguments, i.e. that boat is not null and that slipNumber is between 0 (zero) and the size of array boats.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.