Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6696853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:23:26+00:00 2026-05-26T06:23:26+00:00

I have been tasked with the assignment of printing a rectangle. The user will

  • 0

I have been tasked with the assignment of printing a rectangle. The user will input length, width, and a symbol of their choice to be printed. From this the rectangle will be outputted. This portion of the assignment is done. My next task is to print a rectangle from negative values which will only print an outline of the rectangle (inside area is blank). I am having difficulty creating an algorithm for which to use in this situation.

Any help would be appreciated.

Cheers.

   import java.util.Scanner;

    public class Rectangle
{
public static void main(String[] args)
{

    boolean loopExecuted;
    String userContinue = "NO";

    // Loop to ask to do another rectangle
    do 
    {

    // Variables
    int length;
    int width;
    String symbol;
    char symbolz;
    int xLength;
    int yWidth;


                    // Name Scanner
    Scanner input = new Scanner(System.in);

                    // What program will do
    System.out.println("This program will print a rectangle with the symbol of your choice.  If you enter a positve integer the rectangle"
            + "will be filled, if you enter a negative integer, it will print the outline of the rectangle.\n");


                    // Ask for user input and check for validity

        do 
        {
           System.out.println("Please enter the length of the rectangle that is less than or equal to -10 and less than or equal to 10.");
           length = input.nextInt();
        }
        while (length < -10 || length > 10 || length == 0);

        do
        {
           System.out.println("Please enter the width of the rectangle that is less than or equal to -10 and less than or equal to 10.");
           width = input.nextInt();
        }
        while (width < -10 || width > 10 || width == 0);


        System.out.println("Please enter the symbol to be used for the rectangle");
        symbol = input.next();
        symbolz = symbol.charAt(0);

        System.out.println("You have entered the following values for length, width, and symbol: " + length + " ," + width + " ," + symbolz +"\n");


                 // Algorithm to print filled in rectangle.

        for (yWidth = 1; yWidth <= width; yWidth++) {
            for (xLength = 1; xLength <= length; xLength++) {
                System.out.print(symbolz);
            }

            System.out.println(" ");
        }

                // Algorithm to print outline of rectangle

        //TODO 
        for(yWidth = 1; yWidth >= width; yWidth--){
            for (xLength = 1; xLength >= length; xLength--){
                System.out.print(symbolz);
            }

            System.out.println(" ");
        }


                    // Repeat the program   

     loopExecuted = false;  
   do
           {
               if (!loopExecuted)
               {
                    System.out.println("\n\nWould you like to continue? Please either Yes or No");
               }
               else
               {
                   System.out.println("Please enter a valid response (Yes / No)");
               }
               userContinue = input.next();
               userContinue = userContinue.toUpperCase();
               loopExecuted = true;
           }

   while (!"YES".equals(userContinue) && !"NO".equals(userContinue));

   // Case Insensitive

    userContinue = userContinue.toUpperCase();

    } 
    while (userContinue.equals("YES"));

    }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:23:27+00:00Added an answer on May 26, 2026 at 6:23 am

    I’ll answer you’re question but first I’ll start by making a suggestion: use functions! Break your one big main into functions. Breaking code into smaller functions helps to keep it more understandable and easier to maintain (especially to find stuff). Everywhere you use a comment would be a good function. For example, getInput(), printFilledRectangle(int width, int height, char symbol), etc.

    Now for your question:

    Think about the rules for printing a non-filled rectangle. It seems to me that there are three:

    1) If this is the first row, print the symbol n-times (where n = width)
    2) If this is the last row, print the symbol n-times
    3) Else print one symbol, print n-2 spaces, and then print another symbol

    So now incorporate these rules into your non-filled loops. Hope that helps.

    EDIT:

    Ok – well I can get you started

     for (row = 0; row < height; row++)
         if (row == 0 || row == height - 1)  // first or last row
            // print your symbol n times, where n = width
         else // otherwise, this is an "internal" row
            // print 1 symbol, n-2 spaces, then 1 symbol again
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tasked with creating a program that will generate an amortization schedule.
I have been tasked with creating a program what will create take files in
I have been tasked to write a script that will enable tcp and named
I have been tasked with estimating the amount of time it will take to
I have been tasked with a project which will entail multiple apps, a master
I have been tasked with implementing a PKI library in C# for a company
I have been tasked with going through a number of ColdFusion sites that have
I have been tasked with coming up with a compatibility guide for SharePoint 2007
I have been tasked to optimize some sql queries at work. Everything I have
I have been tasked with developing a solution that tracks changes to a database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.