your askForYesNo method, how would you get the same result with only using one if/else if/else statement?? (IN JAVA PROGRAMMING) You also need to fix up your styling: All your method comments need to be in a block comment (/** comment */) and should include the parameters taken and values returned All your variables need to have a line comment (//comment) next to them explaining what they are going to be used for

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

With your askForYesNo method, how would you get the same result with only using one if/else if/else statement?? (IN JAVA PROGRAMMING)

You also need to fix up your styling:

  • All your method comments need to be in a block comment (/** comment */) and should include the parameters taken and values returned
  • All your variables need to have a line comment (//comment) next to them explaining what they are going to be used for

 

 

import java.util.Scanner;

 

/**

 * 4.3CR User Input Functions

 * a functions to read a number within a range, and read more natural boolean values from the user

 * 

 * @author MEHAKPREET KAUR

 * @version 04/08/2021

 */

public class UserInput {

  // method to diplay askForInt 

  public static int askForInt(Scanner in, String ask, int min, int max){

    int value; //value entered by user

    System.out.print(ask + "(" + min + "-" + max + "): ");

    value = in.nextInt();

     

    //loop check for invalid value

    while(value < min || value > max){

      System.out.println("invald value");

      System.out.print(ask + "(" + min + "-" + max + "): ");

      value = in.nextInt();

    } 

    return value;

  }

   

  //method to display askForDouble 

  public static double askForDouble(Scanner in, String ask, double min, double max){

    double value;

    System.out.print(ask + "(" + min + "-" + max + "): ");

    value = in.nextDouble();

    //loop check for invalid value

    while(value < min || value > max){

      System.out.println("invald value");

      System.out.print(ask + "(" + min + "-" + max + "): ");

      value = in.nextDouble();

    } 

    return value;

  }

  //this method repetedly prompt the user to enter the valid boolean value until it falls under given range

  public static Boolean askForYesNo(Scanner in, String ask) {

    System.out.println(ask + " ( yes/no )");

    String value = in.next();

    Boolean resultValue = true;

    Boolean isValid;

    do {

      value = value.toLowerCase();

      if (value.equals("y") || value.equals("n") || value.equals("yes") || value.equals("no") || value.equals("true") || value.equals("false")) {

        isValid = true;

      } else {

        isValid = false;

      }

      if (value.equals("y") || value.equals("yes") || value.equals("true")) {

        resultValue = true;

        break;

      } else if (value.equals("n") || value.equals("no") || value.equals("false")) {

        resultValue = false;

        break;

      } else {

        System.out.println("Invalid Value");

        System.out.println(ask + " ( yes/no )");

        value = in.next();

      }

    } while (!isValid);

    return resultValue;

  }

   

  //this is the main method of the program

  public static void main(String[] args) { 

    Scanner in = new Scanner(System.in);

    //All values are initialised so code will compile and run

    int guess = -1;    //user's guess (between 1 and 10)

    double percent = -1;  //a percentage (as a value between 0 and 1)

    boolean again = false; //do they want to go again?

     

    System.out.println("Testing prompt for int... the number should be saved in guess.");

    System.out.println(" - Enter '30' -- should loop with error");

    System.out.println(" - Enter '-1' -- should loop with error");

    System.out.println(" - Enter '3' and it should work");

    // guess = askForInt(in, "Enter a number", 1, 10);

    guess = askForInt(in, "Enter a number", 1, 10);

    System.out.println("Guess: " + guess);

    System.out.println();

     

    System.out.println("Testing prompt for double... the number should be saved in percent.");

    System.out.println(" - Enter '30' -- should loop with error");

    System.out.println(" - Enter '-2' -- should loop with error");

    System.out.println(" - Enter '0.5' and it should work");

    // percent = askForDouble(in, "Enter percent value", 0.0, 1.0);

    percent = askForDouble(in, "Enter percent value", 0.0, 1.0);

    System.out.println("Percent: " + percent);

    System.out.println();

     

    System.out.println("Testing prompt for yes/no... the result is saved in again.");

    System.out.println(" - Extend these boolean tests by adding more messages to verify your solution!");

    System.out.println(" - Enter 'yes' and it should succeed");

    // again = askForYesNo(in, "Play again?");

    again = askForYesNo(in, "Play again?");

    System.out.println("Again: " + again);

    System.out.println();

    System.out.println(" - Verify that it can also read in false...");

    // again = askForYesNo(in, "Play again?");

    again = askForYesNo(in, "Play again?");

    System.out.println("Again: " + again);

    System.out.println();

     

    System.out.println("Tests complete...");     

  }

   

}

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Math class and its different methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education