Write a class that has the following methods isPrime This me

Write a class that has the following methods: isPrime: This method takes a single positive integer and checks to see if it is a prime number. The method returns true or false (boolean, not String). Use your prime number solution from A2 to help you here. vowels: This method takes a single String argument and returns the number of vowels (a, e, i, o or u) contained in the String. compoundlnterest: This method takes in all of the necessary arguments to compute and return the future value (futureValue) of an investment given the initial investment (initialValue), an interest rate (i) and a number of time periods (n). futureValue = initialValue(1 + i)^n factorial: This method takes a single integer argument (say n) a single integer. Use a for loop to compute the factorial of n. Return the computed factorial. Recall: n! = 1 times 2 times 3 times ... times n Write a main () method that coordinates all of the input and output - prompt the user for the appropriate values, call the appropriate methods and display the appropriate output. Error checking on the values is optional (you may assume the user puts in good values). Add the following method and include an appropriate call in you main() method. eEstimate: This method takes a single integer argument (say n) and returns a single double value. Once you have completed your factorial function, use it to compute an estimate of the mathematical constant E according to the following formula. Use a for loop that runs n times and sums the factors to produce your e-estimate. Try this on paper - the value of your e-estimate will increase in accuracy for a higher value of n (the argument passed into your function and used by your loop).

Solution

/**
* @author
*
*/
public class question2 {

   /**
   * @param args
   */
   public static void main(String[] args) {
       System.out.println(\" is 17 is prime ?\" + isPrime(17));
       System.out.println(\" is 14 is prime ?\" + isPrime(14));
       System.out.println(\" Number of vowel in AbcdiO :\" + vowels(\"AbcdiO\"));
       System.out
               .println(\" Compound Interest of compoundInterest(7500, 4, 2) :\"
                       + compoundInterest(7500, 4, 2));

       System.out.println(\" Factorial of 5 :\" + factorial(5));
       System.out.println(\" eEstimate(5) :\" + eEstimate(5));

   }

   /**
   * method to check the given number is prime or not
   *
   * @param number
   * @return
   */
   public static boolean isPrime(int number) {
       int i, m = 0, flag = 0;

       m = number / 2;
       for (i = 2; i <= m; i++) {
           if (number % i == 0) {

               flag = 1;
               break;
           }
       }
       if (flag == 0)
           return true;
       else
           return false;

   }

   /**
   * method to count the vowels in the string
   *
   * @param str
   * @return
   */
   public static int vowels(String str) {
       int vowelCount = 0;

       for (int i = 0; i < str.length(); i++) {
           if (str.charAt(i) == \'a\' || str.charAt(i) == \'e\'
                   || str.charAt(i) == \'i\' || str.charAt(i) == \'o\'
                   || str.charAt(i) == \'u\' || str.charAt(i) == \'A\'
                   || str.charAt(i) == \'E\' || str.charAt(i) == \'I\'
                   || str.charAt(i) == \'O\' || str.charAt(i) == \'U\') {

               vowelCount++;
           }
       }
       return vowelCount;

   }

   /**
   * method to find the compound interest
   *
   * @param principal
   * @param rate
   * @param time
   * @return
   */
   public static double compoundInterest(double principal, double rate,
           double time) {
       double compoundInterest = principal * Math.pow((1 + rate / 100), time);
       return compoundInterest;

   }

   /**
   * method to find the factorial of given value
   *
   * @param n
   * @return
   */
   public static int factorial(int n) {

       int fact = 1;
       for (int i = 2; i <= n; i++) {
           fact *= i;
       }
       return fact;
   }

   /**
   * method to find the estimate the value
   *
   * @param n
   * @return
   */
   public static double eEstimate(int n) {

       double estimateValue = 0;
       for (int i = 1; i <= n; i++) {
           estimateValue += (double) 1 / factorial(i);
       }
       return estimateValue;
   }

}

OUTPUT:

is 17 is prime ?true
is 14 is prime ?false
Number of vowel in AbcdiO :3
Compound Interest of compoundInterest(7500, 4, 2) :8112.000000000001
Factorial of 5 :120
eEstimate(5) :1.7166666666666668

 Write a class that has the following methods: isPrime: This method takes a single positive integer and checks to see if it is a prime number. The method return
 Write a class that has the following methods: isPrime: This method takes a single positive integer and checks to see if it is a prime number. The method return
 Write a class that has the following methods: isPrime: This method takes a single positive integer and checks to see if it is a prime number. The method return

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site