Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

L Magic Squares CSC 142 Programming Assignment #4 Due: see our Canvas class site for this information Part B (15 points]: This assignment extends from

image text in transcribedimage text in transcribedimage text in transcribed

L Magic Squares CSC 142 Programming Assignment #4 Due: see our Canvas class site for this information Part B (15 points]: This assignment extends from Part A that you have implemented previously. Recall that in Part A, a grid is drawn for filling in a 4 x 4 square of cells containing integers. For this part of the assignment, first you will need to implement a method that verifies whether a given 4x4 square (containing 16 values) is a Magic Square. A Magic Square for the purpose of this assignment is that the values in any horizontal row, vertical column or main diagonals add up to the same value. You will be using 2-dimensional arrays for storing and processing the values stored in a square. Here is the header for the above method: /* * * This method verifies whether a given square is a magic square or not parameter squareData is a symmetrical 2-dimensional array of integers * returns true if the square is magic if all rows or columns, or two main diagonals add up to same value * return false otherwise */ private static boolean is Square Magic (int 411] squareData); Notes: 1. Implement the above method in the same class from Part A (MagicSquare.java) 2. Once implemented, test this method with squares that are both magic and otherwise. In other words, call this method from the main method in class MagicSquare.java 3. For testing, you need data. For a valid Magic Square, use the data from the grid below: CSC 142 Magic Square 8 11 1 13 2 7 12 60 16 9 6 10 5 4 15 4. For example, here is how the values in the above grid can be stored in using a 2-dimensional array: int (1) squareData = {{8,11,14,1}, {13,2,7,12}, {3,16,9,6}, {10,5,4,15}}; 5. To test squares that are not magic, you need at least four different tests, one each for row and column and two for the diagonals. Using the above valid magic square values, make your own invalid data values. Be sure to include these data sets and describe which particular case they are used to test For the next and final part of the assignment, using the values from the above Magic Square and the directions from Cosmos for making new Magic Squares, you need to generate different versions of Magic Squares, where the rows/columns/diagonals add up to a different total (let's call this value magicValue, so for the above example square, the magicValue is 34). From the above directions, here is a synopsis of process for making a new magic square, given a magicValue > 34: 1. Starting with the above Magic Square as a baseline, 2. Subtract 34 from magic Value, 3. Integer divide the above result by 4, call this result quotient. And, keep the remainder aside, call it remainder, 4. Follow one of the steps below: a. If there is no remainder, add the quotient to all 16 values to the baseline Magic Square, and, voila! we have a new magic square. b. If there is remainder, add quotient to all values in baseline Magic Square, except numbers 13, 14, 15 or 16 in the baseline, where you have to add the quotient plus the remainder. For example, using the steps above, here is a Magic Square for the magicValue = 67: Drawing Per CSC 142 Magic Square 16 19 23 9 22 10 15 20 11 25 17 14 18 13 12 24 C282381.2885-28 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 (1) 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 */ private static void populateGrid (DrawingPanel panel, int [...] squareData); Notes: 1. All esthetical requirements from Part A still apply when the grid is populated with new values (centering, title string etc.) Now it is time to generate and draw few Magic Squares. Here is a pseudo-code fragment for a method in your program (can be in main method) for doing this: Loop that runs n times, where n>= 1, to generate, test and save n magic squares) { newMagicSquare = makeMagicSquare (magicValue) if (isSquareMagic(newmagicSquare)) { clear the drawing panel to erase the existing grid, title and values draw title on the panel draw grid on the panel draw values from newMagicSquare on the panel save the panel as an image file } } Notes: 1. Make use of panel.save (filename) to save the Drawing Panel object as an image file. You could do something like this: panel save ("MagicSquare"+magicValue+".png"); This file will be saved in this project folder. 2. Submit your source-code file (MagicSquare.java) and image files for two Magic Squares for magicValue = 74 and magicValue = 99

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

What is linear transformation? Define with example

Answered: 1 week ago