
WHAT IS A HOLLOW SQUARE STAR PATTERN?
HOLLOW SQUARE STAR PATTERN means the box will be created with stars but only the border of the box will be there all inside of the box will be empty. This is called the hollow square star pattern. to make it we will write some code as output we will get that hollow square star pattern.
code:
import java.util.Scanner; public class SqareLeaveMiddleStar { public static void main(String[] args) { System.out.println("enter no of rows and coloumns"); Scanner sc = new Scanner(System.in); int rows=sc.nextInt(); int coloumns=sc.nextInt(); for(int i=1;i<=rows;i++) { for(int j=1;j<=coloumns;j++) { if((i==1 & j<=coloumns)||(j==coloumns & i<=rows)||(i==rows & j<=coloumns)||(j==1 & i<=rows)) { System.out.print("*"); } else { System.out.print(" "); } } }
example : initialization condition increment for( int i=0; i<=10 ; i++ ) loop body System.out.println(i); }
In "initialization" the 'i' variable will be initialized with zero and then control will go to "condition" condition will be checked if 'i' is greater than or equal to or ten or not. If the condition is evaluated as true then control will go inside the loop body which is "loop body" and after executing the loop body control will go to "increment" therein "increment" increment operator will work and 'i' variable will be incremented with '+1' and again control will go to "condition" and condition will be checked if again condition is evaluated as true then again control will go to into "loop body" body of for loop and execution will happen, and this "condition" and "increment" and "loop body" operations will happen till that the condition which is available in "condition" get false, after that condition becomes false, the loop will be terminated.
2. LOGICAL OR OPERATOR(||)
IN logical or (||) operator it needs two operands/expression/conditions and the behavior of OR(||) operator is it will check two operands of it if one of both operands is true then it will return true and if both of the operands are true it will also return true but if both operands evaluated as false then only it returns false.
In logical or operator another concept comes to the frame and that is short-circuit.
EXAMPLE OF OR(||) : expression 1 expression 2 Int x= 5; x>2 || x>10
So what is short-circuit:
We know that or(||) operator will return true if one or both operands are evaluated as true. but this operator will return true if the “expression 1”(left side) is evaluated as true. It will not even check “expression 2” (right side) whether it is true or false. It will stop the evaluation and return true if “expression 1” is returning true. So this behavior is called SHORT-CIRCUIT.
3. Scanner class
Scanner class is used to take input from users .. for taking input from user we need to create an object of class Scanner and by using Scanner class object we will call some different methods for taking different types of data. Difference type of data means :
1 ) integer = nextInt();
2) String =nextLine();
3) double =nextDouble();
4) char =.next().charAt(0)
5) long = nextLong();
6) boolean =nextBoolean();
The scanner class is available in java.util package. so for using the Scanner class
The 1st step is we need to import that class from java.util package
By writing: import java.util.Scanner;
“Import java.util.Scanner” by writing this we are only importing Scanner class from java.util package. But if we write “ Import java.util.*; ” then what all classes are available in java.util package all will be imported.
Example : Scanner sc=new Scanner(System.in); Int a=sc.nextInt();
System.in
“System” is class and “in” is a static variable that is available in System class and “in” inputStream type variable and System.in tells that take input from keyboard.
Code Explanation :
System.out.print("*")
}
: here in these lines, we are saying that there is an if condition, and inside if condition we have written some condition if the condition evaluates as true then System. out. println("*"); will execute and will print '*'
System.out.print(" ")
}: here in this code we are saying if the above 'if' part is not working then this else part will work and this else part will print space.