I use the following methods to figure out the solutions for the Question 4.26:-
1. Algorithm
2. Replacing the asterisks with the number in the loop to understand the loop process
* Note: If the entered size is 1 or 2, the hollow square cannot be created although the question requires the size range from 1 to 20.
Algorithm

Hollow Square Example
while i less than or equal to 5
print R1C1
// doing last row
if i reaches to R5C1
initialize j to 1
while j less than 5
print R5C2 (*Note: print R5C2 to R5C5 after each increment of j)
increment j to 1
end while
end if
// end of doing last row
// doing first row
while j less than 4
print R1C2 (*Note: print R1C2 to R1C4 after each increment of j)
increment j to 1
if j more than 3 (*Note: start looping from R1C2, when j more than R1C4)
print R1C5
end if
end while
// end of doing first row
// doing middle row
if i more than R1C1 and i less than 5
reset j to 1
while j less than or equal to 3
print the empty space
increment 1 to j
if j equals to C5
print R2C5 (*Note: Print R2C5 to R4C5 when each row reach C5)
end if
end while
end if
// end of doing middle row
increase 1 to i
go to next row
end while
package Exercises;
import java.util.Scanner;
public class HollowSquare {
public static void main(String[] args) {
int size;
int i = 1;
int j = 1;
Scanner input = new Scanner(System.in);
System.out.println(”Enter the side of a square between 3 to 20: “);
size = input.nextInt();
while (i <= size) {
System.out.print(”i:”+i);
if (i == size) {
j = 1;
while (j < size) {
System.out.print(”j:”+j);
j++;
}
}
while (j size - 2) {
System.out.print(”j:”+j);
}
}
if (i > 1 && (i < size)) {
j = 1;
while (j <= size - 2) {
System.out.print(”j:”+j);
j++;
if (j == size - 1) {
System.out.print(”j:”+j);
}
}
}
i++;
System.out.println();
}
}
}
Results:
Enter the side of a square between 3 to 20:
5
i:1j:1j:2j:3j:4
i:2j:1j:2j:3j:4
i:3j:1j:2j:3j:4
i:4j:1j:2j:3j:4
i:5j:1j:2j:3j:4
/* Exercise 4.26
* 1. Prompts the user to enter the size of the side of a square.
* 2. Display a hollow square of that size made of asterisks.
* 3. It should work for squares of all side lengths between 1 and 20.
*/
package Exercises;
import java.util.Scanner;
public class HollowSquare {
public static void main(String[] args) {
// initializing variables in declarations
int size;
int i = 1;
int j = 1;
// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);
// prompt for input and read the value
System.out.println(”Enter the side of a square between 3 to 20: “);
size = input.nextInt();
/* Input validation
* if input size is less than 3, negative number, or more than 20
* prompt for input again until the input is valid
*/
while ((size < 3) || (size > 20)) {
System.out.println(”Invalid size.Please enter the size between 3 to 20: “);
size = input.nextInt();
}
//while row not reaches the size
while (i <= size) {
/* 1. print ‘*’
* 2. increase 1 to i
* 3. go to next row
*/
System.out.print(”*”);
// when last row = size
if (i == size) {
// reset j to 1
j = 1;
/* when j less than size
* print “*”
* increment 1 to j
* last row, start print from second column
*/
while (j < size) {
System.out.print(”*”);
j++;
} // end while
} // end if
/* first row, start print from second column
* print “*”
* increment 1 to j
*/
while (j < size - 1) {
System.out.print(”*”);
j++;
/* first row, last column
* minus 2 asterisks of the first row (excluded the first row, first column)
* once more than size, print “*”
*/
if (j > size - 2) {
System.out.print(”*”);
} // end if
} // end while
// when reach second row and still less than the size
if (i > 1 && (i < size)) {
// reset j to 1
j = 1;
/* while column less than (size -2)
* minus 2 asterisks from the first column and last column
* display ” “
* increment 1 to j
*/
while (j <= size - 2) {
System.out.print(” “);
j++;
/* when column reaches the size
* minus the first column
* print “*” for last column
*/
if (j == size - 1) {
System.out.print(”j”);
} // end if
} // end while
} // end if
// increment 1 after first row
i++;
// go to next line
System.out.println();
} // end while
} // end main
} // end class