Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

QUESTION: Creating a JAVA program which should implement a method that verifies whether a given 4 x 4 square (containing 16 values) is a Magic Square with a given magicValue. Also, adding code to generate values for a Magic Square based on a magicValue.

The code here is good to solve for verifying if it is a magic square but not n generating a new Magic Square, it is not as in the picture. The sum in row and columns are not equal.

PLEASE, PLEASE, PLEASE, don't post code from anywhere else. Please help me by working on this code here.

import java.util.*;

import java.awt.*;

public class MagicSquare{

static int checkMag(int mat[][],int M)

{

// Function for checking Magic square

int i, j,n=4; int sum=0; // filling matrix with its count value // starting from 1;

for ( i = 0; i < n; i++) {

    for ( j = 0; j < n; j++)

    sum=sum+mat[i][j];

if(sum!=M)

{

return 0;

    }

    sum=0;

    }

for ( i = 0; i < n; i++) {

    for ( j = 0; j < n; j++)

    sum=sum+mat[j][i];

if(sum!=M)

{    

return 0;

}

sum=0;

}

int sumR=0,

sumL=0;

for ( i = 0; i < n; i++) {

    for ( j = 0; j < n; j++) {

        if(i==j) sumL=sumL+mat[i][j];

if((i+j)==(n-1))

sumR=sumR+mat[i][j]; } }

if(sumR!=M )

{

return 0;

    }

    if(sumL!=M)

    {

        return 0;

        

    }

return 1;

}

static int[][] builtMagic(int mat[][],int M,int Mnew) {

    int i,j,n=4;

    Mnew=Mnew-M;

    int rem=Mnew%4;

    int quo =Mnew/4;

    if(rem==0){ for ( i = 0; i < n; i++) {

        for ( j = 0; j < n; j++)

        mat[i][j] =mat[i][j]+quo; }}

        else{

for ( i = 0; i < n; i++) {

for ( j = 0; j < n; j++) {

mat[i][j] =mat[i][j]+quo;

if(mat[i][j]==13||mat[i][j]==14||mat[i][j]==15||mat[i][j]==16) {

mat[i][j] =mat[i][j]+rem;

}

}

}

}

return mat;

}

public static void main (String[] args) {

//Scanner object

Scanner sc = new Scanner(System.in);

DrawingPanel panel = new DrawingPanel(500, 500);

Graphics g = panel.getGraphics();

// creating a 2d array of magic square numbers

int numbers[][] = { { 8, 11, 14, 1 }, { 13, 2, 7, 12 }, { 3, 16, 9, 6 }, { 10, 5, 4, 15 } };

// drawing grid of cell width = 80 centered at panel center

drawGrid(g, panel.getWidth() / 2, panel.getHeight() / 2,numbers.length, 80);

//drawNumbers(g, panel.getWidth() / 2, panel.getHeight() / 2, 80, numbers);

// drawing title string centered at y=50

drawString(g, "CSC 142 Magic Square", panel.getWidth() / 2, 50);

int n = 4;

int magMat[][]= new int[n][n];

int M;

int baseMat[][]={{12,6,15,1},{13,3,10,8},{2,16,5,11},{7,9,4,14}};

for (int k=0;k<10;k++){

    System.out.println("Enter magic value");

    M=sc.nextInt();

    System.out.println( "Enter " + ( n*n ) + " values" );

    for(int i=0;i<n;i++) {

        for(int j=0;j<n;j++)

 {

magMat[i][j]= sc.nextInt();

}

}

if(checkMag(magMat,M)==1) {

System.out.println("It is a magic square");

panel.clear();

makeMagicSquareGrid(panel,magMat,k+1);  // insert magic m=numbers on grid

}

else {

System.out.println("It is not a magic square"); }

System.out.println("Enter another magic value greater than 34");

int Mnew=sc.nextInt();

magMat=builtMagic(magMat,34,Mnew);

panel.clear();

makeMagicSquareGrid(panel,magMat,k+1);  // insert magic m=numbers on grid

}

static void makeMagicSquareGrid(DrawingPanel dp,int[][] magMat, int k){

Graphics g = dp.getGraphics();

drawGrid(g, dp.getWidth() / 2, dp.getHeight() / 2,magMat.length, 80);

// drawing numbers

drawNumbers(g, dp.getWidth() / 2, dp.getHeight() / 2, 80, magMat);

// drawing title string centered at y=50

drawString(g, "Magic Square "+k, dp.getWidth() / 2, 50);

String filename="MagicMatrix"+k+".jpeg";

try{

dp.save(filename);

}

catch(Exception e)

{

    System.out.println("File coudn't be saved");

}

}

public static void drawGrid(Graphics g, int centerX, int centerY, int row, int cell) { // cell width

int x = centerX - (row / 2) * cell;

int y = centerY - (row / 2) * cell;

// setting black color

g.setColor(Color.BLACK);

// looping for each row and column

for (int i = 0; i < row; i++) {

for (int j = 0; j < row; j++) {

// drawing a square with equal sides

g.drawRect(x + (cell * j), y + (cell * i),cell, cell);

}

}

}

// method to draw the numbers (given in a 2d array) inside a grid (assuming

// grid is previously drawn)

public static void drawNumbers(Graphics g, int centerX, int centerY,int cell, int numbers[][]) {

int x = centerX - (numbers.length / 2) * cell;

int y = centerY - (numbers.length / 2) * cell; //Setting red color

g.setColor(Color.RED); // Customizing font

g.setFont(new Font("Georgia", Font.BOLD, 25));

for (int i = 0; i < numbers.length; i++) {

for (int j = 0; j < numbers.length; j++) {

// Setting center cell

int xx = x + (cell * j) + cell / 2;

int yy = y + (cell * i) + cell / 2;

// Setting center text

drawString(g, String.valueOf(numbers[i][j]), xx, yy);

}

}

} public static void drawString(Graphics g, String text, int centerX, int centerY) {

// getting FontMetrics of current font

FontMetrics metrics = g.getFontMetrics(g.getFont());

// finding width of text in pixels using metrics

int width = metrics.stringWidth(text);

// drawing string center aligned

g.drawString(text, centerX - width / 2, centerY + metrics.getHeight()/ 2);

}

}

Page
3
> of 4
ZOOM
+
Drawing Panel
Eile View Help
CSC 142 Magic Square
16
19
23
9
22
10
15
20
11
25
17
14
18
13
12
24
(x=506, y=208), r=255 g=255 b=255
expand button
Transcribed Image Text:Page 3 > of 4 ZOOM + Drawing Panel Eile View Help CSC 142 Magic Square 16 19 23 9 22 10 15 20 11 25 17 14 18 13 12 24 (x=506, y=208), r=255 g=255 b=255
Page <
3
> of 4
ZOOM
+
Here is the header for the making a generating a new Magic Square method:
* This method generates values for a Magic Square based on a magicValue
* parameter magicValue is used to generate a new Magic Square where all rows
or columns,
or two diagonals add up to that value
* returns a symmetrical 2-dimensional array containing the newly generated Magic Square
*/
private static int 00 makeMagicSquare (int magicValue);
Notes:
1. Add the above method to MagicSquare.java
2. Add code to generate at least 10 different Magic Squares, using makeMagicSquare method
3. Make sure test the validity of the above generate Magic Squares using isSquareMagic method
Now it is time to integrate Part A with the above parts of generating and testing new Magic Squares, and
as you might've guessed it already!, the newly generate Magic Squares should be rendered in the grid
that you have developed previously (just like the new Magic Square picture is shown in the above
example), and further more these new rendered grids are saved as image files by your program.
Here is the header for the making a populating a grid with new Magic Square values:
/*
*
This method takes a DrawingPanel containing a 4 x 4 grid and fills that
grid with values
from a 2-dimensional array
*parameter panel is a DrawingPanel object with 4 x 4 grid already drawn
* parameter squareData is a integer symmetrical 2-dimensional array containing cell values
expand button
Transcribed Image Text:Page < 3 > of 4 ZOOM + Here is the header for the making a generating a new Magic Square method: * This method generates values for a Magic Square based on a magicValue * parameter magicValue is used to generate a new Magic Square where all rows or columns, or two diagonals add up to that value * returns a symmetrical 2-dimensional array containing the newly generated Magic Square */ private static int 00 makeMagicSquare (int magicValue); Notes: 1. Add the above method to MagicSquare.java 2. Add code to generate at least 10 different Magic Squares, using makeMagicSquare method 3. Make sure test the validity of the above generate Magic Squares using isSquareMagic method Now it is time to integrate Part A with the above parts of generating and testing new Magic Squares, and as you might've guessed it already!, the newly generate Magic Squares should be rendered in the grid that you have developed previously (just like the new Magic Square picture is shown in the above example), and further more these new rendered grids are saved as image files by your program. Here is the header for the making a populating a grid with new Magic Square values: /* * This method takes a DrawingPanel containing a 4 x 4 grid and fills that grid with values from a 2-dimensional array *parameter panel is a DrawingPanel object with 4 x 4 grid already drawn * parameter squareData is a integer symmetrical 2-dimensional array containing cell values
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education