Respuesta :
Answer:
Here is the JAVA program.
import java.util.Scanner; //Scanner class to take input from user
public class HollowFilledSquare {
public static void main(String[] args) { //start of main() function body
Scanner sc= new Scanner(System.in); // Scanner class object
System.out.print("Enter an integer to display a filled and hollow square "); //prompts user to enter an integer
int n = sc.nextInt(); // reads integer input from the user
StringBuilder filled = new StringBuilder(n);
// creates objects filled and hollow of class StringBuilder
StringBuilder hollow = new StringBuilder(n);
for (int i = 1; i <= n; i++) { // outer loop for length of the square
for (int j = 1; j <= n; j++) { // inner loop for width of square
filled.append("*"); //displays asterisks
if (i == 1 || i == n || j == 1 || j == n) // condition to display stars
{hollow.append("*");}
else
{ hollow.append(" "); } } // to display empty spaces
System.out.println(filled + " " + hollow);
// places hollow and filled squares next to each other
filled. delete(0, filled.length());
//removes characters from 0 to the length of filled square
hollow. delete(0, hollow.length());
//removes characters from 0 to the length of hollow square } } }
Explanation:
The program first prompts the user to enter an integer. It uses outer loop for length of the square and inner loop for width of square in order to display the asterisks. append () function is used to display the sequence of asterisks. delete () function removes the asterisks from the sequence of asterisks. String Builder is a class used for to create a string of n characters. It basically works like String but this is used to create modifiable objects.
The screen shot of the code along with the output is attached.