Work on this assignment is going so freaking slow

master
Chloe Fontenot 🏳️‍⚧️ 2023-11-13 09:20:57 +07:00
parent 9ef4210608
commit af97eb2bc8
18 changed files with 1713 additions and 396 deletions

@ -1 +0,0 @@
,caleb,caleb-gaming-laptop-archlinux,20.10.2023 21:26,file:///home/caleb/.config/libreoffice/4;

@ -0,0 +1,286 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>AddressBookFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST2 {color: #ffc66d}
.ST5 {color: #9876aa}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/AddressBookFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> java.io.ObjectInputStream;
<span class="literal">import</span> java.io.ObjectOutputStream;
<span class="literal">import</span> java.io.Serializable;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> javafx.application.Application;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">geometry</span><span class="comment">.</span><span class="comment">HPos</span><span class="comment">;</span>
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.TextField;
<span class="literal">import</span> javafx.scene.layout.ColumnConstraints;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">scene</span><span class="comment">.</span><span class="comment">layout</span><span class="comment">.</span><span class="comment">RowConstraints</span><span class="comment">;</span>
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> AddressBookFX <span class="literal">extends</span> Application {
<span class="literal">static</span> File <span class="ST1">addressBook</span> = <span class="literal">new</span> File(<span class="string">&quot;</span><span class="string">AddressBookFX.dat</span><span class="string">&quot;</span>);
<span class="literal">static</span> <span class="literal">int</span> <span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
<span class="literal">static</span> ArrayList&lt;AddressBookEntry&gt; <span class="ST1">addressArray</span> = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">static</span> TextField <span class="ST1">nameTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">streetTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">cityTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">stateTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">zipTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> Label <span class="ST1">addressBookCounter</span> = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Items in address book:</span><span class="string">&quot;</span>);
<span class="literal">static</span> Label <span class="ST1">indexLabel</span> = <span class="literal">new</span> Label();
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">start</span>(Stage stage) <span class="literal">throws</span> Exception
{
<span class="ST3">getData</span>();
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
GridPane addressFieldPane = <span class="literal">new</span> GridPane();
GridPane buttonFieldPane = <span class="literal">new</span> GridPane();
<span class="comment">// Text fields / labels</span>
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Name</span><span class="string">&quot;</span>), <span class="number">0</span>, <span class="number">0</span>);
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Street</span><span class="string">&quot;</span>), <span class="number">0</span>, <span class="number">1</span>);
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">City</span><span class="string">&quot;</span>), <span class="number">0</span>, <span class="number">2</span>);
addressFieldPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">State</span><span class="string">&quot;</span>), <span class="number">2</span>, <span class="number">0</span>);
addressFieldPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">ZIP</span><span class="string">&quot;</span>), <span class="number">4</span>, <span class="number">0</span>);
textFieldGridPane.add(<span class="ST1">n</span><span class="ST1">ameTextField</span>, <span class="number">1</span>, <span class="number">0</span>);
textFieldGridPane.add(<span class="ST1">s</span><span class="ST1">treetTextField</span>, <span class="number">1</span>, <span class="number">1</span>);
textFieldGridPane.add(addressFieldPane, <span class="number">1</span>, <span class="number">2</span>);
addressFieldPane.add(<span class="ST1">c</span><span class="ST1">ityTextField</span>, <span class="number">1</span>, <span class="number">0</span>);
addressFieldPane.add(<span class="ST1">s</span><span class="ST1">tateTextField</span>, <span class="number">3</span>, <span class="number">0</span>);
addressFieldPane.add(<span class="ST1">z</span><span class="ST1">ipTextField</span>, <span class="number">5</span>, <span class="number">0</span>);
textFieldGridPane.getColumnConstraints().add(<span class="literal">new</span> ColumnConstraints(<span class="number">5</span><span class="number">0</span>));
primaryBorderPane.setTop(textFieldGridPane);
<span class="comment">// Buttons</span>
Button addButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Add</span><span class="string">&quot;</span>);
Button firstButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">First</span><span class="string">&quot;</span>);
Button nextButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Next</span><span class="string">&quot;</span>);
Button previousButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Previous</span><span class="string">&quot;</span>);
Button lastButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Last</span><span class="string">&quot;</span>);
Button updateButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Update</span><span class="string">&quot;</span>);
buttonFieldPane.add(<span class="ST1">i</span><span class="ST1">ndexLabel</span>, <span class="number">0</span>, <span class="number">0</span>);
buttonFieldPane.add(addButton, <span class="number">1</span>, <span class="number">0</span>);
buttonFieldPane.add(firstButton, <span class="number">2</span>, <span class="number">0</span>);
buttonFieldPane.add(nextButton, <span class="number">3</span>, <span class="number">0</span>);
buttonFieldPane.add(previousButton, <span class="number">4</span>, <span class="number">0</span>);
buttonFieldPane.add(lastButton, <span class="number">5</span>, <span class="number">0</span>);
buttonFieldPane.add(updateButton, <span class="number">6</span>, <span class="number">0</span>);
buttonFieldPane.add(<span class="ST1">a</span><span class="ST1">ddressBookCounter</span>, <span class="number">7</span>, <span class="number">0</span>);
primaryBorderPane.setBottom(buttonFieldPane);
buttonFieldPane.setAlignment(Pos.<span class="ST1">CENTER</span>);
buttonFieldPane.setHgap(<span class="number">1</span><span class="number">0</span>);
addButton.setOnAction(e -&gt; {
<span class="ST1">addressArray</span>.add(<span class="literal">new</span> AddressBookEntry());
<span class="ST3">updateData</span>();
});
firstButton.setOnAction(e -&gt; {
<span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
nextButton.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST1">addressArrayPointer</span> &gt;= <span class="ST1">addressArray</span>.size() - <span class="number">1</span>) {
<span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
} <span class="literal">else</span> {
<span class="ST1">addressArrayPointer</span>++;
}
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
previousButton.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST1">addressArrayPointer</span> &gt; <span class="number">0</span>) {
<span class="ST1">addressArrayPointer</span>--;
} <span class="literal">else</span> {
<span class="ST1">addressArrayPointer</span> = <span class="ST1">addressArray</span>.size() - <span class="number">1</span>;
}
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
lastButton.setOnAction(e -&gt; {
<span class="ST1">addressArrayPointer</span> = <span class="ST1">addressArray</span>.size() - <span class="number">1</span>;
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
updateButton.setOnAction(e -&gt; {
AddressBookEntry entry = <span class="ST1">addressArray</span>.get(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
entry.setName(<span class="ST1">n</span><span class="ST1">ameTextField</span>.getText());
entry.setCity(<span class="ST1">n</span><span class="ST1">ameTextField</span>.getText());
entry.setStreet(<span class="ST1">s</span><span class="ST1">treetTextField</span>.getText());
entry.setCity(<span class="ST1">c</span><span class="ST1">ityTextField</span>.getText());
entry.setState(<span class="ST1">s</span><span class="ST1">tateTextField</span>.getText());
entry.setZip(<span class="ST1">z</span><span class="ST1">ipTextField</span>.getText());
<span class="ST3">updateData</span>();
});
<span class="comment">// init fields with data from first entry</span>
<span class="ST3">getEntry</span>(<span class="number">0</span>);
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
<span class="literal">public</span> <span class="literal">static</span> AddressBookEntry <span class="ST4">getEntry</span>(<span class="literal">int</span> index)
{
AddressBookEntry entry = <span class="ST1">addressArray</span>.get(index);
<span class="ST1">indexLabel</span>.setText(<span class="string">&quot;</span><span class="string">Current index: </span><span class="string">&quot;</span> + index);
<span class="ST1">addressBookCounter</span>.setText(<span class="string">&quot;</span><span class="string">Items in address book: </span><span class="string">&quot;</span> + <span class="ST1">addressArray</span>.size());
<span class="ST1">nameTextField</span>.setText(entry.getName());
<span class="ST1">streetTextField</span>.setText(entry.getStreet());
<span class="ST1">cityTextField</span>.setText(entry.getCity());
<span class="ST1">stateTextField</span>.setText(entry.getState());
<span class="ST1">zipTextField</span>.setText(entry.getZip());
<span class="literal">return</span> entry;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">updateData</span>()
{
<span class="literal">try</span> (ObjectOutputStream fileStream = <span class="literal">new</span> ObjectOutputStream(<span class="literal">new</span> FileOutputStream(<span class="ST1">a</span><span class="ST1">ddressBook</span>))) {
fileStream.writeObject(<span class="ST1">a</span><span class="ST1">ddressArray</span>);
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST1">out</span>.println(ex);
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">getData</span>()
{
<span class="literal">if</span> (!<span class="ST1">addressBook</span>.exists()) {
<span class="ST1">addressArray</span>.add(<span class="literal">new</span> AddressBookEntry());
<span class="ST3">updateData</span>();
} <span class="literal">else</span> {
<span class="literal">try</span> (ObjectInputStream fileStream = <span class="literal">new</span> ObjectInputStream(<span class="literal">new</span> FileInputStream(<span class="ST1">a</span><span class="ST1">ddressBook</span>))) {
<span class="ST1">addressArray</span> = (ArrayList&lt;AddressBookEntry&gt;) fileStream.readObject();
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST1">out</span>.println(ex);
} <span class="literal">catch</span> (ClassNotFoundException ex) {
System.<span class="ST1">out</span>.println(ex);
}
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args)
{
<span class="ST3">launch</span>();
}
}
<span class="literal">class</span> AddressBookEntry <span class="literal">implements</span> Serializable {
<span class="literal">private</span> String <span class="ST5">name</span>;
<span class="literal">private</span> String <span class="ST5">street</span>;
<span class="literal">private</span> String <span class="ST5">city</span>;
<span class="literal">private</span> String <span class="ST5">state</span>;
<span class="literal">private</span> String <span class="ST5">zip</span>;
<span class="literal">public</span> AddressBookEntry()
{
<span class="literal">this</span>.<span class="ST5">name</span> = <span class="string">&quot;</span><span class="string">Enter a name here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">street</span> = <span class="string">&quot;</span><span class="string">Enter a street here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">city</span> = <span class="string">&quot;</span><span class="string">Enter a city here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">state</span> = <span class="string">&quot;</span><span class="string">Enter a state here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">zip</span> = <span class="string">&quot;</span><span class="string">Enter a zip here.</span><span class="string">&quot;</span>;
}
<span class="literal">public</span> AddressBookEntry(String name, String street, String city, String state, String zip)
{
<span class="literal">this</span>.<span class="ST5">name</span> = name;
<span class="literal">this</span>.<span class="ST5">street</span> = street;
<span class="literal">this</span>.<span class="ST5">city</span> = city;
<span class="literal">this</span>.<span class="ST5">state</span> = state;
<span class="literal">this</span>.<span class="ST5">zip</span> = zip;
}
<span class="literal">public</span> String <span class="ST2">getZip</span>()
{
<span class="literal">return</span> <span class="ST5">zip</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setZip</span>(String zip)
{
<span class="literal">this</span>.<span class="ST5">zip</span> = zip;
}
<span class="literal">public</span> String <span class="ST2">getState</span>()
{
<span class="literal">return</span> <span class="ST5">state</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setState</span>(String state)
{
<span class="literal">this</span>.<span class="ST5">state</span> = state;
}
<span class="literal">public</span> String <span class="ST2">getCity</span>()
{
<span class="literal">return</span> <span class="ST5">city</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setCity</span>(String city)
{
<span class="literal">this</span>.<span class="ST5">city</span> = city;
}
<span class="literal">public</span> String <span class="ST2">getStreet</span>()
{
<span class="literal">return</span> <span class="ST5">street</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setStreet</span>(String street)
{
<span class="literal">this</span>.<span class="ST5">street</span> = street;
}
<span class="literal">public</span> String <span class="ST2">getName</span>()
{
<span class="literal">return</span> <span class="ST5">name</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setName</span>(String name)
{
<span class="literal">this</span>.<span class="ST5">name</span> = name;
}
}
</pre></body>
</html>

@ -0,0 +1,160 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CombineFilesFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST3 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST1 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/CombineFilesFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">DataInput</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Collections;
<span class="literal">import</span> java.util.Comparator;
<span class="literal">import</span> java.util.List;
<span class="literal">import</span> java.util.regex.Matcher;
<span class="literal">import</span> java.util.regex.Pattern;
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Alert;
<span class="literal">import</span> javafx.scene.control.Alert.<span class="ST1">AlertType</span>;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">scene</span><span class="comment">.</span><span class="comment">control</span><span class="comment">.</span><span class="comment">TextField</span><span class="comment">;</span>
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> CombineFilesFX <span class="literal">extends</span> Application {
<span class="literal">static</span> ArrayList&lt;File&gt; <span class="ST2">files</span> = <span class="literal">new</span> ArrayList&lt;File&gt;();
<span class="literal">final</span> FileChooser <span class="ST3">fileChooser</span> = <span class="literal">new</span> FileChooser();
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">start</span>(<span class="literal">final</span> Stage stage) <span class="literal">throws</span> Exception {
<span class="ST3">fileChooser</span>.setTitle(<span class="string">&quot;</span><span class="string">Open File to Split...</span><span class="string">&quot;</span>);
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
Label infoLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Point me at the files that were output by SplitFilesFX.java.</span><span class="string">&quot;</span>);
Label chooseFile = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Choose files to combine: </span><span class="string">&quot;</span>);
Button openFilePicker = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
Button run = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Start</span><span class="string">&quot;</span>);
textFieldGridPane.add(chooseFile, <span class="number">0</span>, <span class="number">0</span>);
textFieldGridPane.add(openFilePicker, <span class="number">1</span>, <span class="number">0</span>);
primaryBorderPane.<span class="ST1">setAlignment</span>(run, Pos.<span class="ST2">CENTER</span>);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
primaryBorderPane.setBottom(run);
openFilePicker.setOnAction(e -&gt; {
<span class="comment">// When we initially get the files from the file chooser, it returns an unsortable list. Because of this, we need to create a new List with its data.</span>
<span class="ST2">files</span>.addAll(<span class="ST3">f</span><span class="ST3">ileChooser</span>.showOpenMultipleDialog(openFilePicker.getScene().getWindow()));
<span class="comment">// The file picker appears to return files in a random order, so we need to sort them by file name.</span>
Collections.<span class="ST1">sort</span>(<span class="ST2">f</span><span class="ST2">iles</span>, <span class="literal">new</span> Comparator&lt;File&gt;() {
<span class="literal">public</span> <span class="literal">int</span> <span class="ST4">compare</span>(File o1, File o2) {
<span class="literal">return</span> extractInt(o1.getName()) - extractInt(o2.getName());
}
String <span class="ST4">findMatch</span>(String s) {
Pattern findNum = Pattern.<span class="ST1">compile</span>(<span class="string">&quot;</span><span class="literal">\\</span><span class="string">d+$</span><span class="string">&quot;</span>);
Matcher match = findNum.matcher(s);
<span class="literal">while</span> (match.find()) {
<span class="literal">return</span> match.group();
}
<span class="literal">return</span> <span class="string">&quot;&quot;</span>;
}
<span class="literal">int</span> <span class="ST4">extractInt</span>(String s) {
String num = findMatch(s);
System.<span class="ST2">out</span>.println(num);
<span class="comment">// return 0 if no digits found</span>
<span class="literal">return</span> num.isEmpty() ? <span class="number">0</span> : Integer.<span class="ST1">parseInt</span>(num);
}
});
});
run.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST2">files</span> == <span class="literal">null</span>) {
e.consume();
}
<span class="ST1">combineFiles</span>(<span class="ST2">f</span><span class="ST2">iles</span>);
});
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">combineFiles</span>(List&lt;File&gt; filesToCombine) {
String outputPath = filesToCombine.get(<span class="number">0</span>).getParent();
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">reconstructed_</span><span class="string">&quot;</span> + filesToCombine.get(<span class="number">0</span>).getName().substring(<span class="number">0</span>, (filesToCombine.get(<span class="number">0</span>).getName().length() - <span class="number">2</span>)))) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Writing to </span><span class="string">&quot;</span> + outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">reconstructed_</span><span class="string">&quot;</span> + filesToCombine.get(<span class="number">0</span>).getName().substring(<span class="number">0</span>, (filesToCombine.get(<span class="number">0</span>).getName().length() - <span class="number">2</span>)));
<span class="literal">for</span> (File file : filesToCombine) {
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(file)) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Opening the source file </span><span class="string">&quot;</span> + file.getName() + <span class="string">&quot;</span><span class="string">!</span><span class="string">&quot;</span>);
<span class="literal">byte</span>[] buffer = <span class="literal">new</span> <span class="literal">byte</span>[<span class="number">4096</span>];
<span class="literal">int</span> bytesRead = -<span class="number">1</span>;
<span class="literal">while</span> ((bytesRead = dataIn.read(buffer)) != -<span class="number">1</span>) {
dataOut.write(buffer, <span class="number">0</span>, bytesRead);
}
}
<span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
}
}
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
}
Alert alert = <span class="literal">new</span> Alert(<span class="ST1">A</span><span class="ST1">lertType</span>.<span class="ST2">INFORMATION</span>);
alert.setTitle(<span class="string">&quot;</span><span class="string">Success!</span><span class="string">&quot;</span>);
alert.setContentText(<span class="string">&quot;</span><span class="string">Successfully combined the files!</span><span class="string">&quot;</span>);
alert.showAndWait();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
<span class="ST1">launch</span>();
}
}
</pre></body>
</html>

@ -0,0 +1,80 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Exercise17_01.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_01.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.EOFException;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">FileNotFoundException</span><span class="comment">;</span>
<span class="literal">import</span> java.io.RandomAccessFile;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Exercise17_01 {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
{
<span class="literal">try</span> (RandomAccessFile fileIO = <span class="literal">new</span> RandomAccessFile(<span class="string">&quot;</span><span class="string">Exercise17_01.txt</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">rw</span><span class="string">&quot;</span>)) {
<span class="literal">int</span> random = <span class="number">0</span>;
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Writing data to file...</span><span class="string">&quot;</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i &lt; <span class="number">100</span> + <span class="number">1</span>; ++i) {
random = (<span class="literal">int</span>) (Math.<span class="ST3">random</span>() * <span class="number">9</span>) + <span class="number">1</span>;
System.<span class="ST2">out</span>.print(random + <span class="string">&quot;</span> <span class="string">&quot;</span>);
<span class="literal">if</span> (i != <span class="number">0</span> &amp;&amp; i % <span class="number">10</span> == <span class="number">0</span>) {
System.<span class="ST2">out</span>.println();
}
fileIO.writeInt(random);
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Wrote to the file successfully!</span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">File contents:</span><span class="string">&quot;</span>);
fileIO.seek(<span class="number">0</span>);
<span class="literal">int</span> readIterator = <span class="number">1</span>;
<span class="literal">while</span> (<span class="literal">true</span>) {
<span class="literal">try</span> {
System.<span class="ST2">out</span>.print(fileIO.readInt() + <span class="string">&quot;</span> <span class="string">&quot;</span>);
<span class="literal">if</span> (readIterator != <span class="number">0</span> &amp;&amp; readIterator % <span class="number">10</span> == <span class="number">0</span>) {
System.<span class="ST2">out</span>.println();
}
++readIterator;
} <span class="literal">catch</span> (EOFException e) {
<span class="literal">break</span>;
}
}
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST2">out</span>.println(ex);
}
}
}
</pre></body>
</html>

@ -0,0 +1,94 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Exercise17_03.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_03.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.DataInputStream;
<span class="literal">import</span> java.io.DataOutputStream;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Exercise17_03 {
<span class="literal">static</span> <span class="literal">int</span> fileSize = Math.abs((<span class="literal">int</span>) (Math.random() * <span class="number">1024</span>));
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> writeData()
{
<span class="literal">try</span> (FileOutputStream fileWrite = <span class="literal">new</span> FileOutputStream(<span class="string">&quot;</span><span class="string">Exercise17_03.dat</span><span class="string">&quot;</span>)) {
<span class="comment">// Write a unspecified number of integers into the file.</span>
<span class="comment">// Must be positive!</span>
<span class="literal">int</span> randInt = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; fileSize; ++i) {
randInt = (<span class="literal">int</span>) (Math.random() * <span class="number">10</span>);
fileWrite.write(randInt);
}
} <span class="literal">catch</span> (IOException ex) {
System.out.println(ex);
}
System.out.println(<span class="string">&quot;</span><span class="string">Wrote data to the file!</span><span class="string">&quot;</span>);
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span>[] readData()
{
<span class="literal">int</span>[] fileData = <span class="literal">new</span> <span class="literal">int</span>[fileSize];
<span class="literal">try</span> (FileInputStream fileRead = <span class="literal">new</span> FileInputStream(<span class="string">&quot;</span><span class="string">Exercise17_03.dat</span><span class="string">&quot;</span>)) {
<span class="comment">// Read the data back</span>
<span class="literal">int</span> dataIterator = <span class="number">0</span>;
<span class="literal">int</span> dataStream = <span class="number">0</span>;
<span class="literal">while</span> (fileRead.available() &gt; <span class="number">0</span>) {
dataStream = fileRead.read();
fileData[dataIterator++] = dataStream;
System.out.print(dataStream + <span class="string">&quot;</span> <span class="string">&quot;</span>);
<span class="literal">if</span> ((dataIterator + <span class="number">1</span>) % <span class="number">10</span> == <span class="number">0</span>) {
System.out.println();
}
}
} <span class="literal">catch</span> (IOException ex) {
System.out.println(ex);
}
<span class="literal">return</span> fileData;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
{
System.out.println(<span class="string">&quot;</span><span class="string">Ints to write: </span><span class="string">&quot;</span> + fileSize);
writeData();
<span class="literal">int</span>[] fileData = readData();
<span class="comment">// Sum the digits</span>
<span class="literal">int</span> sum = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> i: fileData) {
sum += i;
}
System.out.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">The sum of the integers in the file is: </span><span class="string">&quot;</span> + sum);
}
}
</pre></body>
</html>

@ -0,0 +1,107 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Exercise17_05.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST3 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_05.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">FileNotFoundException</span><span class="comment">;</span>
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> java.io.ObjectInputStream;
<span class="literal">import</span> java.io.Serializable;
<span class="literal">import</span> java.io.ObjectOutputStream;
<span class="literal">import</span> java.util.Date;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Exercise17_05 {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
File dataFile = <span class="literal">new</span> File(<span class="string">&quot;</span><span class="string">Exercise17_05.dat</span><span class="string">&quot;</span>);
<span class="literal">if</span>(!dataFile.exists()) {
<span class="literal">try</span> (ObjectOutputStream fileStream = <span class="literal">new</span> ObjectOutputStream(<span class="literal">new</span> FileOutputStream(dataFile))) {
fileStream.writeObject(<span class="literal">new</span> DataContainer());
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
}
}
DataContainer data = <span class="literal">null</span>;
<span class="literal">try</span> (ObjectInputStream fileStream = <span class="literal">new</span> ObjectInputStream(<span class="literal">new</span> FileInputStream(dataFile))) {
data = (DataContainer) fileStream.readObject();
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
} <span class="literal">catch</span> (ClassNotFoundException ex) {
System.<span class="ST2">out</span>.println(ex);
}
<span class="comment">// Now print out the data!</span>
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">We got the data from the file!</span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(data.toString());
}
}
<span class="literal">class</span> DataContainer <span class="literal">implements</span> Serializable {
<span class="literal">int</span>[] <span class="ST3">intArray</span> = {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>};
Date <span class="ST3">currentDate</span> = <span class="literal">new</span> Date();
<span class="literal">double</span> <span class="ST3">doubleMoment</span> = <span class="number">5.5</span>;
<span class="literal">public</span> <span class="literal">int</span>[] <span class="ST4">getIntArray</span>()
{
<span class="literal">return</span> <span class="ST3">intArray</span>;
}
<span class="literal">public</span> Date <span class="ST4">getCurrentDate</span>()
{
<span class="literal">return</span> <span class="ST3">currentDate</span>;
}
<span class="literal">public</span> <span class="literal">double</span> <span class="ST4">getDoubleMoment</span>()
{
<span class="literal">return</span> <span class="ST3">doubleMoment</span>;
}
@Override
<span class="literal">public</span> String <span class="ST4">toString</span>()
{
String intString = <span class="string">&quot;</span><span class="string">[</span><span class="string">&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST3">intArray</span>.<span class="ST3">length</span> - <span class="number">1</span>; ++i) {
intString += <span class="ST3">intArray</span>[i];
<span class="literal">if</span> (i == (<span class="ST3">intArray</span>.<span class="ST3">length</span> - <span class="number">2</span>)) {
intString += <span class="string">&quot;</span><span class="string">]</span><span class="string">&quot;</span>;
} <span class="literal">else</span> {
intString += <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span>;
}
}
<span class="literal">return</span> <span class="string">&quot;</span><span class="string">DataContainer{</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">intArray=</span><span class="string">&quot;</span> + intString + <span class="string">&quot;</span><span class="string">, currentDate=</span><span class="string">&quot;</span> + <span class="ST3">currentDate</span> + <span class="string">&quot;</span><span class="string">, doubleMoment=</span><span class="string">&quot;</span> + <span class="ST3">doubleMoment</span> + <span class="string">&#39;</span><span class="string">}</span><span class="string">&#39;</span>;
}
}
</pre></body>
</html>

@ -0,0 +1,177 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HexEditorFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST1 {color: #9876aa}
.ST3 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/HexEditorFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> <span class="literal">static</span> com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.alert;
<span class="comment">import</span> <span class="comment">static</span> <span class="comment">com</span><span class="comment">.</span><span class="comment">calebfontenot</span><span class="comment">.</span><span class="comment">mp5</span><span class="comment">.</span><span class="comment">files_calebfontenot</span><span class="comment">.</span><span class="comment">SplitFilesFX</span><span class="comment">.</span><span class="comment">file</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileNotFoundException;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">ArrayList</span><span class="comment">;</span>
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.TextArea;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> HexEditorFX <span class="literal">extends</span> Application {
<span class="literal">final</span> FileChooser <span class="ST1">fileChooser</span> = <span class="literal">new</span> FileChooser();
<span class="literal">static</span> File <span class="ST2">file</span> = <span class="literal">null</span>;
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">start</span>(Stage stage) <span class="literal">throws</span> Exception {
Label label = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Open a file...</span><span class="string">&quot;</span>);
Button openFileButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
TextArea editorWindow = <span class="literal">new</span> TextArea();
Button saveFileButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Save</span><span class="string">&quot;</span>);
editorWindow.setWrapText(<span class="literal">t</span><span class="literal">rue</span>);
BorderPane bp = <span class="literal">new</span> BorderPane();
GridPane openFileGridPane = <span class="literal">new</span> GridPane();
openFileGridPane.add(label, <span class="number">0</span>, <span class="number">0</span>);
openFileGridPane.add(openFileButton, <span class="number">1</span>, <span class="number">0</span>);
bp.setTop(openFileGridPane);
bp.setCenter(editorWindow);
bp.setBottom(saveFileButton);
bp.<span class="ST4">setAlignment</span>(saveFileButton, Pos.<span class="ST2">CENTER</span>);
openFileButton.setOnAction(e -&gt; {
<span class="ST2">file</span> = <span class="ST1">fileChooser</span>.showOpenDialog(openFileButton.getScene().getWindow());
<span class="literal">byte</span>[] data = <span class="literal">null</span>;
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Getting data from fileand encoding it as hex...</span><span class="string">&quot;</span>);
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(<span class="ST2">file</span>)) {
data = dataIn.readAllBytes();
} <span class="literal">catch</span> (FileNotFoundException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IOException ex) {
ex.printStackTrace();
}
String dataString = encodeHexString(data);
editorWindow.setText(dataString);
});
saveFileButton.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST2">file</span> == <span class="literal">null</span>) {
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">No file selected!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setHeaderText(<span class="string">&quot;</span><span class="string">No file selected!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">You have to open a file first, silly!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Re-encoding hex back into a byte array...</span><span class="string">&quot;</span>);
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(<span class="ST2">file</span>)) {
<span class="literal">byte</span>[] bytesToSave = decodeHexString(editorWindow.getText());
dataOut.write(bytesToSave);
} <span class="literal">catch</span> (FileNotFoundException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IOException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IllegalArgumentException ex) {
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Invalid Hex!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">Invalid hex entered into the text box, unable to save.</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
}
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Save successful!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">File saved successfully.</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
});
Scene scene = <span class="literal">new</span> Scene(bp);
stage.setScene(scene);
stage.show();
}
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">toDigit</span>(<span class="literal">char</span> hexChar) {
<span class="literal">int</span> digit = Character.<span class="ST4">digit</span>(hexChar, <span class="number">1</span><span class="number">6</span>);
<span class="literal">if</span> (digit == -<span class="number">1</span>) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(
<span class="string">&quot;</span><span class="string">Invalid Hexadecimal Character: </span><span class="string">&quot;</span> + hexChar);
}
<span class="literal">return</span> digit;
}
<span class="literal">public</span> <span class="literal">byte</span> <span class="ST3">hexToByte</span>(String hexString) {
<span class="literal">int</span> firstDigit = toDigit(hexString.charAt(<span class="number">0</span>));
<span class="literal">int</span> secondDigit = toDigit(hexString.charAt(<span class="number">1</span>));
<span class="literal">return</span> (<span class="literal">byte</span>) ((firstDigit &lt;&lt; <span class="number">4</span>) + secondDigit);
}
<span class="literal">public</span> <span class="literal">byte</span>[] <span class="ST3">decodeHexString</span>(String hexString) {
<span class="literal">if</span> (hexString.length() % <span class="number">2</span> == <span class="number">1</span>) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(
<span class="string">&quot;</span><span class="string">Invalid hexadecimal String supplied.</span><span class="string">&quot;</span>);
}
<span class="literal">byte</span>[] bytes = <span class="literal">new</span> <span class="literal">byte</span>[hexString.length() / <span class="number">2</span>];
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; hexString.length(); i += <span class="number">2</span>) {
bytes[i / <span class="number">2</span>] = hexToByte(hexString.substring(i, i + <span class="number">2</span>));
}
<span class="literal">return</span> bytes;
}
<span class="literal">public</span> String <span class="ST3">byteToHex</span>(<span class="literal">byte</span> num) {
<span class="literal">char</span>[] hexDigits = <span class="literal">new</span> <span class="literal">char</span>[<span class="number">2</span>];
hexDigits[<span class="number">0</span>] = Character.<span class="ST4">forDigit</span>((num &gt;&gt; <span class="number">4</span>) &amp; <span class="number">0xF</span>, <span class="number">1</span><span class="number">6</span>);
hexDigits[<span class="number">1</span>] = Character.<span class="ST4">forDigit</span>((num &amp; <span class="number">0xF</span>), <span class="number">1</span><span class="number">6</span>);
<span class="literal">return</span> <span class="literal">new</span> String(hexDigits);
}
<span class="literal">public</span> String <span class="ST3">encodeHexString</span>(<span class="literal">byte</span>[] byteArray) {
StringBuffer hexStringBuffer = <span class="literal">new</span> StringBuffer();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; byteArray.<span class="ST1">length</span>; i++) {
hexStringBuffer.append(byteToHex(byteArray[i]));
}
<span class="literal">return</span> hexStringBuffer.toString();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
<span class="ST4">launch</span>(args);
}
}
</pre></body>
</html>

@ -0,0 +1,152 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>SplitFilesFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST2 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/SplitFilesFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">DataInput</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Alert;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.ProgressBar;
<span class="literal">import</span> javafx.scene.control.TextField;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> SplitFilesFX <span class="literal">extends</span> Application {
<span class="literal">static</span> File <span class="ST1">file</span> = <span class="literal">null</span>;
<span class="literal">final</span> FileChooser <span class="ST2">fileChooser</span> = <span class="literal">new</span> FileChooser();
<span class="literal">static</span> ProgressBar <span class="ST1">pb</span> = <span class="literal">new</span> ProgressBar();
<span class="literal">static</span> Alert <span class="ST1">alert</span> = <span class="literal">new</span> Alert(Alert.<span class="ST3">AlertType</span>.<span class="ST1">INFORMATION</span>);
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">start</span>(<span class="literal">final</span> Stage stage) <span class="literal">throws</span> Exception
{
<span class="ST2">fileChooser</span>.setTitle(<span class="string">&quot;</span><span class="string">Open File to Split...</span><span class="string">&quot;</span>);
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
BorderPane secondaryBoarderPane = <span class="literal">new</span> BorderPane();
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
<span class="ST1">pb</span>.prefWidthProperty().bind(stage.widthProperty());
Label infoLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">If you split a file named tmp into 3 smaller files,</span><span class="literal">\n</span><span class="string"> the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. </span><span class="string">&quot;</span>);
Label chooseFile = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Choose a file to split: </span><span class="string">&quot;</span>);
Button openFilePicker = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
Label splitCountLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Enter the amount of files to split into: </span><span class="string">&quot;</span>);
TextField splitCount = <span class="literal">new</span> TextField();
Button run = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Start</span><span class="string">&quot;</span>);
textFieldGridPane.add(chooseFile, <span class="number">0</span>, <span class="number">0</span>);
textFieldGridPane.add(openFilePicker, <span class="number">1</span>, <span class="number">0</span>);
textFieldGridPane.add(splitCountLabel, <span class="number">0</span>, <span class="number">1</span>);
textFieldGridPane.add(splitCount, <span class="number">1</span>, <span class="number">1</span>);
secondaryBoarderPane.<span class="ST3">setAlignment</span>(run, Pos.<span class="ST1">CENTER</span>);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
secondaryBoarderPane.setBottom(run);
secondaryBoarderPane.setTop(<span class="ST1">p</span><span class="ST1">b</span>);
primaryBorderPane.setBottom(secondaryBoarderPane);
openFilePicker.setOnAction(e -&gt; {
<span class="ST1">file</span> = <span class="ST2">fileChooser</span>.showOpenDialog(openFilePicker.getScene().getWindow());
});
run.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST1">file</span> == <span class="literal">null</span>) {
e.consume();
}
<span class="ST3">splitFile</span>(<span class="ST1">f</span><span class="ST1">ile</span>, Integer.<span class="ST3">parseInt</span>(splitCount.getText()));
});
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">splitFile</span>(File fileToSplit, <span class="literal">int</span> splitCount) {
<span class="literal">int</span> outputFileSize = (<span class="literal">int</span>) fileToSplit.length() / splitCount;
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">output file size will be: </span><span class="string">&quot;</span> + outputFileSize);
<span class="literal">if</span> (outputFileSize &lt; <span class="number">0</span>) {
<span class="ST1">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Output files too large</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.setHeaderText(<span class="string">&quot;</span><span class="string">Output files too large</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">Please increase the amount of files to split!</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.showAndWait();
<span class="literal">return</span>;
}
<span class="literal">double</span> progress = <span class="number">0</span>;
String outputPath = fileToSplit.getParent();
<span class="comment">// Open the original file</span>
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(fileToSplit)) {
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">Opening the source file!</span><span class="string">&quot;</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; splitCount; ++i) {
progress = (i * <span class="number">100</span>) / splitCount;
System.<span class="ST1">out</span>.println(progress);
<span class="ST1">pb</span>.setProgress(progress);
dataIn.mark(outputFileSize * (i + <span class="number">1</span>));
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + fileToSplit.getName() + <span class="string">&quot;</span><span class="string">.</span><span class="string">&quot;</span> + (i + <span class="number">1</span>))) {
dataOut.write(dataIn.readNBytes(outputFileSize), <span class="number">0</span>, outputFileSize);
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">Writing to </span><span class="string">&quot;</span> + outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + fileToSplit.getName() + <span class="string">&quot;</span><span class="string">.</span><span class="string">&quot;</span> + (i + <span class="number">1</span>));
}
}
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST1">out</span>.println(ex);
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args)
{
<span class="ST3">launch</span>();
}
}
<span class="comment">/*</span>
<span class="comment"> */</span>
</pre></body>
</html>

@ -119,7 +119,43 @@ is divided into following sections:
<property name="module.name" value=""/>
</target>
<target depends="-pre-init,-init-private,-init-user,-init-project,-init-macrodef-property,-init-modules-supported" name="-do-init">
<property name="platform.java" value="${java.home}/bin/java"/>
<j2seproject1:property name="platform.home" value="platforms.${platform.active}.home"/>
<j2seproject1:property name="platform.bootcp" value="platforms.${platform.active}.bootclasspath"/>
<j2seproject1:property name="platform.compiler" value="platforms.${platform.active}.compile"/>
<j2seproject1:property name="platform.javac.tmp" value="platforms.${platform.active}.javac"/>
<condition property="platform.javac" value="${platform.home}/bin/javac">
<equals arg1="${platform.javac.tmp}" arg2="$${platforms.${platform.active}.javac}"/>
</condition>
<property name="platform.javac" value="${platform.javac.tmp}"/>
<j2seproject1:property name="platform.java.tmp" value="platforms.${platform.active}.java"/>
<condition property="platform.java" value="${platform.home}/bin/java">
<equals arg1="${platform.java.tmp}" arg2="$${platforms.${platform.active}.java}"/>
</condition>
<property name="platform.java" value="${platform.java.tmp}"/>
<j2seproject1:property name="platform.javadoc.tmp" value="platforms.${platform.active}.javadoc"/>
<condition property="platform.javadoc" value="${platform.home}/bin/javadoc">
<equals arg1="${platform.javadoc.tmp}" arg2="$${platforms.${platform.active}.javadoc}"/>
</condition>
<property name="platform.javadoc" value="${platform.javadoc.tmp}"/>
<condition property="platform.invalid" value="true">
<or>
<contains string="${platform.javac}" substring="$${platforms."/>
<contains string="${platform.java}" substring="$${platforms."/>
<contains string="${platform.javadoc}" substring="$${platforms."/>
</or>
</condition>
<fail unless="platform.home">Must set platform.home</fail>
<fail unless="platform.bootcp">Must set platform.bootcp</fail>
<fail unless="platform.java">Must set platform.java</fail>
<fail unless="platform.javac">Must set platform.javac</fail>
<fail if="platform.invalid">
The J2SE Platform is not correctly set up.
Your active platform is: ${platform.active}, but the corresponding property "platforms.${platform.active}.home" is not found in the project's properties files.
Either open the project in the IDE and setup the Platform with the same name or add it manually.
For example like this:
ant -Duser.properties.file=&lt;path_to_property_file&gt; jar (where you put the property "platforms.${platform.active}.home" in a .properties file)
or ant -Dplatforms.${platform.active}.home=&lt;path_to_JDK_home&gt; jar (where no properties file is used)
</fail>
<available file="${manifest.file}" property="manifest.available"/>
<condition property="splashscreen.available">
<and>
@ -242,20 +278,6 @@ is divided into following sections:
<condition else="" property="javac.profile.cmd.line.arg" value="-profile ${javac.profile}">
<isset property="profile.available"/>
</condition>
<condition else="false" property="jdkBug6558476">
<and>
<matches pattern="1\.[56]" string="${java.specification.version}"/>
<not>
<os family="unix"/>
</not>
</and>
</condition>
<condition else="false" property="javac.fork">
<or>
<istrue value="${jdkBug6558476}"/>
<istrue value="${javac.external.vm}"/>
</or>
</condition>
<property name="jar.index" value="false"/>
<property name="jar.index.metainf" value="${jar.index}"/>
<property name="copylibs.rebase" value="true"/>
@ -343,7 +365,7 @@ is divided into following sections:
</path>
</resourcecount>
</condition>
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" fork="${javac.fork}" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" executable="${platform.javac}" fork="yes" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<src>
<dirset dir="@{gensrcdir}" erroronmissingdir="false">
<include name="*"/>
@ -394,7 +416,7 @@ is divided into following sections:
<property location="${build.dir}/empty" name="empty.dir"/>
<mkdir dir="${empty.dir}"/>
<mkdir dir="@{apgeneratedsrcdir}"/>
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" fork="${javac.fork}" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" executable="${platform.javac}" fork="yes" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<src>
<dirset dir="@{gensrcdir}" erroronmissingdir="false">
<include name="*"/>
@ -436,7 +458,7 @@ is divided into following sections:
<sequential>
<property location="${build.dir}/empty" name="empty.dir"/>
<mkdir dir="${empty.dir}"/>
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" fork="${javac.fork}" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" executable="${platform.javac}" fork="yes" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<src>
<dirset dir="@{gensrcdir}" erroronmissingdir="false">
<include name="*"/>
@ -515,7 +537,7 @@ is divided into following sections:
<element name="customizePrototype" optional="true"/>
<sequential>
<property name="junit.forkmode" value="perTest"/>
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}">
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" jvm="${platform.java}" showoutput="true" tempdir="${build.dir}">
<syspropertyset>
<propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/>
@ -543,7 +565,7 @@ is divided into following sections:
<element name="customizePrototype" optional="true"/>
<sequential>
<property name="junit.forkmode" value="perTest"/>
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}">
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" jvm="${platform.java}" showoutput="true" tempdir="${build.dir}">
<syspropertyset>
<propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/>
@ -619,7 +641,7 @@ is divided into following sections:
</fileset>
</union>
<taskdef classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}" name="testng"/>
<testng classfilesetref="test.set" failureProperty="tests.failed" listeners="org.testng.reporters.VerboseReporter" methods="${testng.methods.arg}" mode="${testng.mode}" outputdir="${build.test.results.dir}" suitename="MP5-Binary-Files_CalebFontenot" testname="TestNG tests" workingDir="${work.dir}">
<testng classfilesetref="test.set" failureProperty="tests.failed" jvm="${platform.java}" listeners="org.testng.reporters.VerboseReporter" methods="${testng.methods.arg}" mode="${testng.mode}" outputdir="${build.test.results.dir}" suitename="MP5-Binary-Files_CalebFontenot" testname="TestNG tests" workingDir="${work.dir}">
<xmlfileset dir="${build.test.classes.dir}" includes="@{testincludes}"/>
<propertyset>
<propertyref prefix="test-sys-prop."/>
@ -850,6 +872,9 @@ is divided into following sections:
<classpath>
<path path="@{classpath}"/>
</classpath>
<bootclasspath>
<path path="${platform.bootcp}"/>
</bootclasspath>
</nbjpdastart>
</sequential>
</macrodef>
@ -899,7 +924,7 @@ is divided into following sections:
<attribute default="jvm" name="jvm"/>
<element name="customize" optional="true"/>
<sequential>
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" module="@{modulename}">
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" jvm="${platform.java}" module="@{modulename}">
<classpath>
<path path="@{classpath}"/>
</classpath>
@ -933,7 +958,7 @@ is divided into following sections:
<attribute default="jvm" name="jvm"/>
<element name="customize" optional="true"/>
<sequential>
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true">
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" jvm="${platform.java}">
<classpath>
<path path="@{classpath}"/>
</classpath>
@ -965,7 +990,7 @@ is divided into following sections:
<attribute default="jvm" name="jvm"/>
<element name="customize" optional="true"/>
<sequential>
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true">
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" jvm="${platform.java}">
<jvmarg line="${endorsed.classpath.cmd.line.arg}"/>
<jvmarg value="-Dfile.encoding=${runtime.encoding}"/>
<redirector errorencoding="${runtime.encoding}" inputencoding="${runtime.encoding}" outputencoding="${runtime.encoding}"/>
@ -1199,7 +1224,7 @@ is divided into following sections:
<j2seproject3:copylibs manifest="${tmp.manifest.file}"/>
<echo level="info">To run this application from the command line without Ant, try:</echo>
<property location="${dist.jar}" name="dist.jar.resolved"/>
<echo level="info">java -jar "${dist.jar.resolved}"</echo>
<echo level="info">${platform.java} -jar "${dist.jar.resolved}"</echo>
</target>
<target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen,-check-do-mkdist" if="do.archive" name="-do-jar-jar" unless="do.mkdist">
<j2seproject1:jar manifest="${tmp.manifest.file}"/>
@ -1301,8 +1326,8 @@ is divided into following sections:
<isset property="main.class.available"/>
</and>
</condition>
<property name="platform.jlink" value="${jdk.home}/bin/jlink"/>
<property name="jlink.systemmodules.internal" value="${jdk.home}/jmods"/>
<property name="platform.jlink" value="${platform.home}/bin/jlink"/>
<property name="jlink.systemmodules.internal" value="${platform.home}/jmods"/>
<exec executable="${platform.jlink}">
<arg value="--module-path"/>
<arg path="${jlink.systemmodules.internal}:${run.modulepath}:${dist.jar}"/>
@ -1495,16 +1520,19 @@ is divided into following sections:
</not>
</and>
</condition>
<exec executable="${platform.java}" failonerror="false" outputproperty="platform.version.output">
<arg value="-version"/>
</exec>
<condition else="" property="bug5101868workaround" value="*.java">
<matches pattern="1\.[56](\..*)?" string="${java.version}"/>
<matches multiline="true" pattern="1\.[56](\..*)?" string="${platform.version.output}"/>
</condition>
<condition else="" property="javadoc.html5.cmd.line.arg" value="-html5">
<and>
<isset property="javadoc.html5"/>
<available file="${jdk.home}${file.separator}lib${file.separator}jrt-fs.jar"/>
<available file="${platform.home}${file.separator}lib${file.separator}jrt-fs.jar"/>
</and>
</condition>
<javadoc additionalparam="-J-Dfile.encoding=${file.encoding} ${javadoc.additionalparam}" author="${javadoc.author}" charset="UTF-8" destdir="${dist.javadoc.dir}" docencoding="UTF-8" encoding="${javadoc.encoding.used}" failonerror="true" noindex="${javadoc.noindex}" nonavbar="${javadoc.nonavbar}" notree="${javadoc.notree}" private="${javadoc.private}" source="${javac.source}" splitindex="${javadoc.splitindex}" use="${javadoc.use}" useexternalfile="true" version="${javadoc.version}" windowtitle="${javadoc.windowtitle}">
<javadoc additionalparam="-J-Dfile.encoding=${file.encoding} ${javadoc.additionalparam}" author="${javadoc.author}" charset="UTF-8" destdir="${dist.javadoc.dir}" docencoding="UTF-8" encoding="${javadoc.encoding.used}" executable="${platform.javadoc}" failonerror="true" noindex="${javadoc.noindex}" nonavbar="${javadoc.nonavbar}" notree="${javadoc.notree}" private="${javadoc.private}" source="${javac.source}" splitindex="${javadoc.splitindex}" use="${javadoc.use}" useexternalfile="true" version="${javadoc.version}" windowtitle="${javadoc.windowtitle}">
<classpath>
<path path="${javac.classpath}"/>
</classpath>

@ -1,8 +1,8 @@
build.xml.data.CRC32=a0844a83
build.xml.data.CRC32=5b3e797d
build.xml.script.CRC32=dee8d968
build.xml.stylesheet.CRC32=f85dc8f2@1.108.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=a0844a83
nbproject/build-impl.xml.script.CRC32=025ed557
nbproject/build-impl.xml.data.CRC32=5b3e797d
nbproject/build-impl.xml.script.CRC32=7cb0beee
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.108.0.48

@ -49,7 +49,7 @@ javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=\
${libs.JavaFX20.classpath}
${libs.JavaFX_20.classpath}
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
@ -101,7 +101,7 @@ manifest.custom.permissions=
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
platform.active=Graal_JDK_20
run.classpath=\
${javac.classpath}:\
${build.classes.dir}:\
@ -109,7 +109,7 @@ run.classpath=\
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=-XX:+UnlockExperimentalVMOptions -XX:+EagerJVMCI -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+UseJVMCINativeLibrary
run.jvmargs=--module-path=/usr/lib --add-modules javafx.controls -XX:+UnlockExperimentalVMOptions -XX:+EagerJVMCI -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+UseJVMCINativeLibrary
run.modulepath=\
${javac.modulepath}
run.test.classpath=\

@ -4,6 +4,7 @@
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>MP5-Binary-Files_CalebFontenot</name>
<explicit-platform explicit-source-supported="true"/>
<source-roots>
<root id="src.dir"/>
</source-roots>

@ -10,10 +10,16 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
@ -28,12 +34,11 @@ import javafx.stage.Stage;
*/
public class CombineFilesFX extends Application {
static List<File> files = null;
static ArrayList<File> files = new ArrayList<File>();
final FileChooser fileChooser = new FileChooser();
@Override
public void start(final Stage stage) throws Exception
{
public void start(final Stage stage) throws Exception {
fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane();
@ -51,7 +56,30 @@ public class CombineFilesFX extends Application {
primaryBorderPane.setBottom(run);
openFilePicker.setOnAction(e -> {
files = fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow());
// When we initially get the files from the file chooser, it returns an unsortable list. Because of this, we need to create a new List with its data.
files.addAll(fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow()));
// The file picker appears to return files in a random order, so we need to sort them by file name.
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
return extractInt(o1.getName()) - extractInt(o2.getName());
}
String findMatch(String s) {
Pattern findNum = Pattern.compile("\\d+$");
Matcher match = findNum.matcher(s);
while (match.find()) {
return match.group();
}
return "";
}
int extractInt(String s) {
String num = findMatch(s);
System.out.println(num);
// return 0 if no digits found
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});
});
run.setOnAction(e -> {
@ -66,33 +94,37 @@ public class CombineFilesFX extends Application {
stage.show();
}
public static void combineFiles(List<File> filesToCombine)
{
public static void combineFiles(List<File> filesToCombine) {
String outputPath = filesToCombine.get(0).getParent();
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, (filesToCombine.get(0).getName().length() - 2)))) {
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, ( filesToCombine.get(0).getName().length() - 2)));
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)))) {
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)));
for (File file : filesToCombine) {
try (FileInputStream dataIn = new FileInputStream(file)) {
System.out.println("Opening the source file!");
byte[] buffer = new byte[4096]; // You can adjust the buffer size as needed
System.out.println("Opening the source file " + file.getName() + "!");
byte[] buffer = new byte[4096];
int bytesRead = -1;
int bytesRead;
while ((bytesRead = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, bytesRead);
while ((bytesRead = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, bytesRead);
}
}
}
catch (IOException ex) {
System.out.println(ex);
}
}
} catch (IOException ex) {
System.out.println(ex);
}
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Success!");
alert.setContentText("Successfully combined the files!");
alert.showAndWait();
}
} catch (IOException ex) {
System.out.println(ex);
}
}
public static void main(String[] args)
{
public static void main(String[] args) {
launch();
}
}
/*
*/

@ -0,0 +1,147 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import static com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.alert;
import static com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class HexEditorFX extends Application {
final FileChooser fileChooser = new FileChooser();
static File file = null;
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("Open a file...");
Button openFileButton = new Button("Choose...");
TextArea editorWindow = new TextArea();
Button saveFileButton = new Button("Save");
editorWindow.setWrapText(true);
BorderPane bp = new BorderPane();
GridPane openFileGridPane = new GridPane();
openFileGridPane.add(label, 0, 0);
openFileGridPane.add(openFileButton, 1, 0);
bp.setTop(openFileGridPane);
bp.setCenter(editorWindow);
bp.setBottom(saveFileButton);
bp.setAlignment(saveFileButton, Pos.CENTER);
openFileButton.setOnAction(e -> {
file = fileChooser.showOpenDialog(openFileButton.getScene().getWindow());
byte[] data = null;
System.out.println("Getting data from fileand encoding it as hex...");
try (FileInputStream dataIn = new FileInputStream(file)) {
data = dataIn.readAllBytes();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
String dataString = encodeHexString(data);
editorWindow.setText(dataString);
});
saveFileButton.setOnAction(e -> {
if (file == null) {
alert.setTitle("No file selected!");
alert.setHeaderText("No file selected!");
alert.setContentText("You have to open a file first, silly!");
alert.showAndWait();
}
System.out.println("Re-encoding hex back into a byte array...");
try (FileOutputStream dataOut = new FileOutputStream(file)) {
byte[] bytesToSave = decodeHexString(editorWindow.getText());
dataOut.write(bytesToSave);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
alert.setTitle("Invalid Hex!");
alert.setContentText("Invalid hex entered into the text box, unable to save.");
alert.showAndWait();
}
alert.setTitle("Save successful!");
alert.setContentText("File saved successfully.");
alert.showAndWait();
});
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
private int toDigit(char hexChar) {
int digit = Character.digit(hexChar, 16);
if (digit == -1) {
throw new IllegalArgumentException(
"Invalid Hexadecimal Character: " + hexChar);
}
return digit;
}
public byte hexToByte(String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}
public byte[] decodeHexString(String hexString) {
if (hexString.length() % 2 == 1) {
throw new IllegalArgumentException(
"Invalid hexadecimal String supplied.");
}
byte[] bytes = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
}
return bytes;
}
public String byteToHex(byte num) {
char[] hexDigits = new char[2];
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
hexDigits[1] = Character.forDigit((num & 0xF), 16);
return new String(hexDigits);
}
public String encodeHexString(byte[] byteArray) {
StringBuffer hexStringBuffer = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
hexStringBuffer.append(byteToHex(byteArray[i]));
}
return hexStringBuffer.toString();
}
public static void main(String[] args) {
launch(args);
}
}

@ -12,8 +12,10 @@ import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
@ -27,13 +29,18 @@ import javafx.stage.Stage;
public class SplitFilesFX extends Application {
static File file = null;
final FileChooser fileChooser = new FileChooser();
static ProgressBar pb = new ProgressBar();
static Alert alert = new Alert(Alert.AlertType.INFORMATION);
@Override
public void start(final Stage stage) throws Exception
{
fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane();
BorderPane secondaryBoarderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane();
pb.prefWidthProperty().bind(stage.widthProperty());
Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. ");
Label chooseFile = new Label("Choose a file to split: ");
Button openFilePicker = new Button("Choose...");
@ -44,11 +51,15 @@ public class SplitFilesFX extends Application {
textFieldGridPane.add(openFilePicker, 1, 0);
textFieldGridPane.add(splitCountLabel, 0, 1);
textFieldGridPane.add(splitCount, 1, 1);
primaryBorderPane.setAlignment(run, Pos.CENTER);
secondaryBoarderPane.setAlignment(run, Pos.CENTER);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
primaryBorderPane.setBottom(run);
secondaryBoarderPane.setBottom(run);
secondaryBoarderPane.setTop(pb);
primaryBorderPane.setBottom(secondaryBoarderPane);
openFilePicker.setOnAction(e -> {
file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow());
@ -68,12 +79,26 @@ public class SplitFilesFX extends Application {
public static void splitFile(File fileToSplit, int splitCount) {
int outputFileSize = (int) fileToSplit.length() / splitCount;
System.out.println("output file size will be: " + outputFileSize);
if (outputFileSize < 0) {
alert.setTitle("Output files too large");
alert.setHeaderText("Output files too large");
alert.setContentText("Please increase the amount of files to split!");
alert.showAndWait();
return;
}
double progress = 0;
String outputPath = fileToSplit.getParent();
// Open the original file
try (FileInputStream dataIn = new FileInputStream(fileToSplit)) {
System.out.println("Opening the source file!");
for (int i = 0; i < splitCount; ++i) {
//dataIn.mark(outputFileSize * (i + 1));
progress = (i * 100) / splitCount;
System.out.println(progress);
pb.setProgress(progress);
dataIn.mark(outputFileSize * (i + 1));
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + fileToSplit.getName() + "." + (i + 1))) {
dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize);
System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1));

@ -18,14 +18,12 @@ import java.util.Queue;
import java.util.function.Consumer;
public class PriorityQueueASDV<E extends Comparable<E>>
implements Queue<E>, Cloneable
{
implements Queue<E>, Cloneable {
private Node<E> head;//head
private Node<E> tail;//tail
class Node<E>
{
class Node<E> {
E e;
Node<E> l;
@ -33,78 +31,56 @@ public class PriorityQueueASDV<E extends Comparable<E>>
}
/**
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions, returning true upon
* success and throwing an IllegalStateException if no space is currently
* available.
* Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
*
* Specified by: add in interface Collection<E>
* Parameters: e - the element to add Returns: true (as specified by
* Collection.add(E)) Throws: IllegalStateException - if the element cannot
* be added at this time due to capacity restrictions ClassCastException -
* if the class of the specified element prevents it from being added to
* this queue NullPointerException - if the specified element is null and
* this queue does not permit null elements IllegalArgumentException - if
* some property of this element prevents it from being added to this queue
* Parameters: e - the element to add Returns: true (as specified by Collection.add(E)) Throws: IllegalStateException - if the element cannot be added at this time due to capacity restrictions ClassCastException - if the class of the specified element prevents it from being added to this queue NullPointerException - if the specified element is null and this queue does not permit null elements IllegalArgumentException - if some property of this element prevents it from being added to this queue
*
* @param e - the element to add
* @return true if this collection changed as a result of the call
* @throws IllegalStateException - if the element cannot be added at this
* time due to capacity restrictions
* @throws IllegalStateException - if the element cannot be added at this time due to capacity restrictions
* @throws ClassCastException - if the class of the specified element
* @throws NullPointerException - if the specified element is null and this
* queue does not permit null elements
* @throws IllegalArgumentException - if some property of this element
* prevents it from being added to this queue
* @throws NullPointerException - if the specified element is null and this queue does not permit null elements
* @throws IllegalArgumentException - if some property of this element prevents it from being added to this queue
*/
@Override
public boolean add(E e)
{
if (e == null)
{
public boolean add(E e) {
if (e == null) {
throw new NullPointerException("NULL elements not allowed!");
}
}
Node<E> newNode = new Node<E>();
newNode.e = e;
//1. empty queue
if (this.head == null && this.tail == null)
{
if (this.head == null && this.tail == null) {
this.head = this.tail = newNode;
return true;
}
}
int index = findCorrectPositionToInsertElement(e);
//int index = findCorrectPositionToInsertElementHashCode(e);
//2. we add at size ( last node)
if (index == size())
{
if (index == size()) {
tail.r = newNode;//1
newNode.l = tail;//2
tail = newNode;//3
}
//3. we add at 0 in the front
else if (index == 0)
{
} //3. we add at 0 in the front
else if (index == 0) {
newNode.r = head;
this.head.l = newNode;
this.head = newNode;
if (size() == 1)
{
if (size() == 1) {
tail = head;
}
}
//4. we add in the middle
else
{
}
} //4. we add in the middle
else {
Node<E> p = head;
for (int i = 0; i < index - 1; ++i)
{
for (int i = 0; i < index - 1; ++i) {
p = p.r;
}
}
//after for loop p point one position before insertion
newNode.l = p;//we connect the left of the new node
@ -120,181 +96,198 @@ public class PriorityQueueASDV<E extends Comparable<E>>
p.r.r.l = newNode;//we connect the left of the node AFTER the node
//to be iserted to the new node
}
}
return true;
}
@Override
public int size()
{
public int size() {
Node<E> p = this.head;
int count = 0;
while (p != null)
{
while (p != null) {
p = p.r;
count++;
}
}
return count;
}
private int findCorrectPositionToInsertElement(E e)
{
private int findCorrectPositionToInsertElement(E e) {
Node<E> p = this.head;
int pos = 0;
while (p != null)
{
if (e.compareTo(p.e) > 0)
{
while (p != null) {
if (e.compareTo(p.e) > 0) {
p = p.r;
++pos;
}
else
{
} else {
break;
}
}
}
}
return pos;
}
private int findCorrectPositionToInsertElementHashCode(E e)
{
private int findCorrectPositionToInsertElementHashCode(E e) {
Node<E> p = this.head;
int pos = 0;
while (p != null)
{
if (e.hashCode() > p.e.hashCode())
{
while (p != null) {
if (e.hashCode() > p.e.hashCode()) {
p = p.r;
++pos;
}
else
{
} else {
break;
}
}
}
}
return pos;
}
/**
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions. When using a
* capacity-restricted queue, this method is generally preferable to add(E),
* which can fail to insert an element only by throwing an exception.
* Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception.
*
* @param e - the element to add
* @throws IllegalStateException - if the element cannot be added at this
* time due to capacity restrictions
* @throws IllegalStateException - if the element cannot be added at this time due to capacity restrictions
* @throws ClassCastException - if the class of the specified element
* @throws NullPointerException - if the specified element is null and this
* queue does not permit null elements
* @throws IllegalArgumentException - if some property of this element
* prevents it from being added to this queue
* @throws NullPointerException - if the specified element is null and this queue does not permit null elements
* @throws IllegalArgumentException - if some property of this element prevents it from being added to this queue
* @return true if the element was added
*/
@Override
public boolean offer(E e)
{
public boolean offer(E e) {
return add(e);
}
/**
* Retrieves and removes the head of this queue. This method differs from {@link #poll poll} only in that it throws an exception if this queue is empty.
*
* <p>
* This implementation returns the result of {@code poll} unless the queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
@Override
public E remove()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public E remove() {
Node<E> pointer = head.r;
E removedElement = head.e;
head = head.r;
head.l = null;
return removedElement;
}
/**
* Retrieves and removes the head of this queue, or returns null if this
* queue is empty.
* Retrieves and removes the head of this queue, or returns null if this queue is empty.
*
* Returns: the head of this queue, or null if this queue is empty
*
* @return
*/
@Override
public E poll()
{
if (size() == 0)
{
public E poll() {
if (size() == 0) {
return null;
}
if (size() > 1)
{
}
if (size() > 1) {
head = head.r;
E e = head.l.e;
head.l = null;
return e;
}
else //size 1
{
} else //size 1
{
E e = head.e;
head = tail = null;
return e;
}
}
}
/**
* Retrieves, but does not remove, the head of this queue. This method
* differs from {@link #peek peek} only in that it throws an exception
* if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
@Override
public E element() {
if (head != null) {
return (E) head;
} else {
throw new NoSuchElementException("Element does not exist.");
}
}
/**
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
@Override
public E peek() {
return (E) head;
}
@Override
public E element()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public E peek()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isEmpty()
{
public boolean isEmpty() {
return head == null && tail == null ? true : false;
}
/**
* Returns {@code true} if this collection contains the specified element.
* More formally, returns {@code true} if and only if this collection
* contains at least one element {@code e} such that
* {@code Objects.equals(o, e)}.
*
* @param o element whose presence in this collection is to be tested
* @return {@code true} if this collection contains the specified
* element
* @throws ClassCastException if the type of the specified element
* is incompatible with this collection
* ({@linkplain Collection##optional-restrictions optional})
* @throws NullPointerException if the specified element is null and this
* collection does not permit null elements
* ({@linkplain Collection##optional-restrictions optional})
*/
@Override
public boolean contains(Object o)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public boolean contains(Object o) {
Node<E> pointer = head;
do {
if (pointer.equals(o)) {
return true;
} else {
pointer =pointer.r;
}
} while (pointer != null);
return false;
}
@Override
public Iterator<E> iterator()
{
Iterator<E> it = new Iterator<E>()
{
public Iterator<E> iterator() {
Iterator<E> it = new Iterator<E>() {
Node<E> p = head;
@Override
public boolean hasNext()
{
public boolean hasNext() {
return p == null ? false : true;
}
@Override
public E next()
{
if (hasNext() == false)
{
public E next() {
if (hasNext() == false) {
throw new NoSuchElementException("the is no next element");
}
}
E e = p.e;
p = p.r;
return e;
}
@Override
public void forEachRemaining(Consumer<? super E> action)
{
while (hasNext())
{
public void forEachRemaining(Consumer<? super E> action) {
while (hasNext()) {
action.accept(p.e);
p = p.r;
}
}
}
};
@ -303,65 +296,111 @@ public class PriorityQueueASDV<E extends Comparable<E>>
}
@Override
public Object[] toArray()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public Object[] toArray() {
Node<E> pointer = head;
Object[] returnArray = new Object[this.size()];
int i = 0;
while (pointer.r != null) {
returnArray[i++] = pointer.e;
pointer = pointer.r;
}
returnArray[i++] = pointer.e;
return returnArray;
}
/**
* Returns an array containing all of the elements in this collection;
* the runtime type of the returned array is that of the specified array.
* If the collection fits in the specified array, it is returned therein.
* Otherwise, a new array is allocated with the runtime type of the
* specified array and the size of this collection.
*
* <p>If this collection fits in the specified array with room to spare
* (i.e., the array has more elements than this collection), the element
* in the array immediately following the end of the collection is set to
* {@code null}. (This is useful in determining the length of this
* collection <i>only</i> if the caller knows that this collection does
* not contain any {@code null} elements.)
*
* <p>If this collection makes any guarantees as to what order its elements
* are returned by its iterator, this method must return the elements in
* the same order.
*
* @apiNote
* This method acts as a bridge between array-based and collection-based APIs.
* It allows an existing array to be reused under certain circumstances.
* Use {@link #toArray()} to create an array whose runtime type is {@code Object[]},
* or use {@link #toArray(IntFunction)} to control the runtime type of
* the array.
*
* <p>Suppose {@code x} is a collection known to contain only strings.
* The following code can be used to dump the collection into a previously
* allocated {@code String} array:
*
* <pre>
* String[] y = new String[SIZE];
* ...
* y = x.toArray(y);</pre>
*
* <p>The return value is reassigned to the variable {@code y}, because a
* new array will be allocated and returned if the collection {@code x} has
* too many elements to fit into the existing array {@code y}.
*
* <p>Note that {@code toArray(new Object[0])} is identical in function to
* {@code toArray()}.
*
* @param <T> the component type of the array to contain the collection
* @param a the array into which the elements of this collection are to be
* stored, if it is big enough; otherwise, a new array of the same
* runtime type is allocated for this purpose.
* @return an array containing all of the elements in this collection
* @throws ArrayStoreException if the runtime type of any element in this
* collection is not assignable to the {@linkplain Class#getComponentType
* runtime component type} of the specified array
* @throws NullPointerException if the specified array is null
*/
@Override
public <T> T[] toArray(T[] a)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public <T> T[] toArray(T[] a) {
T[] genericArray = new T[this.size()];
Node<E> pointer = head;
for (int i = 0; i < this.size(); ++i) {
genericArray[i] = pointer;
}
}
/**
* Removes a single instance of the specified element from this collection,
* if it is present (optional operation). More formally, removes an element
* e such that (o==null ? e==null : o.equals(e)), if this collection
* contains one or more such elements. Returns true if this collection
* contained the specified element (or equivalently, if this collection
* changed as a result of the call).
* Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).
*
* @param o - element to be removed from this collection, if present
* @throws ClassCastException - if the type of the specified element is
* incompatible with this collection
* @throws NullPointerException - if the specified element is null and this
* collection does not permit null elements
* @throws ClassCastException - if the type of the specified element is incompatible with this collection
* @throws NullPointerException - if the specified element is null and this collection does not permit null elements
* @return true if an element was removed as a result of this call
*/
@Override
public boolean remove(Object o)
{
if (o == null)
{
public boolean remove(Object o) {
if (o == null) {
throw new NullPointerException("null vales not allowed");
}
if (size() == 0)
{
}
if (size() == 0) {
return false;
}
}
Node<E> p = this.head;
int pos = 0;
while (p != this.tail)
{
if (p.e.equals(o))
{
if (size() == 1)
{
while (p != this.tail) {
if (p.e.equals(o)) {
if (size() == 1) {
this.head = this.tail = null;
return true;
}
}
this.removeAt(pos, (E) o);
break;
}
}
++pos;
p = p.r;
}
if (p == tail && p.e.equals(o))
{
}
if (p == tail && p.e.equals(o)) {
this.removeAt(size() - 1, (E) o);
}
}
return true;
}
@ -369,147 +408,128 @@ public class PriorityQueueASDV<E extends Comparable<E>>
*
* @param pos
* @param e
* @throws IndexOutOfBoundsException - if pos less 0 OR pos greater-equal
* size()
* @throws IndexOutOfBoundsException - if pos less 0 OR pos greater-equal size()
* @return
*/
private boolean removeAt(int pos, E e)
{
if (pos < 0 || pos >= size())
{
private boolean removeAt(int pos, E e) {
if (pos < 0 || pos >= size()) {
throw new IndexOutOfBoundsException(pos + " is out of bounds");
}
}
//1.list is empty
if (isEmpty())
{
if (isEmpty()) {
return false;
}
//2. one node exists
else if (size() == 1)
{
} //2. one node exists
else if (size() == 1) {
this.head = this.tail = null;
}
//3. remove in the front( head)
else if (pos == 0)
{
} //3. remove in the front( head)
else if (pos == 0) {
this.head = this.head.r;
head.l = null;
}
//4. remove in the end ( tail)
else if (pos == size() - 1)
{
} //4. remove in the end ( tail)
else if (pos == size() - 1) {
this.tail = this.tail.l;
this.tail.r = null;
}
//5. remove in the middle ( at least 3 nodes are in the queue)
else
{
} //5. remove in the middle ( at least 3 nodes are in the queue)
else {
Node<E> p = head;
for (int i = 0; i < pos - 1; ++i)
{
for (int i = 0; i < pos - 1; ++i) {
p = p.r;
}
}
p.r = p.r.r;
p.r.l = p;
}
}
return true;
}
/**
* Returns {@code true} if this collection contains all of the elements
* in the specified collection.
*
* @param c collection to be checked for containment in this collection
* @return {@code true} if this collection contains all of the elements
* in the specified collection
* @throws ClassCastException if the types of one or more elements
* in the specified collection are incompatible with this
* collection
* ({@linkplain Collection##optional-restrictions optional})
* @throws NullPointerException if the specified collection contains one
* or more null elements and this collection does not permit null
* elements
* ({@linkplain Collection##optional-restrictions optional})
* or if the specified collection is null.
* @see #contains(Object)
*/
@Override
public boolean containsAll(Collection<?> c)
{
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/**
* Adds all of the elements in the specified collection to this collection
* (optional operation). The behavior of this operation is undefined if the
* specified collection is modified while the operation is in progress.
* (This implies that the behavior of this call is undefined if the
* specified collection is this collection, and this collection is
* nonempty.)
* Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
*
* @param c - collection containing elements to be added to this collection
* @throws ClassCastException - if the class of an element of the specified
* collection prevents it from being added to this collection.
* @throws NullPointerException - if the specified collection contains a
* null element and this collection does not permit null elements, or if the
* specified collection is null
* @throws IllegalArgumentException - if some property of an element of the
* specified collection prevents it from being added to this collection
* @throws IllegalArgumentException - if some property of an element of the
* specified collection prevents it from being added to this collection
* @throws ClassCastException - if the class of an element of the specified collection prevents it from being added to this collection.
* @throws NullPointerException - if the specified collection contains a null element and this collection does not permit null elements, or if the specified collection is null
* @throws IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this collection
* @throws IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this collection
* @return true if this collection changed as a result of the call
*/
@Override
public boolean addAll(Collection<? extends E> c)
{
public boolean addAll(Collection<? extends E> c) {
int sizeBefore = size();
for (E e : c)
{
for (E e : c) {
add(e);
}
}
int sizeAfter = size();
return sizeAfter > sizeBefore;
}
@Override
public boolean removeAll(Collection<?> c)
{
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean retainAll(Collection<?> c)
{
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void clear()
{
// head = tail = null;
public void clear() {
// head = tail = null;
//extra, no necessary to set the link of every node
Node<E> p = head ;
while ( p != tail )
{
p = p.r;
p.l = null;
}
Node<E> p = head;
while (p != tail) {
p = p.r;
p.l = null;
}
head = tail = null;
}
@Override
public void forEach(Consumer<? super E> action)
{
public void forEach(Consumer<? super E> action) {
//1. use a pointer that points to the head
//2. while the pointer has not reached the end of the queue
//consume it
Node<E> p = head;
while ( p != null)
{
while (p != null) {
action.accept(p.e);
p = p.r;
}
}
}
@Override
public String toString()
{
public String toString() {
String s = "PriorityQueueASDV {";
Node<E> p = head;
while (p != null)
{
while (p != null) {
s += p.e.toString();
if (p != tail)
{
if (p != tail) {
s += ", ";
}
}
p = p.r;
}
}
s += "}";
return s;
@ -517,14 +537,12 @@ public class PriorityQueueASDV<E extends Comparable<E>>
@Override
protected Object clone()
throws CloneNotSupportedException
{
PriorityQueueASDV<E> c = ( PriorityQueueASDV<E> ) super.clone();
throws CloneNotSupportedException {
PriorityQueueASDV<E> c = (PriorityQueueASDV<E>) super.clone();
return c;
}
public static void main(String[] args)
{
public static void main(String[] args) {
System.out.println("\n--> PriorityQueueASDV testing add");
PriorityQueueASDV<String> pq1 = new PriorityQueueASDV();
pq1.add("Paris");
@ -629,44 +647,32 @@ public class PriorityQueueASDV<E extends Comparable<E>>
System.out.println(pq2);
System.out.println("\n\t offer null");
try
{
try {
pq1.offer(null);
}
catch (Exception e)
{
} catch (Exception e) {
System.err.println(e);
}
}
try
{
try {
pq2.offer(null);
}
catch (Exception e)
{
} catch (Exception e) {
System.err.println(e);
}
}
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t offer ClassCastException with Object");
try
{
try {
pq1.offer((String) new Object());
}
catch (Exception e)
{
} catch (Exception e) {
System.err.println(e);
}
}
try
{
try {
pq2.offer((String) new Object());
}
catch (Exception e)
{
} catch (Exception e) {
System.err.println(e);
}
}
System.out.println(pq1);
System.out.println(pq2);
@ -680,55 +686,43 @@ public class PriorityQueueASDV<E extends Comparable<E>>
Iterator<String> it1 = pq1.iterator();
Iterator<String> it2 = pq2.iterator();
while (it1.hasNext())
{
while (it1.hasNext()) {
System.out.print(it1.next() + " ");
}
}
System.out.println("");
while (it2.hasNext())
{
while (it2.hasNext()) {
System.out.print(it2.next() + " ");
}
}
System.out.println("");
System.out.println("\n\t Iterator NoSuchElementException ");
try
{
try {
System.out.println(it1.next());
}
catch (NoSuchElementException e)
{
} catch (NoSuchElementException e) {
System.err.println(e);
}
try
{
}
try {
System.out.println(it2.next());
}
catch (NoSuchElementException e)
{
} catch (NoSuchElementException e) {
System.err.println(e);
}
}
System.out.println("\n\t Iterator foreach ");
it1 = pq1.iterator();
it2 = pq2.iterator();
it1.forEachRemaining(new Consumer()
{
it1.forEachRemaining(new Consumer() {
@Override
public void accept(Object t)
{
public void accept(Object t) {
System.out.print(t + "*** ");
}
});
System.out.println("");
it2.forEachRemaining(new Consumer()
{
it2.forEachRemaining(new Consumer() {
@Override
public void accept(Object t)
{
public void accept(Object t) {
System.out.print(t + "+++ ");
}
});
@ -741,7 +735,7 @@ public class PriorityQueueASDV<E extends Comparable<E>>
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t clear" );
System.out.println("\n\t clear");
pq1.clear();
pq2.clear();
System.out.println(pq1);
@ -766,34 +760,69 @@ public class PriorityQueueASDV<E extends Comparable<E>>
System.out.println(pq2);
System.out.println("\n\t forEach");
pq1.forEach(new Consumer(){
pq1.forEach(new Consumer() {
@Override
public void accept(Object t)
{
System.out.print( t + "*** ");
public void accept(Object t) {
System.out.print(t + "*** ");
}
});
System.out.println("");
pq2.forEach(new Consumer(){
pq2.forEach(new Consumer() {
@Override
public void accept(Object t)
{
System.out.print( t + "+++ ");
public void accept(Object t) {
System.out.print(t + "+++ ");
}
});
System.out.println("");
System.out.println("\n\t clone");
try{PriorityQueueASDV<String> pq1Cloned =
( PriorityQueueASDV<String>) pq1.clone();
System.out.println(pq1Cloned);
pq1Cloned.add("Las Vegas");
System.out.println(pq1Cloned);
System.out.println(pq1);
try {
PriorityQueueASDV<String> pq1Cloned
= (PriorityQueueASDV<String>) pq1.clone();
System.out.println(pq1Cloned);
pq1Cloned.add("Las Vegas");
System.out.println(pq1Cloned);
System.out.println(pq1);
}
catch(CloneNotSupportedException e ){System.err.println(e);}
} catch (CloneNotSupportedException e) {
System.err.println(e);
}
pq1.clear();
pq2.clear();
pq1.add("Paris");
pq1.add("Athens");
pq1.add("London");
pq1.add("Lafayette");
pq1.add("Berlin");
pq1.add("Zurich");
pq2.add("Paris");
pq2.add("Athens");
pq2.add("London");
pq2.add("Lafayette");
pq2.add("Berlin");
pq2.add("Zurich");
System.out.println("----------------");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("Attempt to remove an element.");
pq1.remove();
pq2.remove();
System.out.println(pq1);
System.out.println(pq2);
System.out.println("Get array of the priority queues.");
Object pqArray1[] = pq1.toArray();
Object pqArray2[] = pq2.toArray();
printArrays(pqArray1);
printArrays(pqArray2);
}
static void printArrays(Object[] arr) {
for (Object element : arr) {
System.out.print(element + ", ");
}
System.out.println();
}
}