Write a java class with the following two userdefined method
Write a java class with the following two user-defined methods:
Write a value-returning method named isVowel, that returns the boolean value true if a given character is a vowel, and otherwise returns the boolean value false.
Write a value-returning method named reverseDigit, that takes an int (integer) as a parameter and returns the number with its digits reversed as an int. For example, calling reverseDigit(12345) returns the int value of 54321.
Write the main method for this class to test the above two user-defined methods.
Solution
Hi, Please find my implmenetation.
Please let me know in case of any issue.
import java.util.Scanner;
public class JavaProgram {
//1. isVowel
public static boolean isVowel(char c){
// if it is not letter
if(! Character.isLetter(c))
return false;
// getting lower case representation of c
char lower = Character.toLowerCase(c);
if(c==\'a\' || c==\'e\' || c==\'i\' || c==\'o\' || c==\'u\')
return true;
else
return false;
}
public static int reverseDigit(int n){
int reverse = 0;
while(n > 0){
reverse = reverse*10 + n%10;
n = n/10;
}
return reverse;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(\"Enter a character: \");
char c = sc.next().charAt(0);
System.out.println(c+ \" is vowel: \"+isVowel(c));
System.out.print(\"Enter a number: \");
int n = sc.nextInt();
System.out.println(\"Reverse of \"+n+\" : \"+reverseDigit(n));
}
}
/*
Sample run:
Enter a character: g
g is vowel: false
Enter a number: 6543
Reverse of 6543 : 3456
*/

