Draw That Square!
Modules - Structured Programming 1, Structured Programming 2
Teacher’s Notes:
========================================================================
This is a relatively simple exercise that gets students thinking mathematically to break down a problem.
My JAVA solution below is just one way of doing this but there are many more!
Assignment:
========================================================================
Draw That Square
In this assignment, you will write a program that reads a single integer d , followed by a single character c from the keyboard. There is no prompt for this input and there is no need to error trap.
The value of d will be such that 1 <= d <= 50. c may be any printable symbol from the ASCII table.
The program will then draw a hollow square with edges made of the symbol that is d x d . Note that due to the way the square may be printed, it may look more rectangular.
Sample Input:
4
h
Sample Output:
hhhh
h h
h h
hhhh
Explanation:
The first input is a 4 indicating that the square should be 4 symbols by 4 symbols. The next character input is an 'h' which indicates that the symbol should be an 'h'.
Solution (JAVA):
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int dimension = input.nextInt();
String symbol = input.next();
if(dimension == 1)
System.out.print(symbol);
else {
for(int x = 0; x < dimension; x++)
System.out.print(symbol);
System.out.println();
for(int y = 0; y < dimension - 2; y++){
System.out.print(symbol);
for(int x = 0; x < dimension-2; x++)
System.out.print(" ");
System.out.println(symbol);
}
for(int x = 0; x < dimension; x++)
System.out.print(symbol);
System.out.println();
}
}
}