Part 1: there is no part 1 in this assignemnt Part 2: Programming: (20 points) Y
ID: 3779514 • Letter: P
Question
Part 1: there is no part 1 in this assignemnt Part 2: Programming: (20 points) Your assignment is to design a class called MagicSquares.A magic square is an n by n matrix that is filled with the numbers 1, 2, 3, 4 n square if the sum of the elements in each row, in each column, and in the two diagonals isthe same value. Your class must have two instance variables: a 2-Darray to hold the integers and an integer size which is the size of your square (for example n). Use the following UML diagram to design the class: Mat Square 1 int size int MagicSquares Magic Squares (int tj) leftRight Diagonals int right Left Diagonal Sum Int coulmnSum0 int rows um int boolean correct Numbers validMagic Square boolean t tostring Str Program requirements and/or constraints: You will need to create two files Magicsquares,java file, and va file which has the Magicsquares has two constructors. The first constructor Magicsquares reads the input from a fileExplanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class MagicSquares
{
int a[i][j];
int size;
MagicSquares(){
size=0;
a[i][j]= new int[size][size];
}
MagicSquares(){
BufferedReader br = new BufferedReader(new FileReader("nums.txt"));
size = Integer.parseInt(br.readLine());
for(int i=0 : size){
for(int j=0: size){
a[i][j] = Integer.parseInt(br.readLine());
}
}
}
MagicSquares(int[] array){
int index = 0;
size = Math.sqrt(array.size());
for(int i=0 : size){
for(int j=0: size){
a[i][j] = array[index];
index++;
}
}
}
private int LeftRightDiagonalSum(){
int sumLRD=0;
for(int i=0 : size){
for(int j=0: size){
if(i==j){
sumLRD += a[i][j];
}
}
}
return sumLRD;
}
private int RightLeftDiagonalSum(){
int sumRLD=0;
for(int i=0 : size){
for(int j=0: size){
if((i+j)==(size-1)){
sumRLD += a[i][j];
}
}
}
return sumRLD;
}
private int columnSum(){
int sumCol = 0;
for(int j: size){
sumCol = 0;
for(int i : size){
sumCol += a[i][j];
}
if(j==0)
sumVerify = sumCol;
else{
if(sumVerify != sumCol){
return -1;
}
}
sumCol = 0;
}
}
private int rowSum(){
for(int i: size){
sumRow = 0;
for(int j : size){
sumRow += a[i][j];
}
if(i==0){
sumVerify = sumRow;
}
else{
if(sumVerify != sumRow){
return -1;
}
}
}
return sumRow;
}
private boolean correctNumbers(){
int check[]=new int[size];
for(int i=0 : size){
for(int j=0: size){
if(!(a[i][j]>=1 || a[i][j]<=size)){
return false;
}
}
}
return true;
}
public boolean validMagicsquares(){
int data = LeftRightDiagonalSum();
if(sumRLD != sumVerify){
return -1;
}
if(RightLeftDiagnalSum()!=data && columnSum()==data && rowSum()==data ){
return true;
}
return false;
}
public toString(){
for(int i=0 : size){
for(int j=0: size){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.