-
Notifications
You must be signed in to change notification settings - Fork 0
/
a2.html
233 lines (200 loc) · 12.2 KB
/
a2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!doctype html>
<html>
<head>
<title>Assignment 2 - Report</title>
<meta charset='utf-8'/>
<link rel="stylesheet" type="text/css" href="./a2.html_files/github-markdown.css">
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
</style>
<script>
window.onload = function() {
if (document.querySelector("script[type=\"math/tex; mode=display\"]") !== null) {
var mathjax = document.createElement("script");
mathjax.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML";
document.head.appendChild(mathjax);
}
if (document.getElementsByTagName("code").length !== 0) {
var highlight = document.createElement("script");
var highlightcss = document.createElement("link");
highlight.src = "http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/highlight.min.js";
highlightcss.rel = "stylesheet";
highlightcss.href = "http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/styles/github.min.css";
highlight.onload = function() {hljs.initHighlighting();};
document.head.appendChild(highlight);
document.head.appendChild(highlightcss);
}
}
</script>
</head>
<body>
<article class="markdown-body">
<h1 id="assignment-2-report">Assignment 2 - Report</h1>
<h2 id="lo-shu-magic-square">Lo Shu Magic Square</h2>
<p>Lo Shu Magic Square is a square where if "n" is the number of rows and columns, then sum of every row, column and diagonal is n(n^2+1)/2.</p>
<h2 id="loshumagicsquare-program">LoShuMagicSquare Program</h2>
<p>LoShuMagicSquare is a program that takes the dimension of a square and the elements of the square as input from user and determines whether the given square is Lo Shu Magic Square.</p>
<h2 id="loshumagicsquare-program-work-procedure">LoShuMagicSquare Program Work Procedure</h2>
<h3 id="1-loshumagicsquare-class">1. <code>LoShuMagicSquare</code> Class</h3>
<p>LoShuMagicSquare class is the main class of LoShuMagicSquare program. The <code>main</code> method of this program is inside this class.</p>
<pre><code>public class LoShuMagicSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
System.out.print("Please enter the desired grid dimension: ");
n = input.nextInt();
int[][] square = new int[n][n];
System.out.println("Please enter the desired input:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
square[i][j] = input.nextInt();
}
}
LoShuMagicCheck lsmc = new LoShuMagicCheck();
System.out.println();
System.out.println(lsmc.magicCheck(square, n));
System.out.println();
System.out.println("Thank you.");
}
}
</code></pre><h4 id="1-1-main-method">1.1 <code>main</code> Method</h4>
<p>First statement of the <code>main</code> method creates an object <code>input</code> of the <code>java.util.Scanner</code> class.</p>
<pre><code>Scanner input = new Scanner(System.in);
</code></pre><p>Then a variable <code>n</code> of type <code>int</code> is declared, value of <code>n</code> is the dimension of the square. Next two statements takes the value of <code>n</code> as input from user.</p>
<pre><code>int n;
System.out.print("Please enter the desired grid dimension: ");
n = input.nextInt();
</code></pre><p>After taking the value of <code>n</code> from user, a two dimensional array of type <code>int[][]</code> of size <code>n</code>x<code>n</code> is declared and its elements are taken as input from the user. Nested <code>for</code> loops are used for taking the value of elements as input.</p>
<pre><code>int[][] square = new int[n][n];
System.out.println("Please enter the desired input:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
square[i][j] = input.nextInt();
}
}
</code></pre><p>Then an object <code>lsmc</code> of the class <code>LoShuMagicCheck</code> was created. Next <code>println</code> statements prints the output of the program. The statement <code>System.out.println(lsmc.magicCheck(square, n));</code> prints the <code>String</code> returned by the method <code>magicCheck()</code> of the <code>LoShuMagicCheck</code> class, for the argument <code>square</code> and <code>n</code>, <code>magicCheck()</code> determines whether the square has been taken from user as input is a Lo Shu Magic Square.</p>
<pre><code>LoShuMagicCheck lsmc = new LoShuMagicCheck();
System.out.println();
System.out.println(lsmc.magicCheck(square, n));
System.out.println();
System.out.println("Thank you.");
</code></pre><h3 id="2-loshumagiccheck-class">2. <code>LoShuMagicCheck</code> Class</h3>
<p>LoShuMagicCheck class contains the definition of the method <code>magicCheck(int[][] sq, int d)</code>, which method determines whether the two dimensional array <code>sq</code> of size <code>dm</code>x<code>dm</code> is a Lo Shu Magic Square.</p>
<p><code>magicCheck()</code> method takes <code>sq</code> of type <code>int[][]</code> and <code>dm</code> of type <code>int</code> as argument and returns a <code>String</code>.</p>
<pre><code>class LoShuMagicCheck {
String magicCheck(int[][] sq, int dm) {
int magicNumber = (dm * ((dm * dm) + 1)) / 2;
int sumOfDiagDown = 0, sumOfDiagUp = 0;
for(int i = 0; i < dm; i++) {
int sumOfRow = 0, sumOfCol = 0;
for(int j = 0; j < dm; j++) {
sumOfRow += sq[i][j];
sumOfCol += sq[j][i];
}
if ((sumOfRow != magicNumber) || (sumOfCol != magicNumber)) {
return "This is not a Lo Shu Magic Square.";
}
sumOfDiagDown += sq[i][i];
sumOfDiagUp += sq[i][dm - (i + 1)];
}
if ((sumOfDiagDown != magicNumber) || (sumOfDiagUp != magicNumber)) {
return "This is not a Lo Shu Magic Square.";
}
else {
return "This is a Lo Shu Magic Square.\nAnd, the magic number is " + magicNumber + ".";
}
}
}
</code></pre><h4 id="2-1-magiccheck-method">2.1 <code>magicCheck()</code> Method</h4>
<p>The first statement of this method evaluates the magic number for <code>dm</code> with the formula n(n^2+1)/2.</p>
<pre><code>int magicNumber = (dm * ((dm * dm) + 1)) / 2;
</code></pre><p>Then two variables <code>sumOfDiagUp</code> and <code>sumOfDiagDown</code> of type <code>int</code> was declared and initialized with value <code>0</code>. This variables will contain the sum of the elements of two diagonals of the two dimensional array <code>sq</code>.</p>
<pre><code>int sumOfDiagDown = 0, sumOfDiagUp = 0;
</code></pre><p>Then a <code>for</code> loop is used to get the sum of every row, column and diagonal. A variable <code>i</code> of type <code>int</code> is declared and is initialized with the value <code>0</code>. The iteration of the <code>for</code> loop will continue as long as the value of <code>i</code> is lesser than <code>dm</code>. <code>1</code> is added to the value of <code>i</code> after each iteration by <code>i++</code>.</p>
<pre><code>for(int i = 0; i < dm; i++) {
int sumOfRow = 0, sumOfCol = 0;
for(int j = 0; j < dm; j++) {
sumOfRow += sq[i][j];
sumOfCol += sq[j][i];
}
if ((sumOfRow != magicNumber) || (sumOfCol != magicNumber)) {
return "This is not a Lo Shu Magic Square.";
}
sumOfDiagDown += sq[i][i];
sumOfDiagUp += sq[i][dm - (i + 1)];
}
</code></pre><p>Two variables <code>sumOfRow</code> and <code>sumOfColumn</code> of type <code>int</code> is declared in the first statement inside the <code>for</code> loop, which will contain the sum of all the elements of the row <code>i</code> and column <code>i</code>. This statement is followed by another <code>for</code> loop which is used to get the sum of the row <code>i</code> and the sum of the column <code>i</code>. Then <code>if</code> condition is used to check whether the sum of row <code>i</code> or sum of column <code>i</code> is not equal to <code>magicNumber</code>. If any of them is not equal, then the <code>String</code>, <code>"This is not a Lo Shu Magic Square."</code> is returned as the return value of the method <code>magicCheck()</code>.</p>
<pre><code>int sumOfRow = 0, sumOfCol = 0;
for(int j = 0; j < dm; j++) {
sumOfRow += sq[i][j];
sumOfCol += sq[j][i];
}
if ((sumOfRow != magicNumber) || (sumOfCol != magicNumber)) {
return "This is not a Lo Shu Magic Square.";
}
</code></pre><p>Next two statements inside the first <code>for</code> loop is used to get the value of <code>sumOfDiagDown</code> and <code>sumOfDiagUp</code>. <code>SumOfDiagDown</code> and <code>SumOfDiagUp</code> are the sum of downward and upward diagonal of <code>sq</code> respectively. After all the iterations of the first <code>for</code> loop <code>sumOfDiagDown</code> and <code>sumOfDiagUp</code> get their exact value.</p>
<pre><code>sumOfDiagDown += sq[i][i];
sumOfDiagUp += sq[i][dm - (i + 1)];
</code></pre><p>The next <code>if</code> condition, after the <code>for</code> loop block is used to check whether the sum of downward diagonal <code>sumOfDiagDown</code> and sum of upward diagonal <code>sumOfDiagUp</code> is not equal to <code>magicNumber</code>. If it is not equal, then the <code>String</code>, <code>"This is not a Lo Shu Magic Square."</code> is returned as the return value of the method <code>magicCheck()</code>, else <code>"This is a Lo Shu Magic Square.\nAnd, the magic number is " + magicNumber + "."</code> is returned as the return value.</p>
<pre><code>if ((sumOfDiagDown != magicNumber) || (sumOfDiagUp != magicNumber)) {
return "This is not a Lo Shu Magic Square.";
}
else {
return "This is a Lo Shu Magic Square.\nAnd, the magic number is " + magicNumber + ".";
}
</code></pre><h3 id="the-complete-program">The Complete Program</h3>
<p>import java.util.Scanner;</p>
<pre><code>public class LoShuMagicSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
System.out.print("Please enter the desired grid dimension: ");
n = input.nextInt();
int[][] square = new int[n][n];
System.out.println("Please enter the desired input:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
square[i][j] = input.nextInt();
}
}
LoShuMagicCheck lsmc = new LoShuMagicCheck();
System.out.println();
System.out.println(lsmc.magicCheck(square, n));
System.out.println();
System.out.println("Thank you.");
}
}
class LoShuMagicCheck {
String magicCheck(int[][] sq, int dm) {
int magicNumber = (dm * ((dm * dm) + 1)) / 2;
int sumOfDiagDown = 0, sumOfDiagUp = 0;
for(int i = 0; i < dm; i++) {
int sumOfRow = 0, sumOfCol = 0;
for(int j = 0; j < dm; j++) {
sumOfRow += sq[i][j];
sumOfCol += sq[j][i];
}
if ((sumOfRow != magicNumber) || (sumOfCol != magicNumber)) {
return "This is not a Lo Shu Magic Square.";
}
sumOfDiagDown += sq[i][i];
sumOfDiagUp += sq[i][dm - (i + 1)];
}
if ((sumOfDiagDown != magicNumber) || (sumOfDiagUp != magicNumber)) {
return "This is not a Lo Shu Magic Square.";
}
else {
return "This is a Lo Shu Magic Square.\nAnd, the magic number is " + magicNumber + ".";
}
}
}
</code></pre>
</article>
</body>
</html>