Question: Part 1 - numberOfStoplights Method Write a public static method named numberOfStoplights that takes two arguments (a double and an int) and returns a value
Part 1 - numberOfStoplights Method
Write a public static method named numberOfStoplights that takes two arguments (a double and an int) and returns a value (an int). The first argument will be the length of the road project in miles, and the second argument will be the number of lanes for this road. When called, this method should compute and return the number of stoplights needed for this road project.
Method Header
Here is a template for you to follow for this method:
public static int numberOfStoplights(double miles, int lanes) {
// Use the values stored in the miles and lanes parameter variables
// to calculate the total number of stoplights needed.
// Then return (not print) that calculated value.
}
Method Body
First, compute the number of stoplights for each intersection of this road and store this number in a variable named stoplightsPerIntersection. Remember that the number of lanes for this road will be stored in the parameter variable named lanes. Also remember that at each intersection there will be two stoplights plus one more stoplight for each lane - for example, for a four lane road, each intersection will have six stoplights (2 for the intersection itself, plus one more for each of the four lanes).
Second, compute the number of intersections for this length of road and store this number in a variable named numberOfIntersections. Remember that the length of the road (in miles) will be stored in the parameter variable named miles. Also remember that there will be one intersection for each full mile of road - for example, for a 2.75 mile road, there will be 2 intersections, because there is one intersection for each full mile of road.
Third, multiply the numberOfIntersections by the stoplightsPerIntersection to compute the total number of stoplights required for this road project. Store this result in a variable named totalStoplights.
Finally, return the value stored in totalStoplights.
Here are some examples of calls to this method, and the expected return value for different sets of argument values:
numberOfStoplights(1.0, 1) should return the value 3
numberOfStoplights(1.0, 2) should return the value 4
numberOfStoplights(2.0, 1) should return the value 6
numberOfStoplights(2.75, 3) should return the value 10
Helpful Hints:
You may wish to write some additional code (in the main method) to test your method.
There should be no print (or println or printf) statements in your numberOfStoplights method.
There should be no calls to any Scanner methods in your numberOfStoplights method.
You can use a cast operation or the Math.floor method to perform the necessary rounding operations.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
