Assignments Documentation At the beginning ofeach programmin

Assignments Documentation: At the beginning ofeach programming assignment you must have a comment block with the following information: MAUTHOR: your name LENAME title of the source file SPECIFICATION: description of the program YOUR Lab Letter and Name of the TA for your Closed lab ll FOR: CSE 110- homework days and time of your class llTIME SPENT how long it took you to complete the assignment 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 is the same value. Your class must have two instance variables: a 2-D arrayto 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 Squares square ll int size int Magic Squares Magicsquares (inta) leftRight Diagonal Sum Int right Left Diagona Sum int coulmnsum rows um int

Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.lang.*;

public class MagicSquare {
   public static void main(String[] args) {
       int[][] arr = new int[100][100];
       int commandType = takeCommandType();
       switch (commandType) {
       case 1:
           takeInputFromFile(arr);
           break;
       case 2:
           takeInputFromUser(arr);
           break;
       case 3:
           System.exit(0);
       default:
           takeCommandType();
           break;

       }
   }

   private static boolean takeInputFromUser(int[][] array) {
       int[] arr = new int[100];
       int index = 0;
       Scanner sc = new Scanner(System.in);
       boolean result=false;
       try {
           System.out.println(\"Enter the size of the square\");
           int i = sc.nextInt();
           System.out.println(\"Enter the Values square\");
           for(int j=0; j<i;j++){
               for(int k=0; k<i; k++) {
                   array[j][k]=sc.nextInt();
                   //System.out.print(array[j][k]);
               }
               //System.out.println(\"\");
           }
           for (int j=0; j<i; j++) {
               int sum=0;
               int sumCurrent=0;
               for(int k=0; k<i;k++) {
                   sumCurrent+=array[j][k];
               }
               if(j ==0) {
                   sum = sumCurrent;
               } else {
                   if (sum != sumCurrent) {
                       result = false;
                       break;
                   }
               }
           }
           for (int j=0; j<i; j++) {
               int sum=0;
               int sumCurrent=0;
               for(int k=0; k<i;k++) {
                   sumCurrent+=array[k][j];
               }
               if(j ==0) {
                   sum = sumCurrent;
               } else {
                   if (sum != sumCurrent) {
                       result = false;
                       break;
                   }
               }
           }
       }catch(Exception e){
           System.out.println(\"Enter Integer Only\");
           sc.nextLine();
       }
       return result;
   }

   private static int takeInputFromFile(int[][] array) {
       int[] arr = new int[100];
       int index = 0;
       Scanner sc = null;
       int i = 0;
       try {
           System.out.println(\"Please Enter Valid File Name\");
           sc = new Scanner(new File(sc.next()));
       } catch (FileNotFoundException e1) {
           sc.nextLine();
           System.exit(1);
       }
       try {

           while (sc.hasNext()) {
               arr[index] = sc.nextInt();
               index++;
           }

           i = (int) Math.sqrt(index);
           for(int j=0; j<i;j++){
               for(int k=0; k<i; k++) {
                   array[j][k]=arr[i];
                   System.out.print(array[j][k]);
               }
               System.out.println(\"\");
           }

       } catch (InputMismatchException e) {
           System.out.println(\"Error: Not Valid Input Data\");
           System.exit(1);
       }
       return i;
   }

   private static int takeCommandType() {
       Scanner sc = new Scanner(System.in);
       try {
           System.out.println(\"Please Enter a command or type\");
           return (sc.nextInt());
       } catch (InputMismatchException ime) {
           System.out.println(\"Please ENTER Valid Input\");
           sc.nextLine();
           return takeCommandType();
       }
   }
}

 Assignments Documentation: At the beginning ofeach programming assignment you must have a comment block with the following information: MAUTHOR: your name LENA
 Assignments Documentation: At the beginning ofeach programming assignment you must have a comment block with the following information: MAUTHOR: your name LENA
 Assignments Documentation: At the beginning ofeach programming assignment you must have a comment block with the following information: MAUTHOR: your name LENA

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site