Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a public static void method drawMainDiagonal() to print a square picture with a main diagonal of 'X's as follows. (Side note: a diagonal of

Write a public static void method drawMainDiagonal() to print a square picture with a main diagonal of 'X's as follows.

(Side note: a diagonal of a matrix is made of the elements that go from top left to bottom right. The main diagonal starts at the first row & column position at top left. Super-diagonals lie above (or to the right) of the main diagonal. Sub-diagonals lie below or to the left.)

Your method takes a single integer parameter (n), which is the number of rows and columns of the square.

Your code will print the characters 'X' and '.' in each row as follows, putting an X on the main diagonal and padding with periods to fill out the square on both sides. Remember to println() at the end of each row.

For example, with n=5 your method would print:

X....

.X...

..X..

...X.

....X

The main ideas of this program are:

1. write a method. you will need to do this 100's of times in CS, so you need to be confident in doing it quickly.

2. use the input parameter from the method.

3. loop over the rows of the matrix, doing something

4. loop over the columns of a row, doing something

5. figure out what to do at each row & column.

public class Main { // TODO - write your method code here. // Do not modify this method. public static void main(String[] args) { drawMainDiagonal(5); drawMainDiagonal(7); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

Describe BUSINESS MANAGEMENT

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago