E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconHollow Square Star PatternSepratorIcon

Hollow Square Star Pattern

Han Solosrikanta kumar behera
calendar13 May 2022
poster

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(" ");
}
 }
}
  •  TO DO THIS PROGRAM WE NEED ALL CONCEPTS LIKE:
  1. For loop(for(int i=0;i<10;i++))
  2. Logical or operator(||)
  3. Scanner class                    

     FOR LOOP 

    In for loop, there are four parts :
  1.  Variable initialization
  2.  Condition
  3.  Increment / decrement operator
  4.  Loop body
 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.println("enter number of rows and columns");:  Here in this line of code we are giving a message to the user to enter the number of rows and columns.
  • Scanner sc = new Scanner(System.in);: in this line of code we are creating an object of the Scanner class
  •  int rows=sc.nextInt(); : in this line we taking input for number of rows..
  •  int columns=sc.nextInt(); : in this line we taking input for number of columns
  •  for(int i=1;i<=rows;i++) { : this is the outer for loop where we are defining how many rows will be there in our program.. or we can say here we are defining how many times the inner loop will work.
  •  if((i==1 & j<=columns)||(j==columns & i<=rows)||(i==rows & j<=columns)||(j==1 & i<=rows)) {

 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 '*'

  •      else {

       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.

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML

copycopycopycopy

Han Solo

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML