In: Computer Science
Write a Java program to do the following:
Specifics:
Write an application that prompts a user for two integers and displays every integer between them. Display a message if there are no integers between the entered values.
Make sure the program works regardless of which entered value is larger. Save the file as InBetween.java.
Don’t forget to create the application/project InBetweenTest.java Class that has the main method and an object to use the InBetween class.
// CODE: Code of InBetween.java
package application.project;
import java.util.Scanner;
public class InBetween {
public void integersInBetween() {
Scanner sc = new Scanner(System.in);
int a, b;
// Taking 2 integers from the user
System.out.print("Enter the first number: ");
a = sc.nextInt();
System.out.print("Enter the second number: ");
b = sc.nextInt();
// If a < b then swapping a and b.
if (a > b) {
int temp = a;
a = b;
b = temp;
}
// Handling the case when there is no element between the numbers.
if ((a == b) || (b == a + 1)) {
System.out.println("No integers present between the given integers.");
}
// Printing the integers in between the provided values.
else {
for ( int i = a + 1; i < b; i++ ) {
System.out.println(i);
}
}
}
}
// CODE: Code of InBetweenTest.java
import application.project.InBetween;
public class InBetweenTest {
public static void main(String[] args) {
InBetween inBetween = new InBetween();
inBetween.integersInBetween();
}
}
OUTPUT: