Problem Statement
The road has negligible width, so it is just a horizontal line running along a given y coordinate. Create a class FowlRoad that contains a method crossings that is given roadY (the y coordinate of the road), and int[]s bobX and bobY giving Bob's path. It should return the number of times that Bob crosses the road.
The ith elements of bobX and bobY give the coordinates of a point in Bob's path. Bob starts at the first point, and proceeds in a straight line to the next point, continuing until he finishes at the last point. Bob does not start or end on the road.
Definition
- Class:
- FowlRoad
- Method:
- crossings
- Parameters:
- int, int[], int[]
- Returns:
- int
- Method signature:
- int crossings(int roadY, int[] bobX, int[] bobY)
- (be sure your method is public)
Constraints
- bobX contains between 2 and 50 elements, inclusive.
- bobY contains the same number of elements as bobX.
- roadY and each element of bobX and each element of bobY is between -100 and 100, inclusive.
- Neither the first nor the last element of bobY is equal to roadY.
Examples
6
{3,7,9,2}{-3,8,8,2}Returns: 2
As Bob goes from (3,-3) to (7,8) he crosses the road. From (7,8) to (9,8) he is moving parallel to the road, not crossing it. But when he goes from (9,8) to (2,2) he crosses the road again.10
{3,7,9,2}{-3,8,8,2}Returns: 0
Bob wanders around on the south side of the road, never crossing it.0
{3,2,5,6,9}{2,0,0,2,-88}Returns: 1
Bob wanders from the north side onto the road, along the road, then back to the north side at (6,2). On his last segment he crosses the road from the north side to the south side.0
{3,2,5,6,9}{-2,0,0,2,-88}Returns: 2
This is the same as the preceding example except that Bob starts on the south side of the road at (3,-2). He wanders onto the road, along the road and then onto the north side thereby completing a crossing from south to north. On his last segment he crosses the road from north to south.