234k views
23 votes
Write a function in Java that implements the following logic: Given three ints, a, b, and c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.

User James Yang
by
4.4k points

1 Answer

3 votes

Answer:

Follows are the code to this question:

public class Main//defining a class Main

{

public static boolean evenly_Spaced(int a,int b,int c)//defining a method evenly_Spaced that accepts three integer parameter

{

if(a-b==b-c)//defining if block to check small and the medium is the same as the difference between medium and large.

return true;//return value true

else//else block

return false;//return value false

}

public static void main(String[] args) //defining the main method

{

int a,b,c;//defining integer variable

a=4;//assigning value

b=6;//assigning value

c=8;//assigning value

System.out.println(evenly_Spaced(a,b,c));//use print method to call evenlySpaced

}

}

Output:

True

Step-by-step explanation:

In the above code, a boolean method "evenly_Spaced" is declared, that accepts three integer variables in its parameter and defined the if block to check the smallest and the medium value is the same as the difference value in between the medium and large value, and return its value.

In the next step, the main method is defined, and inside this three integer variable "a, b, and c" is declared, that holds a value and passes into the method and print its return value.

User Bvamos
by
4.4k points