Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can this be done in java please? Create a program that incorporates a main metho

ID: 3932114 • Letter: C

Question

can this be done in java please?

Create a program that incorporates a main method and a value-returning method. The main method shall call the value-returning method, passing zero arguments. The value-returning method shall return an integer value from 2 to 10 (valid values) or a value of 99 (a flag to indicate that the user quit before entering a valid value). Display the numeric value returned in main() when the value-returning method completes. The value-returning method needs to do these things: 1. Prompt the user for an integer value between 2 and 10, or 99 to quit 2. Validate the input to ensure it is in range 3. If out of range, display an error message, then re-prompt, and try again 4. Continue to do this until the value is in range or the user types 99 5. Return the numeric value to the calling (main) method

Explanation / Answer

ValueReturn.java

import java.util.Scanner;


public class ValueReturn {

   public static void main(String[] args) {
       while(true){
           int n = value_returning();
           if(n == 99){
               break;
           }
           else{
               System.out.println("You entered: "+n);
           }
       }
   }
   public static int value_returning(){
       Scanner scan = new Scanner(System.in);
       while(true){
       System.out.print("Enter an integer value between 2 and 10: ");
       int n = scan.nextInt();
       if(n < 2 || n > 10 && n != 99){
           System.out.println("Invalid input. An integer must be between 2 and 10. ");
       }
       else{
           return n;
       }
       }
   }

}

Output:

Enter an integer value between 2 and 10: 2
You entered: 2
Enter an integer value between 2 and 10: 6
You entered: 6
Enter an integer value between 2 and 10: 1
Invalid input. An integer must be between 2 and 10.
Enter an integer value between 2 and 10: 2
You entered: 2
Enter an integer value between 2 and 10: 7
You entered: 7
Enter an integer value between 2 and 10: 8
You entered: 8
Enter an integer value between 2 and 10: 11
Invalid input. An integer must be between 2 and 10.
Enter an integer value between 2 and 10: 99

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote