Question 2 – 2-D character arrays, switch and nested loops You are asked to writ
ID: 3733066 • Letter: Q
Question
Question 2 – 2-D character arrays, switch and nested loops
You are asked to write a Java program which will produced patterned squares. The size of the square must be between 4 and 20 inclusive
The choices are as follows:
1. A full square – you need the size of the square and the character to fill the square with.
2. A hollow square - you need the size of the square and the character for the border around the square.
3. An X - you need the size of the square, the character for the X and the character to fill the spaces around the X.
4. A square with horizontal bars - you need the size of the square, the character for the even rows and the character for the odd rows.
5. A square with vertical bars - you need the size of the square, the character for the even columns and the character for the odd columns.
6. A square with diagonal bars - you need the size of the square, the character for the even diagonals and the character for the odd diagonals.
7. An integer filled square – you need the size of the square and the starting integer value.
8. A checkered square - you need the size of the square, and 2 characters for the checkers. The size must be a multiple of 4 for the pattern to work.
9. Quit the program
Explanation / Answer
I solved question 1 2 4 5 and 7
package eample;
/**
*
* @author nida
*/
import java.util.Scanner;
import java.lang.*;
public class Eample
{
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
System.out.print("Please Enter size of square:");
int size = s.nextInt();
if(size<3||size>20)
{
System.out.println("Please Enter the size of square in between 4 and 20");
System.exit(0);
}
System.out.print("Please Enter choice:");
int c = s.nextInt();
int[][] arr = new int[size][size];
switch(c)
{
case 1:
for(int row=0;row<size;row++)
{
for(int col=0;col<size;col++){
System.out.print("Please Enter number:");
int num = s.nextInt();
arr[row][col]=num;
}
}
for(int row=0;row<size;row++)
{
for(int col=0;col<size;col++){
System.out.print(arr[row][col]+" ");
}
System.out.println();
}
break;
case 2:
for(int row=0;row<size;row++)
{
for(int col=0;col<size;col++){
System.out.print("Please Enter number:");
int num = s.nextInt();
arr[row][col]=num;
}
}
for(int row=0;row<size;row++){
for(int col=0;col<size;col++){
if(row==0 || row==size-1 || col==0 || col==size-1){
System.out.print(arr[row][col]+" ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
break;
case 4:
for(int row=0;row<size;row++)
{
if(row%2==0){
for(int col=0;col<size;col++){
System.out.print("Please Enter number for odd rows:");
int num = s.nextInt();
arr[row][col]=num;
}
}
else
{
for(int col=0;col<size;col++){
System.out.print("Please Enter number for even rows:");
int num = s.nextInt();
arr[row][col]=num;
}
}
}
for(int row=0;row<size;row++)
{
for(int col=0;col<size;col++){
System.out.print(arr[row][col]+" ");
}
System.out.println();
}
break;
case 5:
for(int row=0;row<size;row++)
{
if(row%2==0){
for(int col=0;col<size;col++){
System.out.print("Please Enter number for odd col:");
int num = s.nextInt();
arr[col][row]=num;
}
}
else
{
for(int col=0;col<size;col++){
System.out.print("Please Enter number for even rows:");
int num = s.nextInt();
arr[col][row]=num;
}
}
}
for(int row=0;row<size;row++)
{
for(int col=0;col<size;col++){
System.out.print(arr[row][col]+" ");
}
System.out.println();
}
break;
case 7:
System.out.print("Please Enter number:");
int num = s.nextInt();
for(int row=0;row<size;row++)
{
for(int col=0;col<size;col++){
System.out.print(num++ +" ");
}
System.out.println();
}
break;
case 9:
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.