Java Static Initializer Block

Sort by

recency

|

1428 Discussions

|

  • + 0 comments

    JAVA SOLUTION : import java.io.; import java.util.;

    public class Solution {

    public static int calcularArea(int a,int b) throws Exception{
        if (a <= 0 || b<= 0) {
            throw new Exception("java.lang.Exception: Breadth and height must be positive");
        }
        else{
        return (a*b);
        }
    }
    
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        Scanner scanner = new Scanner (System.in);
    
        try{
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            System.out.println(calcularArea(a,b));
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
        scanner.close();
    }
    

    }

  • + 0 comments
    public class Solution {
        
    public static int B;
    public static int H;
    public static boolean flag = printArea();
    
    public static boolean printArea() {
        Scanner sc = new Scanner(System.in);
        // System.out.println("Enter two numbers between -100 and 100");
        B = sc.nextInt();
        H = sc.nextInt();
        sc.close();
            Solution.flag = (B<=0 || H<=0)? false: true;
            if(!flag){
                System.out.println( "java.lang.Exception: Breadth and height must be positive");
            }
            return Solution.flag;
    }
    
    public static void main(String[] args){
    		if(flag){
    			int area=B*H;
    			System.out.print(area);
    		}
    
    }//end of main``
    }//end of class
    
    
    
    
    
  • + 0 comments

    import java.io.; import java.util.; class soln{ static{ Scanner ob=new Scanner(System.in); int B=ob.nextInt(); int H=ob.nextInt(); int area=B*H;

      if(H<=0 || B<=0) 
      System.out.println("java.lang.Exception: Breadth and height must be positive");
      else
      System.out.println(area);  
    }
    }
    

    public class Solution { public static void main(String[] args) { soln ob1= new soln();

    } }

  • + 0 comments

    import java.io.; import java.util.;

    // Code in JAVA 8

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=  new Scanner(System.in);
        int B= sc.nextInt();
        int H = sc.nextInt();
        try{
            if (B <0 || B>=100 && H <0 || H >=100 )
            {
                throw(new Exception("java.lang.Exception: Breadth and height must be positive"));
            }
            int area = B*H ;
            System.out.println(area);
        }
    
        // call message / Exception 
        catch (Exception e )
        {
            System.out.println(e.getMessage());
        }
    
    
    }
    

    }


  • + 0 comments

    import java.io.; import java.util.;

    // Code in JAVA 8

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=  new Scanner(System.in);
        int B= sc.nextInt();
        int H = sc.nextInt();
        try{
            if (B <0 || B>=100 && H <0 || H >=100 )
            {
                throw(new Exception("java.lang.Exception: Breadth and height must be positive"));
            }
            int area = B*H ;
            System.out.println(area);
        }
    
        // call message / Exception 
        catch (Exception e )
        {
            System.out.println(e.getMessage());
        }
    
    
    }
    

    }