Answer to Question #306524 in Java | JSP | JSF for JAY

Question #306524

Control Structures: As a build-up from the previous question, consider 3 possible scenarios of input: A character, number, or word. Your program should then do the following:

  • In case the input is a character it should indicate if it’s a vowel or not
  • when a number is entered then it’s should check if it’s a prime
  • Finally, if a word is entered the system should check if it’s a palindrome or not.
1
Expert's answer
2022-03-05T13:53:53-0500
import java.math.*;

import java.io.*; 

import java.util.*;

import java.lang.StringBuilder;




public class ControlStructures{




  public static void main(String args[]) {




  // checking for vowels

  if(args[0].matches("[aeiouy]")){




   System.out.println("it’s a vowel");

   

  // checking for number

  }else if(args[0].matches("\\d+")){




    int number = Integer.parseInt(args[0]);




    BigInteger bigInteger = BigInteger.valueOf(number);

     

    // checking for prime number

    boolean probablePrime = bigInteger.isProbablePrime((int) Math.log(number));




    if(probablePrime){




     System.out.println("it’s a prime");




    }else{




     System.out.println("it’s not a prime");




    }




  // checking for word

  }else if(args[0].length() > 1){




    // reverse word then compare without upper case

   int boolPalindrome = args[0].compareToIgnoreCase(new StringBuilder(args[0]).reverse().toString());




    

  // checking for palindrome from result

   if(boolPalindrome == 0){




    System.out.println("It's a palindrome");




   }else{




    System.out.println("It's not a palindrome");




   }




  }else{




   System.out.println("it’s not a vowel");




  }




 }




}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS