Skip to content
Snippets Groups Projects
Commit 1ebc5f94 authored by Ghala Almasri's avatar Ghala Almasri
Browse files

Added Q1a Maven Project Folder

parent ae37f4a6
Branches main
No related tags found
No related merge requests found
Showing
with 206 additions and 0 deletions
File added
<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>
File added
File added
File added
File added
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();
}
}
File added
File added
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);
}
}
File added
MagicSquareGenerator.class
/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/src/main/java/MagicSquareGenerator.java
MagicSquareTest.class
/Users/ghalaalmasri/Desktop/cm1210 coursework/Q1a_24067844/src/test/java/MagicSquareGeneratorTest.java
-------------------------------------------------------------------------------
Test set: MagicSquareTest
-------------------------------------------------------------------------------
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 s - in MagicSquareTest
<?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="&#10;"/>
<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
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment