Write a Java program, RectangleOne, to take input from the user and print a special rectangle. We will do this step-by-step, refactoring at each step. This is usually a good software development strategy, especially to control development risks.

Step 1) Write a Java program to print a solid rectangle, six lines of five asterisks ("*")

The output should look like

This gives us a quick and easy start on the program with most of the software structure and its multiple-line behavior. We develop the main part of the program's "architecture" in this step.

1) Name the program RectangleOne
2) Define static class constants for the width and height: static final int WIDTH = 5; static final int HEIGHT = 6;
3) Structure the program to use a method, "writeALine(int width)" that writes one line of width "width": static void writeALine(int width) { }
4) Add a loop in the "main" method to call writeALine(), name the loop variable "lineNumber": for (int lineNumber = 1; lineNumber <= HEIGHT; lineNumber++) { }
5) Call writeALine(WIDTH) from within the loop.
6) Run the program and confirm that you have the program running properly before proceeding to the next step

Step 2) Modify RectangleOne to let the user specify the width and height

Why? Most software gets input from its users.

1. Use the scanner class within the "main" method to get the width and height from the user. The program should first ask the user for the width, then get the width, then ask for the height, then get the height.
2. Name the variables "width" and "height". Assume that the user always enters values of 2 or more for both width and height.
3. Remove the static class constants WIDTH and HEIGHT, and use "width" and "height" in their place within the "main" method.
4. Run the program and confirm that the program prints the proper pattern before proceeding to the next step
5. You can use the Scanner Example, below, to see how to use the scanner.
6. Run the program and confirm that you have the program running properly before proceeding to the next step

Scanner Example to Input Two Integer Values

Here is an example Java program that asks the user for two integer values

import java.util.Scanner;
public class BasicScanner {
   public static void main( String[] args ) {
      int valueA;
      int valueB;
      // Get the values from the user
      Scanner sc = new Scanner(;
      System.out.println("Please enter an integer value A:");
      valueA = sc.nextInt();
      System.out.println("Enter Value B:");
      valueB = sc.nextInt();
      System.out.println("Value A= " + valueA + "; Value B= " + valueB);
• The import statement goes at the top of the file
• The variable sc is "an instance of a scanner" because the "new" expression creates one and a reference to that newly created Scanner is assigned to the variable "sc"
• nextInt() does the work. It gets the input from the user
• nextInt() can be put into a loop
• You still need to ask the user for input before calling nextInt()
• The Scanner class supports other kinds of input

Step 3) Modify the program to print only the outline of the rectangle
*    *
*    *
*    *
*    *


This introduces a static "helper" method to print consecutive spaces. You will need to "delegate" parts of problems to helper methods, if you want to have a chance at solving complex problems. You will also need to be able to develop software step-by-step, making each step work and then modifying and extending the code for the next step.

1. Modify and use writeALine() to write only the top an bottom lines.
2. Define a static "helper" method, "printSpaces(int count)" to print "count" consecutive spaces (no new line), e.g., printSpaces(17) prints 17 consecutive spaces. Use System.out.print(), but not println(). This way other characters can be added on the line, before or after calling printSpaces()static void printSpaces(int count) { }
3. Write a static method writeInsideLine(int width) to write the other (not top, not bottom) lines. writeInsideLine() should use printSpaces()
4. Use the correct line numbers when calling writeInsideLine(): run its line number loop from 2 through (height-1). This makes it easier for the reader to understand the program.
5. Run the program and confirm that you have the program running properly before proceeding to the next step

Step 4) Modify RectangleOne to print square with a diagonal line within it

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


We use calculations to determine how many spaces to print

1. Do not ask the user for the width, but instead set it to the height that the user specifies (so the rectangle will be a square)
2. Modify the static method writeInsideLine() to take two parameters:
3. static void writeInsideLine(int lineNumber, int width)
4. Number the lines 1 through height. (remember, here height = width)
5. The first internal line number is two (2).
6. Note that the number of spaces between the left vertical border and the diagonal is lineNumber - 2
7. The number of spaces after the diagonal, between the diagonal and the right vertical border is height (lineNumber + 1)

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

Part 1- Write a Java program to print a solid rectagle, six lines of five.
Part 2-Modify RectangleOne to let the user specify the width and height.
Part 3-Modify the program to print only the outline of the rectangle.
Part 4-Modify RectangleOne to print a square with a diagonal line.

// Import Scanner module
import java.util.Scanner;

* RectangleOne class
public class RectangleOne {

//static final int WIDTH = 5; //Part 1
//static final int HEIGHT = 6; //Part 1

// Main method for program execution
public static void main(String[] args) {
    int width; // Variable to store width of rectangle
    int height; // Variable to store height of rectangle
    // Collect the number of lines from the user
    Scanner sc = new Scanner(; // Scanner to handle console input
    //System.out.println("Please enter the width you wish to see:");
    // width = sc.nextInt(); // Query width from user // Part 2,3
    System.out.println("Please enter the height you wish to see:" );...

By purchasing this solution you'll be able to access the following files:

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats