Question

In: Computer Science

public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up,...

public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up, ‘d’ for down, ‘l’ for left or ‘r’ for up. And in case the user enters anything except one of these four letters, it will be kept asked to enter a new char until the entered char matches one of them.

(in java)

*note (do just complete this specific method as this is just one of the methods apart of this.)

Solutions

Expert Solution

import java.util.*;

class Hello {

    public static boolean compare(char c) {

        // comparing with u

        int p = Character.compare(c, 'u');

        // comparing with d

        int q = Character.compare(c, 'd');

        // comparing with l

        int r = Character.compare(c, 'l');

        // comparing with r

        int s = Character.compare(c, 'r');

        // returing true when we see u,d,l,r

        if ((p == 0) || (r == 0) || (q == 0) || (s == 0)) {

            return true;

        } else

            return false;

    }

    public static char inputDirection() {

        Scanner sc = new Scanner(System.in);

        char c;

        do {

            System.out.print("Please enter u for up , d for down, l for left and r for right: ");

            // reading the character

            c = sc.next().charAt(0);

            if (!compare(c)) {

                System.out.println("Please try again!");

            }

            // while the given char is not u,d,l,r repeat asking

        } while (!compare(c));

        return c;

    }

    public static void main(String[] args) {

        char x = inputDirection();

    }

}

output:


Related Solutions

public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';   ...
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';        }        String temp="";        for (int i = 0; i < str.length(); i++) {                 if(!temp.contains(String.valueOf(str.charAt(i)))) {                     temp += String.valueOf(str.charAt(i));                 }             }        char[] tempArray=stringToArray(temp);        int[] countArr=new int[tempArray.length];        int max=0;        for(int i=0;i<tempArray.length;i++) {            int cnt=numOccurences(tempArray[i],str);            countArr[i]=cnt;...
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;        int number = 0;        Scanner in = new Scanner(System.in);        System.out.print("Enter a letter: ");        letter = in.next().charAt(0);        if(letter == 'A' || letter == 'B' || letter == 'C') number = 2;        if(letter == 'D' || letter == 'E' || letter == 'F') number = 3;        if(letter == 'G' || letter ==...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it returns 0 if (words == null OR words.length == 0) // returns number of times letter is the first letter of a String in words array // use equalIgnoreCase to check if letter and str.charAt(0) are equal and ignoring case }
import java.util.Scanner; public class Lab5 { public static void main(String[] args) { final char SIDE_SYMB =...
import java.util.Scanner; public class Lab5 { public static void main(String[] args) { final char SIDE_SYMB = '-'; final char MID_SYMB = '*'; Scanner scanner = new Scanner(System.in); String inputStr = ""; char choice = ' '; int numSymbols = -1, sideWidth = -1, midWidth = -1; do { displayMenu(); inputStr = scanner.nextLine(); if (inputStr.length() > 0) { choice = inputStr.charAt(0); } switch (choice) { case 'r': System.out.println("Width of the sides?"); sideWidth = scanner.nextInt(); System.out.println("Width of the middle?"); midWidth = scanner.nextInt();...
User is asked to enter a series of numbers. That input will stop when user enters...
User is asked to enter a series of numbers. That input will stop when user enters -9999. Find a maximum number from that series and a minimum number from that series. Output the location of Maximum number and minimum number.  Write a C++ program that asks the user to repeatedly input positive numbers until   -1 is pressed. Your program should print the second largest number and the count of even and odd numbers.  Write a C++ program that asks...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero or the initial reward. Functionality: After the result of the reward function is stored, this function should be called. The goal of this function is to help the robot in case it faced a lot of damage in the current step. However, this function will help the robot only if the robot’s reward was negative and the direction that the user inputted was up....
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT