diff --git a/Q1a_24067844/.DS_Store b/Q1a_24067844/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..418fdb06de8034f48186d19c51bb4ad62248e7fe Binary files /dev/null and b/Q1a_24067844/.DS_Store differ diff --git a/Q1a_24067844/pom.xml b/Q1a_24067844/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..715ac65bc88eadbe4603c8d20fe40bf3619b2b6d --- /dev/null +++ b/Q1a_24067844/pom.xml @@ -0,0 +1,43 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>com.example</groupId> + <artifactId>MagicSquareGenerator</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + + <properties> + <maven.compiler.source>11</maven.compiler.source> + <maven.compiler.target>11</maven.compiler.target> + </properties> + + <dependencies> + <!-- JUnit 5 API and Jupiter Engine --> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>5.9.2</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>5.9.2</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <!-- Maven Surefire Plugin to run tests --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>3.0.0-M7</version> + </plugin> + </plugins> + </build> +</project> diff --git a/Q1a_24067844/src/.DS_Store b/Q1a_24067844/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..39aafda386b8bcd08046df1ee5d2a01043d65062 Binary files /dev/null and b/Q1a_24067844/src/.DS_Store differ diff --git a/Q1a_24067844/src/main/.DS_Store b/Q1a_24067844/src/main/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ab00c14bea7bad79a276163c149cfd05019dcf3d Binary files /dev/null and b/Q1a_24067844/src/main/.DS_Store differ diff --git a/Q1a_24067844/src/main/java/.DS_Store b/Q1a_24067844/src/main/java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6f4d6bc37d47bd82e8f1669db9616a2a63a55d58 Binary files /dev/null and b/Q1a_24067844/src/main/java/.DS_Store differ diff --git a/Q1a_24067844/src/main/java/MagicSquareGenerator.class b/Q1a_24067844/src/main/java/MagicSquareGenerator.class new file mode 100644 index 0000000000000000000000000000000000000000..74f3815d309c80d2103868567a9bd63ebaf7eb3d Binary files /dev/null and b/Q1a_24067844/src/main/java/MagicSquareGenerator.class differ diff --git a/Q1a_24067844/src/main/java/MagicSquareGenerator.java b/Q1a_24067844/src/main/java/MagicSquareGenerator.java new file mode 100644 index 0000000000000000000000000000000000000000..9f6bfe892f3694f43d1b1591bd37343b3de36a2e --- /dev/null +++ b/Q1a_24067844/src/main/java/MagicSquareGenerator.java @@ -0,0 +1,64 @@ +import java.util.Scanner; + +public class MagicSquareGenerator { + private int size; + private int[][] square; + + // Constructor to initialize the magic square + public MagicSquareGenerator(int size) { + this.size = size; + this.square = new int[size][size]; + generateMagicSquare(); + } + + // Generating the magic square using method in coursework brief + private void generateMagicSquare() { + int x = 0, y = size / 2; + for (int i = 1; i <= size * size; i++) { + square[x][y] = i; + int newX = (x - 1 + size) % size; + int newY = (y - 1 + size) % size; + + if (square[newX][newY] == 0) { + x = newX; + y = newY; + } else { + x = (x + 1) % size; + } + } + } + + // Display the magic square in a formatted grid + public void display() { + for (int[] row : square) { + for (int num : row) { + System.out.printf("%3d ", num); + } + System.out.println(); + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int size; + + // Input validation: Ensuring the number is a positive odd integer + while (true) { + System.out.print("Enter an odd integer for the magic square size: "); + if (!scanner.hasNextInt()) { + System.out.println("Invalid input. Please enter a positive odd integer."); + // Consume invalid input + scanner.next(); + continue; + } + size = scanner.nextInt(); + if (size % 2 == 1 && size > 0) break; + System.out.println("Invalid input. Please enter a positive odd integer."); + } + + // Generate and display the magic square + MagicSquareGenerator magicSquare = new MagicSquareGenerator(size); + System.out.println("\nGenerated Magic Square:"); + magicSquare.display(); + } +} diff --git a/Q1a_24067844/src/test/.DS_Store b/Q1a_24067844/src/test/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..34bb422410b4719753196e8ac792a538dc850208 Binary files /dev/null and b/Q1a_24067844/src/test/.DS_Store differ diff --git a/Q1a_24067844/src/test/java/.DS_Store b/Q1a_24067844/src/test/java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fb4b9171541e7de387ee5fe4ac632a4284b3f2d2 Binary files /dev/null and b/Q1a_24067844/src/test/java/.DS_Store differ diff --git a/Q1a_24067844/src/test/java/MagicSquareGeneratorTest.java b/Q1a_24067844/src/test/java/MagicSquareGeneratorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0b7aca6de621ececfcc1f7fe51b98163ee3b3a61 --- /dev/null +++ b/Q1a_24067844/src/test/java/MagicSquareGeneratorTest.java @@ -0,0 +1,27 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class MagicSquareTest { + + // Normal case: A 5x5 magic square should be generated with no issues + @Test + void generateMagicSquareNormal() { + MagicSquareGenerator magicSquare = new MagicSquareGenerator(5); + assertNotNull(magicSquare); + } + + // Abnormal case: Negative size should throw an exception + @Test + void generateMagicSquareAbnormal() { + assertThrows(NegativeArraySizeException.class, () -> { + new MagicSquareGenerator(-5); + }); + } + + // Boundary case: A 1x1 magic square should be generated with no issues + @Test + void generateMagicSquareBoundary() { + MagicSquareGenerator magicSquare = new MagicSquareGenerator(1); + assertNotNull(magicSquare); + } +} diff --git a/Q1a_24067844/target/classes/MagicSquareGenerator.class b/Q1a_24067844/target/classes/MagicSquareGenerator.class new file mode 100644 index 0000000000000000000000000000000000000000..5843feb21a16d5416c86f8923590a5a63bbdad2c Binary files /dev/null and b/Q1a_24067844/target/classes/MagicSquareGenerator.class differ diff --git a/Q1a_24067844/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/Q1a_24067844/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000000000000000000000000000000000000..23cd3a47e48f52fe6814fc3bd3ab52b4f67fa3d5 --- /dev/null +++ b/Q1a_24067844/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1 @@ +MagicSquareGenerator.class diff --git a/Q1a_24067844/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/Q1a_24067844/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000000000000000000000000000000000000..267cef78a48aa913a21f1007daeeb28c6c837913 --- /dev/null +++ b/Q1a_24067844/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1 @@ +/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/src/main/java/MagicSquareGenerator.java diff --git a/Q1a_24067844/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst b/Q1a_24067844/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst new file mode 100644 index 0000000000000000000000000000000000000000..445f820bbbb4f8a9fe236ecad48b418969dc103a --- /dev/null +++ b/Q1a_24067844/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst @@ -0,0 +1 @@ +MagicSquareTest.class diff --git a/Q1a_24067844/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/Q1a_24067844/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000000000000000000000000000000000000..23c1831166f163cbd5d2fafbfc05cb54282acc97 --- /dev/null +++ b/Q1a_24067844/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -0,0 +1 @@ +/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/src/test/java/MagicSquareGeneratorTest.java diff --git a/Q1a_24067844/target/surefire-reports/MagicSquareTest.txt b/Q1a_24067844/target/surefire-reports/MagicSquareTest.txt new file mode 100644 index 0000000000000000000000000000000000000000..599f640b35b4510e70c3a20c13eb0620f8849a09 --- /dev/null +++ b/Q1a_24067844/target/surefire-reports/MagicSquareTest.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: MagicSquareTest +------------------------------------------------------------------------------- +Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 s - in MagicSquareTest diff --git a/Q1a_24067844/target/surefire-reports/TEST-MagicSquareTest.xml b/Q1a_24067844/target/surefire-reports/TEST-MagicSquareTest.xml new file mode 100644 index 0000000000000000000000000000000000000000..fdc43f4b62cc784e249acae8be54980651e91d98 --- /dev/null +++ b/Q1a_24067844/target/surefire-reports/TEST-MagicSquareTest.xml @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="MagicSquareTest" time="0.015" tests="3" errors="0" skipped="0" failures="0"> + <properties> + <property name="gopherProxySet" value="false"/> + <property name="awt.toolkit" value="sun.lwawt.macosx.LWCToolkit"/> + <property name="java.specification.version" value="11"/> + <property name="sun.cpu.isalist" value=""/> + <property name="sun.jnu.encoding" value="UTF-8"/> + <property name="java.class.path" value="/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/target/test-classes:/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/target/classes:/Users/ghalaalmasri/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.9.2/junit-jupiter-api-5.9.2.jar:/Users/ghalaalmasri/.m2/repository/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar:/Users/ghalaalmasri/.m2/repository/org/junit/platform/junit-platform-commons/1.9.2/junit-platform-commons-1.9.2.jar:/Users/ghalaalmasri/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar:/Users/ghalaalmasri/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.9.2/junit-jupiter-engine-5.9.2.jar:/Users/ghalaalmasri/.m2/repository/org/junit/platform/junit-platform-engine/1.9.2/junit-platform-engine-1.9.2.jar:"/> + <property name="java.vm.vendor" value="JetBrains s.r.o."/> + <property name="sun.arch.data.model" value="64"/> + <property name="java.vendor.url" value="https://openjdk.java.net/"/> + <property name="user.timezone" value=""/> + <property name="java.vm.specification.version" value="11"/> + <property name="os.name" value="Mac OS X"/> + <property name="sun.java.launcher" value="SUN_STANDARD"/> + <property name="user.country" value="US"/> + <property name="sun.boot.library.path" value="/opt/anaconda3/lib"/> + <property name="sun.java.command" value="/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/target/surefire/surefirebooter-20250315030338681_3.jar /Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/target/surefire 2025-03-15T03-03-38_592-jvmRun1 surefire-20250315030338681_1tmp surefire_0-20250315030338681_2tmp"/> + <property name="jdk.debug" value="release"/> + <property name="surefire.test.class.path" value="/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/target/test-classes:/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/target/classes:/Users/ghalaalmasri/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.9.2/junit-jupiter-api-5.9.2.jar:/Users/ghalaalmasri/.m2/repository/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar:/Users/ghalaalmasri/.m2/repository/org/junit/platform/junit-platform-commons/1.9.2/junit-platform-commons-1.9.2.jar:/Users/ghalaalmasri/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar:/Users/ghalaalmasri/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.9.2/junit-jupiter-engine-5.9.2.jar:/Users/ghalaalmasri/.m2/repository/org/junit/platform/junit-platform-engine/1.9.2/junit-platform-engine-1.9.2.jar:"/> + <property name="sun.cpu.endian" value="little"/> + <property name="user.home" value="/Users/ghalaalmasri"/> + <property name="user.language" value="en"/> + <property name="java.specification.vendor" value="Oracle Corporation"/> + <property name="java.version.date" value="2021-10-19"/> + <property name="java.home" value="/opt/anaconda3"/> + <property name="file.separator" value="/"/> + <property name="basedir" value="/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844"/> + <property name="java.vm.compressedOopsMode" value="Zero based"/> + <property name="line.separator" value=" "/> + <property name="java.specification.name" value="Java Platform API Specification"/> + <property name="java.vm.specification.vendor" value="Oracle Corporation"/> + <property name="java.awt.graphicsenv" value="sun.awt.CGraphicsEnvironment"/> + <property name="surefire.real.class.path" value="/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/target/surefire/surefirebooter-20250315030338681_3.jar"/> + <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/> + <property name="java.runtime.version" value="11.0.13+7-b1751.21"/> + <property name="user.name" value="ghalaalmasri"/> + <property name="path.separator" value=":"/> + <property name="os.version" value="15.1.1"/> + <property name="java.runtime.name" value="OpenJDK Runtime Environment"/> + <property name="file.encoding" value="UTF-8"/> + <property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/> + <property name="java.vendor.version" value="JBR-11.0.13.7-1751.21-jcef"/> + <property name="localRepository" value="/Users/ghalaalmasri/.m2/repository"/> + <property name="java.vendor.url.bug" value="https://bugreport.java.com/bugreport/"/> + <property name="java.io.tmpdir" value="/var/folders/ny/94znt2hs7tlgk2swz46c4ylh0000gn/T/"/> + <property name="java.version" value="11.0.13"/> + <property name="user.dir" value="/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844"/> + <property name="os.arch" value="aarch64"/> + <property name="java.vm.specification.name" value="Java Virtual Machine Specification"/> + <property name="java.awt.printerjob" value="sun.lwawt.macosx.CPrinterJob"/> + <property name="sun.os.patch.level" value="unknown"/> + <property name="java.library.path" value="/Users/ghalaalmasri/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:."/> + <property name="java.vm.info" value="mixed mode"/> + <property name="java.vendor" value="JetBrains s.r.o."/> + <property name="java.vm.version" value="11.0.13+7-b1751.21"/> + <property name="sun.io.unicode.encoding" value="UnicodeBig"/> + <property name="java.class.version" value="55.0"/> + </properties> + <testcase name="generateMagicSquareNormal" classname="MagicSquareTest" time="0.007"/> + <testcase name="generateMagicSquareAbnormal" classname="MagicSquareTest" time="0.001"/> + <testcase name="generateMagicSquareBoundary" classname="MagicSquareTest" time="0"/> +</testsuite> \ No newline at end of file diff --git a/Q1a_24067844/target/test-classes/MagicSquareTest.class b/Q1a_24067844/target/test-classes/MagicSquareTest.class new file mode 100644 index 0000000000000000000000000000000000000000..25b1913d8e16314c945ebfbd6f2db2ec74adbc05 Binary files /dev/null and b/Q1a_24067844/target/test-classes/MagicSquareTest.class differ