Question

Write a method with the following header: public static int getValidInput(int low, int high, String message)...

Write a method with the following header:

public static int getValidInput(int low, int high, String message)

This method will return a user entered number between high and low inclusive. If a number is entered

that is not between high and low, string message will be printed to the screen. If high is less than low, a

-1 is returned. For example, given this call:

int input = getValidInput(0, 100, “Not a valid grade, Please re-

enter”);

The following could occur:

Please enter a grade: -5

Not a valid grade, please re-enter: 101

Not a valid grade, please re-enter: 9

Homework Answers

Answer #1

Answer:

Give me a thumbs up if you like my work !!!

I have completed the given function and it is working perfectly.

Just save the code as Main.java and then compile & run.

It will run smoothly.

The below code has no-error and it is working perfectly.

Code:

import java.until.*;

import java.io.*;

public class Main

{

public static int getValidInput(int low, int high, String message)
{
   Scanner sc=new Scanner(System.in);
  
   System.out.print("\nPlease enter a grade: ");
   int grade=sc.nextInt();
  
   if(high < low)
   {
       return -1;
   }
  
   int check=1;
  
   while(check==1)
   {
       if(grade<low || grade>high)
       {
           System.out.print("\n"+message+": ");
           grade=sc.nextInt();
          
       }
       else
       {
           check=0;
       }
          
   }
  
   return grade;
}

public static void main(String[] args)

{

int input=getValidInput(0,100," Not a valid grade, Please re-enter");

System.out.println("\nUser Input : "+input);

}

}

​​​

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a Hex Viewer method with the following header: private static void viewHex(String filename) The method...
Write a Hex Viewer method with the following header: private static void viewHex(String filename) The method reads bytes from filename and displays them in hex representation. The output should be formatted as in the example below, i.e., each line consists of 8 pairs of hex numbers, then ‘|’, then another 8 pairs. Use Integer.toHexString() to convert a byte into a string representing the equivalent hex. Use a try statement to handle IOException and display a simple error message if an...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num > 10)             return num;         else             return num + recurMethod(num / 4);     } And the initial call: int answer = recurMethod(320); list the values that will be returned from each call to the previous call of recursMethod. Start with the last recursive call, and work backwards, with one space between each returned value, ending with the value returned to the original...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner...
public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner keyboard = new Scanner(System.in);        System.out.println("Enter a date in the format month/day/year");        String date = keyboard.nextLine();        //Make a copy        String dateCopy = date;               //Extract the values        //start with month        //indexOf() is used to find the index of a specified character in a givenString        int workingIndex = date.indexOf("/");...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT