Let’s take a simple program idea and refine it over the course of a few versions of the code.
The program we are writing is meant to print a box made of asterisks. The width and height of the box is based on a value entered by the user. For example, if the user enters 3, the program would display the following:
***
***
***
However, if the user enters 5, the program would display the following:
*****
*****
*****
*****
*****
Version 1.0
In this version of the code, we are going to use nested loops to print the box. Here is the pseudocode:
Declare a Scanner named s
Declare an integer named size (used for size of rectangle)
Prompt the user to enter size of rectangle
size = read integer from the keyboard
loop from 0 to size - 1:
loop from 0 to size - 1:
print 1 asterisk without newline
print a new line
And here’s the code.
import java.util.*; public class StarsV01 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int size = 0; System.out.println("This program will print a rectangle based on the value you enter."); System.out.print("Enter the size --> "); size = s.nextInt(); // The outer loop handles each line of output, as in the number of lines to // output for (int i = 0; i < size; i++) { // the inner loop handles what is printed on each line of the output for (int j = 0; j < size; j++) { System.out.print("*"); } System.out.println(); } } }
Here’s the output of running the program:
This program will print a rectangle based on the value you enter.
Enter the size --> 5
*****
*****
*****
*****
*****
Version 2.0
Now, let’s say we want to modify the program so the user can draw multiple boxes, not just 1 box. The way we are going to do that is by enclosing the bulk of the code in a do-while loop.
Here’s some pseudocode for that:
Declare a Scanner named s
Declare an integer named size (used for size of rectangle)
Declare an integer named choice (to be used for continuing)
loop:
Prompt the user to enter size of rectangle
size = read integer from the keyboard
loop from 0 to size - 1:
loop from 0 to size - 1:
print 1 asterisk without newline
print a new line
Ask the user if they would like to draw another rectangle.
choice = read next integer from keyboard
while entered choice is to continue:
Now for the code.
import java.util.*; public class StarsV02 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int size = 0; int choice = 1; System.out.println("This program will print rectangles based on the value you enter."); do { System.out.print("Enter the size --> "); size = s.nextInt(); // The outer loop handles each line of output, as in the number of lines to // output for (int i = 0; i < size; i++) { // the inner loop handles what is printed on each line of the output for (int j = 0; j < size; j++) { System.out.print("*"); } System.out.println(); } System.out.print("Would you like to draw another figure (1 for Y or 2 for N)? --> "); choice = s.nextInt(); } while (choice != 2); } }
Here is the output of the running of the program above:
This program will print rectangles based on the value you enter.
Enter the size --> 3
***
***
***
Would you like to draw another figure (1 for Y or 2 for N)? --> 1
Enter the size --> 5
*****
*****
*****
*****
*****
Would you like to draw another figure (1 for Y or 2 for N)? --> 1
Enter the size --> 2
**
**
Would you like to draw another figure (1 for Y or 2 for N)? --> 2
Speak Your Mind