Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new project that contains a class. This class contains a public static

ID: 3641681 • Letter: C

Question

Create a new project that contains a class. This class contains a public static void main method.
This program will output a shape as the following. Your program will ask the user to enter the
dimensions of the outer rectangle, and the dimensions of the inner rectangle. The following
shape has 5 by 10 for outer rectangle and 3 by 4 for inner rectangle. Try to make the inner
rectangle in the center of the outer rectangle as possible. In certain configuration, it is impossible
to center the inner rectangle due to text output mode. In that case, off by 1 is okay.

Explanation / Answer

import java.util.Scanner;

public class RectangleInRectangle {
  
    public static void main(String[] args) {
        /**
         Outer rectangle: H1, W1
         Inner rectangle: H2, W2
        
              ****************     case 1
              *              *        case 2
              *              *        case 2
              *              *        case 2
         SR   *LB ******* RB *           case 3
              *   *     *    *              case 4
              *   *     *    *              case 4
              *   *     *    *              case 4
         ER   *   *******    *           case 3
              *              *        case 2
              *              *        case 2
              ****************     case 1

         Constants:
            SR = H1 / 2 - H2 / 2;    (Start row of inner rectangle)
            ER = SR + H2 - 1;        (End row of inner rectangle)
            LB = (W1 - 2 - W2) / 2; (Left blank width)
            RB = W1 - 2 - W2 - LB;   (Right blank width)
         *If one of the constants above is negative then the program
         cannot print the shape
          
         case 1: print (W1) asterisks
         case 2: print first asterisk
                 print (W1 - 2) spaces
                 print last asterisk
         case 3: print first asterisk
                 print (LB) spaces
                 print (W2) asterisks
                 print (RB) spaces
                 print last asterisk
         case 4: print first asterisk
                 print (LB) spaces
                 print second asterisk
                 print (W2 - 2) spaces
                 print third asterisk
                 print (RB) spaces
                 print last asterisk
         */
      
        Scanner keyboard = new Scanner(System.in);
        int outHeight;
        int outWidth;
        int inHeight;
        int inWidth;
        int startRow;
        int endRow;
        int leftBlank;
        int rightBlank;
      
        System.out.println("Enter the dimensions of the outer rectangle:");
        System.out.print("Height: ");
        outHeight = keyboard.nextInt();
        System.out.print(" Width: ");
        outWidth = keyboard.nextInt();
      
        System.out.println("Enter the dimensions of the inner rectangle:");
        System.out.print("Height: ");
        inHeight = keyboard.nextInt();
        System.out.print(" Width: ");
        inWidth = keyboard.nextInt();
      
        startRow = outHeight / 2 - inHeight / 2;
        endRow = startRow + inHeight - 1;
        leftBlank = (outWidth - 2 - inWidth) / 2;
        rightBlank = outWidth - 2 - inWidth - leftBlank;
      
        System.out.println();
        if (startRow >= 0 && endRow >= 0
            && leftBlank >= 0 && rightBlank >= 0
            && outHeight > 0 && outWidth > 0
            && inHeight > 0 && inWidth > 0)
        {
            for (int row = 0; row < outHeight; row++)
            {
                if (row == 0 || row == outHeight - 1) //case 1
                {
                    for (int col = 0; col < outWidth; col++)
                        System.out.print('*');
                }
                else if (row < startRow || row > endRow) //case 2
                {
                    System.out.print('*');
                    for (int col = 0; col < outWidth - 2; col++)
                        System.out.print(' ');
                    System.out.print('*');
                }
                else if (row == startRow || row == endRow) //case 3
                {
                    System.out.print('*');
                    for (int col = 0; col < leftBlank; col++)
                        System.out.print(' ');
                    for (int col = 0; col < inWidth; col++)
                        System.out.print('*');
                    for (int col = 0; col < rightBlank; col++)
                        System.out.print(' ');
                    System.out.print('*');
                }
                else //case 4
                {
                    System.out.print('*');
                    for (int col = 0; col < leftBlank; col++)
                        System.out.print(' ');
                    System.out.print('*');
                    for (int col = 0; col < inWidth - 2; col++)
                        System.out.print(' ');
                    System.out.print('*');
                    for (int col = 0; col < rightBlank; col++)
                        System.out.print(' ');
                    System.out.print('*');
                }
                System.out.println();
            }
        }
        else
        {
            System.out.println("Invalid rectangles or outer rectangle "
                    + "is not large enough to hold inner rectangle");
        }
    }
}

Sample run:

Enter the dimensions of the outer rectangle:
Height: 7
Width: 10
Enter the dimensions of the inner rectangle:
Height: 3
Width: 4

**********
*        *
* **** *
* * * *
* **** *
*        *
**********

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote