In this tutorial, we’ll learn how to code Java Static Initializer Block Hackerrank Solution With Explanation. If you’re wondering how to solve the Java Static Block Initializer Block Hackerrank Solution, then you’re at the right place. Here you’ll get all the Hackerrank solutions with an explanation. But before getting started we’ll let you know about Hackerrank. So,
Table of Contents
Hackerrank is a tech company that mainly focuses on competitive programming challenges for students, companies, and consumers. Hackerrank was founded by two founders Vivek Ravishankar and Harishankar Karunanidhi in 2012. Its headquarters are situated in California, United States.
With the help of this platform students as well as professionals prepare themselves for the placements by practicing the coding problems. The questions that hacker rank contains are asked in the interview rounds of many companies.
So let us see how to code Java Static Initializer Block Hackerrank Problem with an Explanation.
The static initialization blocks are executed when the class is loaded, and you can initialize static variables in those blocks.
It’s time to test your knowledge of Static initialization blocks. So, you can read about it by clicking on this link.
You’re given a class Solution with the main method. Complete the given code so that it will display the output of the area of a height H. You should read the variables from the standard input.
If B <=0 or H <=0, the outputs should be “java.lang.Exception: Breadth and height must be positive” without quotes.
Input Format:
So, there are two lines of the input. The first line contains the B: the breadth of the parallelogram. And the next line contains H: the height of the parallelogram.
If both, values are greater than zero, then the main method must output an area of the parallelogram. If not then it should print “java.lang.Exception: Breadth and the height must be positive“. without quotes.
1
3
3
-1
2
java.lang.Exception: Breadth and height must be positive
Now we’ll see the solution for the above problem using different approaches.
//Learnprogramo - programming made simple import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Main { static boolean flag = true; static int B,H; static{ Scanner scan = new Scanner(System.in); B = scan.nextInt(); scan.nextLine(); H = scan.nextInt(); scan.close(); if(B>0 && H>0) { flag = true; }else if(B<=0 || H<=0) { flag=false; System.out.println("java.lang.Exception: Breadth and height must be positive"); } } public static void main(String[] args) { if(flag){ int area=B*H; System.out.print(area); } } }
Output:
Explanation:
In the above java program, we’ve to enter the breadth and the height of the parallelogram and then print the area of the parallelogram. When the user will compiler the above program the compiler will ask the user to enter the breadth and height of the parallelogram and then display the area on the screen. And if the breadth entered by the user is negative then the compiler should print an exception i.e “java.lang.Exception: Breadth and height must be positive.
//Learnprogramo - programming made simple import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Main { static Scanner sc = new Scanner(System.in); static int B = sc.nextInt(); static int H = sc.nextInt(); static boolean flag = initializeInstanceVariable(); protected static boolean initializeInstanceVariable() { boolean flag = false; if(B>0&&H>0){ flag = true; } else{ System.out.println("java.lang.Exception: Breadth and height must be positive"); flag=false; } return flag; } public static void main(String[] args) { if(flag){ int area=B*H; System.out.print(area); } } }
Output:
So, in this way we’ve learned how to code java static initializer block Hackerrank solution with an explanation. If you’ve any doubts then please feel free to contact us. We’ll reach you as soon as possible.
Thanks and Happy Coding :)
Also Read:
This website uses cookies.