From bb7d195e0167b08d3f5ed59dc28e72b3a298a3f6 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 27 Apr 2025 17:05:42 +0200 Subject: simplify: merge subpackage model into main package --- .../dk/biks/bachelorizer/Combi.java | 105 +++++++++++++++++++++ .../dk/biks/bachelorizer/Control.java | 2 - .../dk/biks/bachelorizer/GUI.java | 60 ++++++++++++ .../dk/biks/bachelorizer/Person.java | 18 ++++ .../dk/biks/bachelorizer/Window.java | 2 - .../dk/biks/bachelorizer/model/Combi.java | 105 --------------------- .../dk/biks/bachelorizer/model/GUI.java | 62 ------------ .../dk/biks/bachelorizer/model/Person.java | 18 ---- 8 files changed, 183 insertions(+), 189 deletions(-) create mode 100644 src/dk.biks.bachelorizer/dk/biks/bachelorizer/Combi.java create mode 100644 src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java create mode 100644 src/dk.biks.bachelorizer/dk/biks/bachelorizer/Person.java delete mode 100644 src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Combi.java delete mode 100644 src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/GUI.java delete mode 100644 src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Person.java (limited to 'src/dk.biks.bachelorizer/dk') diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Combi.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Combi.java new file mode 100644 index 0000000..78a1ee0 --- /dev/null +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Combi.java @@ -0,0 +1,105 @@ +// SPDX-FileCopyrightText: 2025 Jonas Smedegaard +// SPDX-License-Identifier: GPL-3.0-or-later + +package dk.biks.bachelorizer; + +import java.util.Collection; +import java.util.Set; + +import com.example.portfolio3.AdjListGraph; +import com.example.portfolio3.Graph; +import com.example.portfolio3.GraphAlgorithms; +import com.example.portfolio3.MatrixGraph; +import com.example.portfolio3.Vertex; + +/// Combi - static sample dataset of course combinations +/// +/// Slurps and parses data from upstream-provided comma-separated file. +/// +/// @version 0.0.1 +/// @see +public final class Combi { + + /// data about combinations as a Graph + private Graph g; + + /// Default constructor + /// + /// @param path path to data file + private Combi(final String path) { + + // read into temporary graph to resolve vertice count + // + // use Adjacency List Representation: + // * cheap to bootstrap (done once) + // * simple to count vertices (done once): O(1) + Graph _g = new AdjListGraph(); + GraphAlgorithms.readGraph(_g, path); + Integer _vertice_count = _g.vertices().size(); + + // read into final graph + // + // use Adjacency Matrix Representation: + // * expensive to bootstrap (done once) + // * simple to add edges but needs vertice count + // * simple to get edges (done repeatedly): O(1) + // + // TODO: avoid reading and parsing file twice + Graph g = new MatrixGraph(_vertice_count); + GraphAlgorithms.readGraph(g, path); + + // ensure the graph is connected + isConnected(g); + + GraphAlgorithms.printGraph(g); + + // release temporary variables for garbage collection + _g = null; + _vertice_count = null; + } + + /// JVM entry point + /// + /// @param args command-line arguments + public static void main(final String[] args) { + + // first argument, if provided, is the data file path; + // else use upstream named file in current directory. + String path = (args.length > 0) + ? args[0] + : "combi.txt"; + + Combi combi = new Combi(path); + } + + /// utility function to check that a graph is connected + /// + /// If check fails, throw an unchecked exception, + /// since it occurs at runtime and is unrecoverable. + /// + /// Time complexity of the operation is O(n²) + /// where n is the amount of vertices, + /// since visitDepthFirst() recurses out-edges of all vertices. + /// + /// @param g Graph to inspect + /// @throws IllegalArgumentException + /// https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html + public static void isConnected(Graph g) { + + // collect all vertices in the graph + Collection c = g.vertices(); + + // pick a random vertice in the graph + Vertex v = g.vertices().iterator().next(); + + // collect the set of visitable vertices + Set visited = GraphAlgorithms.visitDepthFirst( + g, v); + + // throw exception if not all vertices were visited + if (visited.size() != c.size()) { + throw new IllegalArgumentException( + "graph is not connected"); + } + } +} diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java index 985c026..320b36c 100644 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java @@ -5,8 +5,6 @@ package dk.biks.bachelorizer; import java.util.List; -import dk.biks.bachelorizer.model.GUI; - /// Bachelorizer - Controller public class Control{ diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java new file mode 100644 index 0000000..695c19c --- /dev/null +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java @@ -0,0 +1,60 @@ +// SPDX-FileCopyrightText: 2025 Jonas Smedegaard +// SPDX-License-Identifier: GPL-3.0-or-later + +package dk.biks.bachelorizer; + +import java.util.ArrayList; + +/// Bachelorizer - GUI model +public class GUI{ + + /// Default constructor + // (declared explicitly only to silence javadoc) + public GUI(){ + } + + /// Activity list + private Person student; + + /// Activity list + private ArrayList list = new ArrayList<>(); + + /// Add student + /// + /// @param name Name of student + public void addStudent(String name){ + student = new Person(name); + } + + /// Get student name + /// + /// @return name of student + public String getStudentName(){ + return student.name; + } + + /// Add activity to list + /// + /// @param s Activity to add + public void addActivity(String s){ + list.add(s); + } + + /// Get list of activities + /// + /// @return activity list + public ArrayList getActivities(){ + return list; + } + + /// Delete last activity from list + public void delOneActivity(){ + if(list.size()>0) + list.remove(list.size()-1); + } + + /// Delete all activities from list + public void delAllActivities(){ + list.clear(); + } +} diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Person.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Person.java new file mode 100644 index 0000000..f7eeab3 --- /dev/null +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Person.java @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2025 Jonas Smedegaard +// SPDX-License-Identifier: GPL-3.0-or-later + +package dk.biks.bachelorizer; + +/// Bachelorizer - Person model +public class Person { + + /// Person name + public String name; + + /// Constructor + /// + /// @param name Name of person + public Person (String name) { + this.name = name; + } +} diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Window.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Window.java index 7bdc88a..85638c8 100644 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Window.java +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Window.java @@ -14,8 +14,6 @@ import javafx.scene.layout.VBox; import javafx.scene.Scene; import javafx.stage.Stage; -import dk.biks.bachelorizer.model.GUI; - /// Bachelorizer - JavaFX Window view // Class is final to forbid subclassing, // because object is passed to controller during instatiation diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Combi.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Combi.java deleted file mode 100644 index 371eb79..0000000 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Combi.java +++ /dev/null @@ -1,105 +0,0 @@ -// SPDX-FileCopyrightText: 2025 Jonas Smedegaard -// SPDX-License-Identifier: GPL-3.0-or-later - -package dk.biks.bachelorizer.model; - -import java.util.Collection; -import java.util.Set; - -import com.example.portfolio3.AdjListGraph; -import com.example.portfolio3.Graph; -import com.example.portfolio3.GraphAlgorithms; -import com.example.portfolio3.MatrixGraph; -import com.example.portfolio3.Vertex; - -/// Combi - static sample dataset of course combinations -/// -/// Slurps and parses data from upstream-provided comma-separated file. -/// -/// @version 0.0.1 -/// @see -public final class Combi { - - /// data about combinations as a Graph - private Graph g; - - /// Default constructor - /// - /// @param path path to data file - private Combi(final String path) { - - // read into temporary graph to resolve vertice count - // - // use Adjacency List Representation: - // * cheap to bootstrap (done once) - // * simple to count vertices (done once): O(1) - Graph _g = new AdjListGraph(); - GraphAlgorithms.readGraph(_g, path); - Integer _vertice_count = _g.vertices().size(); - - // read into final graph - // - // use Adjacency Matrix Representation: - // * expensive to bootstrap (done once) - // * simple to add edges but needs vertice count - // * simple to get edges (done repeatedly): O(1) - // - // TODO: avoid reading and parsing file twice - Graph g = new MatrixGraph(_vertice_count); - GraphAlgorithms.readGraph(g, path); - - // ensure the graph is connected - isConnected(g); - - GraphAlgorithms.printGraph(g); - - // release temporary variables for garbage collection - _g = null; - _vertice_count = null; - } - - /// JVM entry point - /// - /// @param args command-line arguments - public static void main(final String[] args) { - - // first argument, if provided, is the data file path; - // else use upstream named file in current directory. - String path = (args.length > 0) - ? args[0] - : "combi.txt"; - - Combi combi = new Combi(path); - } - - /// utility function to check that a graph is connected - /// - /// If check fails, throw an unchecked exception, - /// since it occurs at runtime and is unrecoverable. - /// - /// Time complexity of the operation is O(n²) - /// where n is the amount of vertices, - /// since visitDepthFirst() recurses out-edges of all vertices. - /// - /// @param g Graph to inspect - /// @throws IllegalArgumentException - /// https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html - public static void isConnected(Graph g) { - - // collect all vertices in the graph - Collection c = g.vertices(); - - // pick a random vertice in the graph - Vertex v = g.vertices().iterator().next(); - - // collect the set of visitable vertices - Set visited = GraphAlgorithms.visitDepthFirst( - g, v); - - // throw exception if not all vertices were visited - if (visited.size() != c.size()) { - throw new IllegalArgumentException( - "graph is not connected"); - } - } -} diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/GUI.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/GUI.java deleted file mode 100644 index ca9bd86..0000000 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/GUI.java +++ /dev/null @@ -1,62 +0,0 @@ -// SPDX-FileCopyrightText: 2025 Jonas Smedegaard -// SPDX-License-Identifier: GPL-3.0-or-later - -package dk.biks.bachelorizer.model; - -import java.util.ArrayList; - -import dk.biks.bachelorizer.model.Person; - -/// Bachelorizer - GUI model -public class GUI{ - - /// Default constructor - // (declared explicitly only to silence javadoc) - public GUI(){ - } - - /// Activity list - private Person student; - - /// Activity list - private ArrayList list = new ArrayList<>(); - - /// Add student - /// - /// @param name Name of student - public void addStudent(String name){ - student = new Person(name); - } - - /// Get student name - /// - /// @return name of student - public String getStudentName(){ - return student.name; - } - - /// Add activity to list - /// - /// @param s Activity to add - public void addActivity(String s){ - list.add(s); - } - - /// Get list of activities - /// - /// @return activity list - public ArrayList getActivities(){ - return list; - } - - /// Delete last activity from list - public void delOneActivity(){ - if(list.size()>0) - list.remove(list.size()-1); - } - - /// Delete all activities from list - public void delAllActivities(){ - list.clear(); - } -} diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Person.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Person.java deleted file mode 100644 index 9edb7af..0000000 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/model/Person.java +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-FileCopyrightText: 2025 Jonas Smedegaard -// SPDX-License-Identifier: GPL-3.0-or-later - -package dk.biks.bachelorizer.model; - -/// Bachelorizer - Person model -public class Person { - - /// Person name - public String name; - - /// Constructor - /// - /// @param name Name of person - public Person (String name) { - this.name = name; - } -} -- cgit v1.2.3