Question

In: Computer Science

I am using NetBeans IDE Java to code and I am seeking comment to the code...

I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding).

  1. Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.

Write a program that reads in 16 values from the keyboard, displays them in a 4 x 4 array, and tests whether they form a magic square.

Example:

              16     3       2      13

               5      10     11     8

               9       6       7      12

               4       15    14     1

You need to test two features:

  1. Does each of the numbers 1, 2, …., 16 occur in the user input?
  2. When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

Hint: use a two-dimentional array.

  1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods:

public void turnLeft()

public void turnRight()

public void move()

public Point getLocation()

public String getDirection()

The turnLeft and turnRight methods change the direction but not the location. The move method moves the robot by one unit in the direction it is facing. The getDirection method returns a string "N", "E", "S", or "W".

Solutions

Expert Solution

package Matrix;

import java.util.Scanner;

// Defines class MagicSquareTest
public class MagicSquareTest
{
   // Method to accept data to matrix
   static void accpetData(int matrix[][])
   {
       // Scanner class object created
       Scanner sc = new Scanner(System.in);
      
       // Loops till number of rows
       for(int r = 0; r < matrix.length; r++)
       {
           // Loops till number of columns
           for(int c = 0; c < matrix[r].length; c++)
           {
               // Accepts data for current cell
               System.out.print("\n Enter the data for [" + r + "] [" + c + "]: ");
               matrix[r][c] = sc.nextInt();
           }// End of for loop for column
       }// End of for loop for row
      
       // Loops to display the matrix
       // Loops till number of rows
       for(int r = 0; r < matrix.length; r++)
       {
           // Loops till number of columns
           for(int c = 0; c < matrix[r].length; c++)
           {
               // Accepts data for current cell
               System.out.print(matrix[r][c] + " ");
           }// End of for loop for column
           System.out.println();
       }// End of for loop for row
      
       // Close the scanner
       sc.close();
   }// End of method
  
   // Method to return true if parameter matrix is magic matrix
   // Otherwise returns false
   static boolean checkMagicMatrix(int matrix[][])
   {
       // calculate the sum of the prime diagonal
   int leftDiagonalSum = 0, rightDiagonalSum = 0;
  
   // Loops till end of the matrix to calculate the sum of left diagonal
   for(int r = 0; r < matrix.length; r++)
       leftDiagonalSum = leftDiagonalSum + matrix[r][r];
  
   // Loops till end of the matrix to calculate the sum of right diagonal
   for(int r = 0; r < matrix.length; r++)
       rightDiagonalSum = rightDiagonalSum + matrix[r][matrix.length - 1 - r];
  
   // Checks if left and right diagonal sum is not equal then return false
   if(leftDiagonalSum != rightDiagonalSum)
   return false;
  
   // Loops to calculate each row sum
  
   // Loops till number of rows
   for(int r = 0; r < matrix.length; r++)
   {
       // To store each row sum
   int rowSum = 0;
  
   // Loops till number of columns
   for(int c = 0; c < matrix.length; c++)
   rowSum += matrix[r][c];
  
   // Checks if current row sum is not equals to left diagonal sum
   // then returns false
   if (rowSum != leftDiagonalSum)
   return false;
   }// End of for loop
  
   // Loops to calculate each column sum
  
   // Loops till number of rows
   for(int r = 0; r < matrix.length; r++)
   {
       // To store each column sum
   int colSum = 0;
  
   // Loops till number of columns
   for(int c = 0; c < matrix.length; c++)
   colSum += matrix[c][r];
  
   // Checks if current column sum is not equals to left diagonal sum
   // then returns false
   if(colSum != leftDiagonalSum)
       return false;
   }// End of for loop
   // Otherwise returns true
   return true;
   }// End of method
  
   // main method definition
   public static void main(String []s)
   {
       // Declares a matrix of 4 x 4
       int matrix[][] = new int[4][4];
       // Calls the method to accept and display matrix
       accpetData(matrix);
      
       // Call the method to check matrix is magic matrix or not
       if(checkMagicMatrix(matrix))
           System.out.print("\n Matrix is magic matrix.");
       else
           System.out.print("\n Matrix is not a magic matrix.");
      
   }// End of main method
}// End of class


Sample Output:


Enter the data for [0] [0]: 16

Enter the data for [0] [1]: 3

Enter the data for [0] [2]: 2

Enter the data for [0] [3]: 13

Enter the data for [1] [0]: 5

Enter the data for [1] [1]: 10

Enter the data for [1] [2]: 11

Enter the data for [1] [3]: 8

Enter the data for [2] [0]: 9

Enter the data for [2] [1]: 6

Enter the data for [2] [2]: 7

Enter the data for [2] [3]: 12

Enter the data for [3] [0]: 4

Enter the data for [3] [1]: 15

Enter the data for [3] [2]: 14

Enter the data for [3] [3]: 1
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Matrix is magic matrix.


Related Solutions

I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java SE version only). If you do not have the latest Java installation to match, Netbeans will direct you to download the needed version of Java. If needed, download and install the latest Oracle Official Java Software Development Kit Standard Edition (Java SDK SE), and JUnit. I suggest you do this via the bundle referred to as The Java Development Kit (JDK). Download the architecture...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
Write this program using an IDE. Comment and style the code according to the CS 200...
Write this program using an IDE. Comment and style the code according to the CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. Monster collector is a game of chance, where the user tries to collect monsters by guessing the correct numbers between 1 and 5. If the user doesn't guess the incorrect number, you catch...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode searchBST(TreeNode root, int val) {    }...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) {    } }; Given an array where elements are sorted in...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> inorderTraversal(TreeNode* root) {    } }; Given the root of a binary tree, return...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> preorderTraversal(TreeNode* root) {    } }; Given the root of a binary tree, return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT