Sort by

recency

|

296 Discussions

|

  • + 0 comments

    class MyCalculator {

    long power(int n, int p) throws Exception{
        if(n<0||p<0){
            throw new Exception("n or p should not be negative.");
        }
        else if(n==0 && p== 0){
            throw new Exception("n and p should not be zero.");
        }
        return (long)Math.pow(n,p);
    
    }
    

    }

  • + 0 comments
    import java.util.Scanner;
    
    public class Solution {
    
        private static MyCalculator myCalculator = new MyCalculator();
        private static Scanner scanner = new Scanner(System.in);
    
        static class MyCalculator {
            public long power(int n, int p) throws Exception {
                if (n < 0 || p < 0) {
                    throw new Exception("n or p should not be negative.");
                }
                if (n == 0 && p == 0) {
                    throw new Exception("n and p should not be zero.");
                }
    
                return (long) Math.pow(n, p);
            }
        }
    
    
        public static void main(String[] args) {
            while (scanner.hasNextInt()) {
                try  {
                    int x = scanner.nextInt();
                    int y = scanner.nextInt();
                    long result = myCalculator.power(x, y);
                    System.out.println(result);
                } catch (Exception e) {
                    System.out.println(e);
                }
    
            }
    
        }
    
    }
    
  • + 0 comments

    The exception messages expected in the results are not consistent with what the documentation says, for this problem.

  • + 0 comments
    import java.util.Scanner;
    class MyCalculator {
        /*
        * Create the method long power(int, int) here.
        */
        public static  int power(int n , int p) throws Exception{
                if(n == 0 && p== 0){
                    throw new Exception("n and p should not be zero.");
                }
                else if(n < 0 || p < 0){
                    throw new Exception("n or p should not be negative.");
                } 
                else{
                    return (int) Math.pow(n, p);
                }            
        }
        
    }
    
  • + 0 comments

    class MyCalculator {

        /*
    * Create the method long power(int, int) here.
    */
    
    public static long power(int n, int p) throws Exception{
      int c = 1;  
    
      if(n < 0 || p < 0){
         throw new Exception("n or p should not be negative.");
      }
      if(n == 0 && p == 0){
        throw new Exception("n and p should not be zero.");
      }
    
      if(p >= 0){
      for(int i = 0; i < p; i++){
        c = c * n;
      }
      }
    
      return c;
    }
    

    }