Area of a Parallelogram-Hacker Rank Problem.

You are given a class Solution with a main method. Your task is to complete the given code so that it outputs the area of a parallelogram with breadth  and height . You should read the variables from standard input.
If  or  , the output should be "java.lang.Exception: Breadth and height must be positive" without quotes.
Input Format
Two lines of input. First line contains : breadth of parallelogram. Next line contains : height of parallelogram.
Constraints 
 
Output Format
If both values are greater than zero, then the main method must output the area of the parallelogram; else, print "java.lang.Exception: Breadth and height must be positive" without quotes.
Sample input 1
1
3
Sample output 1
3
Sample input 2
-1
2
Sample output 2
java.lang.Exception: Breadth and height must be positive
Solution :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
//Write your code here
static boolean flag=false;
static int B,H;
static{
 Scanner in= new Scanner(System.in);
 B=in.nextInt();
 H=in.nextInt();
 try{
  if(B<=0||H<=0)
   throw new Exception("Breadth and height must be positive");
  else{
   flag= true;
  }
 }
 catch(Exception e){
  System.out.println(e);
 }
}                                                                   public static void main(String[] args){
  if(flag){
   int area=B*H;
   System.out.print(area);
  }
  
 }//end of main

}//end of class

No comments:

Post a Comment

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...