Hello, I have a question as follows: Write a method called hasMidpoint that acce
ID: 3553035 • Letter: H
Question
Hello, I have a question as follows:
Write a method called hasMidpoint that accepts three integers as parameters and returns 'true' if one of the integers is the midpoint between the other two integers; that is to is if one integer is exactly between the two others. The method should return false if no such midpoint relationship exists. For example, (7,4,10) should return false.
Below is what I've written down currently, although I haven't been able to get it to work yet. I've got the number inputs and am but can't get it to run my method at the end, which I think is correct. Any help would be appreciated. Thanks!
import java.util.*;
public class midPoint1{
public static void main(String[]args) {
int a, b, c;
Scanner console = new Scanner(System.in);
System.out.println("Enter a number: ");
a = console.nextInt();
System.out.println("Enter a second number: ");
b = console.nextInt();
System.out.println("Enter a third number: ");
c = console.nextInt();
hasMidpoint(a, b, c);
}
public static boolean hasMidpoint(int a, int b, int c) {
int midpoint = a + c;
if(b == midpoint) return true;
else
return false;
}
}
Explanation / Answer
import java.util.*;
public class midPoint1{
public static void main(String[]args) {
int a, b, c;
Scanner console = new Scanner(System.in);
System.out.println("Enter a number: ");
a = console.nextInt();
System.out.println("Enter a second number: ");
b = console.nextInt();
System.out.println("Enter a third number: ");
c = console.nextInt();
boolean check = hasMidpoint(a, b, c);
if(check == true)
System.out.println("True");
else
System.out.println("False");
}
public static boolean hasMidpoint(int a, int b, int c) {
int midpoint = (a + c)/2;
if(b == midpoint) return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.