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. You can read about it here.
You are given a class Solution with a main method. Complete the given code so that it outputs the area of a parallelogram with breadth and height . You should read the variables from the standard input.
If or , the output should be "java.lang.Exception: Breadth and height must be positive" without quotes.
Input Format
There are two lines of input. The first line contains : the breadth of the parallelogram. The next line contains : the height of the parallelogram.
Constraints
Output Format
If both values are greater than zero, then the main method must output the area of the parallelogram. Otherwise, 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
//Write your code here
static int B;
static int H;
static boolean flag = true;
static {
Scanner sc = new Scanner(System.in);
B = sc.nextInt();
H = sc.nextInt();
if(B<=0 || H<=0){
System.out.println("java.lang.Exception: Breadth and height must be positive");
flag = false;
//return;
}else{
//System.out.println(H);
}
}
public static void main(String[] args){
if(flag){
int area=B*H;
System.out.print(area);
}
}//end of main
}//end of class
}//end of class
This comment has been removed by the author.
ReplyDeletewhy we have to initialize everything as static?
ReplyDeletewhy we cant get same output without it
Because static variables are used independently
DeleteSolution.java:40: error: reached end of file while parsing
ReplyDelete}//end of class
^
1 error..................showing this type of error please help
take off the last one brace at the end line of code
Deletepublic class Solution { is already present in the code, see that if you havent pasted it twice.
Deleteit is shown compiler error so what i do
ReplyDeleteimport java.io.*;
ReplyDeleteimport java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int B;
static int H;
static boolean flag=true;
static{
Scanner sc= new Scanner(System.in);
B=sc.nextInt();
H=sc.nextInt();
if(B<=0 || H<=0)
{
System.out.println("java.lang.Exception: Breadth and height must be positive");
flag=false;
}
}
public static void main(String[] args){
if(flag){
int area=B*H;
System.out.print(area);
}
}//end of main
}//end of class
-> USE THIS CODE!! IT MIGHT HELP
Nice article and explanation Keep continuing to write an article like this you may also check my website
ReplyDeleteHotspot Shield Crack
Foxit PhantomPDF Crack
StudioLinked VST Crack
Synapse Audio The Legend Crack
Halftime VST Crack
nice information thanks for shring............................!
ReplyDeletespring boot certification course training