BigIntegerFib lol
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>BinaryToDecimal.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}
|
||||
.literal {color: #cc7832}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.whitespace {color: #505050}
|
||||
.comment {color: #808080}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP3_Recursion_CalebFontenot/src/mp3_recursion_calebfontenot/BinaryToDecimal.java</td></tr></table>
|
||||
<pre>
|
||||
<span class="literal">package</span> mp3_recursion_calebfontenot;
|
||||
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<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="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> BinaryToDecimal {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.in);
|
||||
<span class="literal">while</span> (<span class="literal">true</span>) {
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a binary number: </span><span class="string">"</span>);
|
||||
String binary = input.nextLine();
|
||||
System.out.println(binary + <span class="string">"</span><span class="string"> in decimal is </span><span class="string">"</span> + binaryToDecimal(binary));
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> binaryToDecimal(String binaryString) {
|
||||
<span class="literal">return</span> binaryToDecimal(binaryString, <span class="number">0</span>, binaryString.length(), <span class="number">0</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> binaryToDecimal(String binaryString, <span class="literal">int</span> low, <span class="literal">int</span> high, <span class="literal">int</span> value) {
|
||||
<span class="literal">if</span> (low == binaryString.length() - <span class="number">1</span>) {
|
||||
<span class="literal">return</span> value;
|
||||
}
|
||||
<span class="literal">if</span> (binaryString.charAt(low) == <span class="string">'</span><span class="string">1</span><span class="string">'</span>) {
|
||||
<span class="literal">return</span> binaryToDecimal(binaryString, ++low, --high, value += Math.pow(<span class="number">2</span>, high));
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> binaryToDecimal(binaryString, ++low, --high, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,86 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>DecimalToBinary.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}
|
||||
.literal {color: #cc7832}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP3_Recursion_CalebFontenot/src/mp3_recursion_calebfontenot/DecimalToBinary.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> mp3_recursion_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<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> DecimalToBinary {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.in);
|
||||
<span class="literal">while</span> (<span class="literal">true</span>) {
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a decimal integer: </span><span class="string">"</span>);
|
||||
<span class="literal">int</span> decimal = input.nextInt();
|
||||
System.out.println(decimal + <span class="string">"</span><span class="string"> is </span><span class="string">"</span> + decimalToBinary(decimal) + <span class="string">"</span><span class="string"> in binary.</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> String decimalToBinary(<span class="literal">int</span> value) {
|
||||
<span class="literal">return</span> reverse(decimalToBinary(value, <span class="string">""</span>));
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> String decimalToBinary(<span class="literal">int</span> value, String bin) {
|
||||
<span class="literal">if</span> (value == <span class="number">0</span>) {
|
||||
<span class="literal">return</span> bin += <span class="string">""</span>;
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (value > <span class="number">0</span>) {
|
||||
<span class="literal">if</span> (value % <span class="number">2</span> == <span class="number">1</span>) {
|
||||
<span class="literal">return</span> decimalToBinary((<span class="literal">int</span>) Math.floor(value / <span class="number">2</span>), bin + <span class="string">"</span><span class="string">1</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> decimalToBinary((<span class="literal">int</span>) Math.floor(value / <span class="number">2</span>), bin + <span class="string">"</span><span class="string">0</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="string">"</span><span class="string">0 naht ssel si eulav</span><span class="string">"</span>;
|
||||
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> String reverse(String input) {
|
||||
String returnString = <span class="string">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = input.length() - <span class="number">1</span>; <span class="number">0</span> <= i; --i) {
|
||||
returnString += input.charAt(i);
|
||||
}
|
||||
<span class="literal">return</span> returnString;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> String decimalToBinaryIterative(<span class="literal">int</span> value) {
|
||||
String bin = <span class="string">""</span>;
|
||||
<span class="literal">while</span> (value > <span class="number">0</span>) {
|
||||
<span class="literal">if</span> (value % <span class="number">2</span> == <span class="number">1</span>)
|
||||
bin += <span class="string">"</span><span class="string">1</span><span class="string">"</span>;
|
||||
<span class="literal">else</span>
|
||||
bin += <span class="string">"</span><span class="string">0</span><span class="string">"</span>;
|
||||
|
||||
value /= <span class="number">2</span>;
|
||||
}
|
||||
<span class="literal">return</span> reverse(bin);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,397 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Maze.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}
|
||||
.literal {color: #cc7832}
|
||||
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.ST1 {color: #9876aa}
|
||||
.ST2 {color: #ffc66d}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST5 {color: #ffc66d; 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/MP3_Recursion_CalebFontenot/src/mp3_recursion_calebfontenot/Maze.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> mp3_recursion_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Optional;
|
||||
<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.ButtonType;
|
||||
<span class="literal">import</span> javafx.scene.control.Label;
|
||||
<span class="literal">import</span> javafx.scene.layout.BorderPane;
|
||||
<span class="literal">import</span> javafx.scene.layout.GridPane;
|
||||
<span class="literal">import</span> javafx.scene.layout.HBox;
|
||||
<span class="literal">import</span> javafx.scene.layout.StackPane;
|
||||
<span class="literal">import</span> javafx.scene.paint.Color;
|
||||
<span class="literal">import</span> javafx.scene.shape.Line;
|
||||
<span class="literal">import</span> javafx.scene.shape.Rectangle;
|
||||
<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> Maze <span class="literal">extends</span> Application {
|
||||
|
||||
<span class="literal">double</span> <span class="ST1">paneWidth</span> = <span class="number">600</span>;
|
||||
<span class="literal">double</span> <span class="ST1">paneHeight</span> = <span class="number">600</span>;
|
||||
|
||||
<span class="literal">private</span> Cell[][] <span class="ST1">board</span> = <span class="literal">new</span> Cell[<span class="number">8</span>][<span class="number">8</span>];
|
||||
<span class="literal">private</span> Button <span class="ST1">btFindPath</span> = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Find Path Top Left to Bottom Right</span><span class="string">"</span>);
|
||||
<span class="literal">private</span> Button <span class="ST1">btFindPath2</span> = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Find Path Top Right to Bottom Left</span><span class="string">"</span>);
|
||||
<span class="literal">private</span> Button <span class="ST1">btClearPath</span> = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Clear Path</span><span class="string">"</span>);
|
||||
<span class="literal">private</span> Label <span class="ST1">lblStatus</span> = <span class="literal">new</span> Label();
|
||||
|
||||
@Override <span class="comment">// Override the start method in the Application class</span>
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">start</span>(Stage primaryStage) {
|
||||
GridPane gridPane = <span class="literal">new</span> GridPane();
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="number">8</span>; i++) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < <span class="number">8</span>; j++) {
|
||||
gridPane.add(<span class="ST1">board</span>[i][j] = <span class="literal">new</span> Cell(), j, i);
|
||||
}
|
||||
}
|
||||
|
||||
HBox hBox = <span class="literal">new</span> HBox(<span class="number">5</span>);
|
||||
hBox.setAlignment(Pos.<span class="ST3">CENTER</span>);
|
||||
hBox.getChildren().addAll(<span class="ST1">b</span><span class="ST1">tFindPath</span>, <span class="ST1">b</span><span class="ST1">tFindPath2</span>, <span class="ST1">b</span><span class="ST1">tClearPath</span>);
|
||||
|
||||
BorderPane pane = <span class="literal">new</span> BorderPane();
|
||||
pane.setTop(<span class="ST1">l</span><span class="ST1">blStatus</span>);
|
||||
BorderPane.<span class="ST4">setAlignment</span>(<span class="ST1">l</span><span class="ST1">blStatus</span>, Pos.<span class="ST3">CENTER</span>);
|
||||
pane.setCenter(gridPane);
|
||||
pane.setBottom(hBox);
|
||||
|
||||
<span class="comment">// Create a scene and place it in the stage</span>
|
||||
Scene scene = <span class="literal">new</span> Scene(pane, <span class="ST1">p</span><span class="ST1">aneWidth</span>, <span class="ST1">paneHeight</span> + <span class="number">60</span>);
|
||||
primaryStage.setTitle(<span class="string">"</span><span class="string">Maze</span><span class="string">"</span>); <span class="comment">// Set the stage title</span>
|
||||
primaryStage.setScene(scene); <span class="comment">// Place the scene in the stage</span>
|
||||
primaryStage.show(); <span class="comment">// Display the stage</span>
|
||||
|
||||
<span class="ST1">btFindPath</span>.setOnAction(e -> findPath());
|
||||
<span class="ST1">btFindPath2</span>.setOnAction(e -> findPath2());
|
||||
<span class="ST1">btClearPath</span>.setOnAction(e -> clearPath());
|
||||
showSampleMessage();
|
||||
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">showSampleMessage</span>() {
|
||||
Alert a = <span class="literal">new</span> Alert(Alert.<span class="ST4">AlertType</span>.<span class="ST3">CONFIRMATION</span>);
|
||||
|
||||
a.setTitle(<span class="string">"</span><span class="string">Maze Information</span><span class="string">"</span>);
|
||||
a.setHeaderText(<span class="string">"</span><span class="string"> Put this message in its proper place. </span><span class="string">"</span>);
|
||||
a.setContentText(<span class="string">"</span><span class="string">This square cannot have an X</span><span class="string">"</span>);
|
||||
Optional<ButtonType> result = a.showAndWait();
|
||||
<span class="literal">if</span> (result.get() == ButtonType.<span class="ST3">OK</span>) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">OK </span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (result.get() == ButtonType.<span class="ST3">CANCEL</span>) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">CANCEL</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">clearCells</span>() {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="number">8</span>; ++i) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < <span class="number">8</span>; j++) {
|
||||
<span class="ST1">board</span>[i][j].resetCell();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">findPath</span>() {
|
||||
clearCells();
|
||||
<span class="literal">if</span> (findPath(<span class="number">0</span>, <span class="number">0</span>)) {
|
||||
<span class="ST1">lblStatus</span>.setText(<span class="string">"</span><span class="string">path found</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="ST1">lblStatus</span>.setText(<span class="string">"</span><span class="string">No path exists</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">findPath2</span>() {
|
||||
clearCells();
|
||||
<span class="literal">if</span> (findPath2(<span class="number">0</span>, <span class="number">7</span>)) {
|
||||
<span class="ST1">lblStatus</span>.setText(<span class="string">"</span><span class="string">path found</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="ST1">lblStatus</span>.setText(<span class="string">"</span><span class="string">No path exists</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">findPath</span>(<span class="literal">int</span> row, <span class="literal">int</span> col) {
|
||||
<span class="ST1">board</span>[row][col].visit();
|
||||
|
||||
<span class="literal">if</span> ((col == <span class="number">7</span>) && (row == <span class="number">7</span>)) {
|
||||
<span class="ST1">board</span>[row][col].selectCell();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((row > <span class="number">0</span>) && !<span class="ST1">board</span>[row - <span class="number">1</span>][col].marked()
|
||||
&& !<span class="ST1">board</span>[row - <span class="number">1</span>][col].blocked() && !<span class="ST1">board</span>[row - <span class="number">1</span>][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
<span class="literal">if</span> (findPath(row - <span class="number">1</span>, col)) {
|
||||
<span class="ST1">board</span>[row][col].selectCell();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((row < <span class="number">7</span>) && !<span class="ST1">board</span>[row + <span class="number">1</span>][col].marked()
|
||||
&& !<span class="ST1">board</span>[row + <span class="number">1</span>][col].blocked() && !<span class="ST1">board</span>[row + <span class="number">1</span>][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
<span class="literal">if</span> (findPath(row + <span class="number">1</span>, col)) {
|
||||
<span class="ST1">board</span>[row][col].selectCell();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((col > <span class="number">0</span>) && !<span class="ST1">board</span>[row][col - <span class="number">1</span>].marked()
|
||||
&& !<span class="ST1">board</span>[row][col - <span class="number">1</span>].blocked() && !<span class="ST1">board</span>[row][col - <span class="number">1</span>].visited()) {
|
||||
block(row, col);
|
||||
<span class="literal">if</span> (findPath(row, col - <span class="number">1</span>)) {
|
||||
<span class="ST1">board</span>[row][col].selectCell();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((col < <span class="number">7</span>) && !<span class="ST1">board</span>[row][col + <span class="number">1</span>].marked()
|
||||
&& !<span class="ST1">board</span>[row][col + <span class="number">1</span>].blocked() && !<span class="ST1">board</span>[row][col + <span class="number">1</span>].visited()) {
|
||||
block(row, col);
|
||||
<span class="literal">if</span> (findPath(row, col + <span class="number">1</span>)) {
|
||||
<span class="ST1">board</span>[row][col].selectCell();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">findPath2</span>(<span class="literal">int</span> row, <span class="literal">int</span> col) {
|
||||
<span class="ST1">board</span>[row][col].visit();
|
||||
|
||||
<span class="literal">if</span> ((col == <span class="number">0</span>) && (row == <span class="number">7</span>)) {
|
||||
<span class="ST1">board</span>[row][col].selectCellGreen();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((row > <span class="number">0</span>) && !<span class="ST1">board</span>[row - <span class="number">1</span>][col].marked()
|
||||
&& !<span class="ST1">board</span>[row - <span class="number">1</span>][col].blocked() && !<span class="ST1">board</span>[row - <span class="number">1</span>][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
<span class="literal">if</span> (findPath2(row - <span class="number">1</span>, col)) {
|
||||
<span class="ST1">board</span>[row][col].selectCellGreen();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((row < <span class="number">7</span>) && !<span class="ST1">board</span>[row + <span class="number">1</span>][col].marked()
|
||||
&& !<span class="ST1">board</span>[row + <span class="number">1</span>][col].blocked() && !<span class="ST1">board</span>[row + <span class="number">1</span>][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
<span class="literal">if</span> (findPath2(row + <span class="number">1</span>, col)) {
|
||||
<span class="ST1">board</span>[row][col].selectCellGreen();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((col > <span class="number">0</span>) && !<span class="ST1">board</span>[row][col - <span class="number">1</span>].marked()
|
||||
&& !<span class="ST1">board</span>[row][col - <span class="number">1</span>].blocked() && !<span class="ST1">board</span>[row][col - <span class="number">1</span>].visited()) {
|
||||
block(row, col);
|
||||
<span class="literal">if</span> (findPath2(row, col - <span class="number">1</span>)) {
|
||||
<span class="ST1">board</span>[row][col].selectCellGreen();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">if</span> ((col < <span class="number">7</span>) && !<span class="ST1">board</span>[row][col + <span class="number">1</span>].marked()
|
||||
&& !<span class="ST1">board</span>[row][col + <span class="number">1</span>].blocked() && !<span class="ST1">board</span>[row][col + <span class="number">1</span>].visited()) {
|
||||
block(row, col);
|
||||
<span class="literal">if</span> (findPath2(row, col + <span class="number">1</span>)) {
|
||||
<span class="ST1">board</span>[row][col].selectCellGreen();
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// Temporary block the neighbor to prevent neighboring path</span>
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">block</span>(<span class="literal">int</span> row, <span class="literal">int</span> col) {
|
||||
<span class="literal">if</span> (row > <span class="number">0</span>) {
|
||||
<span class="ST1">board</span>[row - <span class="number">1</span>][col].block();
|
||||
}
|
||||
|
||||
<span class="literal">if</span> (row < <span class="number">7</span>) {
|
||||
<span class="ST1">board</span>[row + <span class="number">1</span>][col].block();
|
||||
}
|
||||
|
||||
<span class="literal">if</span> (col > <span class="number">0</span>) {
|
||||
<span class="ST1">board</span>[row][col - <span class="number">1</span>].block();
|
||||
}
|
||||
|
||||
<span class="literal">if</span> (col < <span class="number">7</span>) {
|
||||
<span class="ST1">board</span>[row][col + <span class="number">1</span>].block();
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">// Remove the temporary block</span>
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">unblock</span>(<span class="literal">int</span> row, <span class="literal">int</span> col) {
|
||||
<span class="literal">if</span> (row > <span class="number">0</span>) {
|
||||
<span class="ST1">board</span>[row - <span class="number">1</span>][col].unblock();
|
||||
}
|
||||
|
||||
<span class="literal">if</span> (row < <span class="number">7</span>) {
|
||||
<span class="ST1">board</span>[row + <span class="number">1</span>][col].unblock();
|
||||
}
|
||||
|
||||
<span class="literal">if</span> (col > <span class="number">0</span>) {
|
||||
<span class="ST1">board</span>[row][col - <span class="number">1</span>].unblock();
|
||||
}
|
||||
|
||||
<span class="literal">if</span> (col < <span class="number">7</span>) {
|
||||
<span class="ST1">board</span>[row][col + <span class="number">1</span>].unblock();
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">clearPath</span>() {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> row = <span class="number">0</span>; row < <span class="ST1">board</span>.<span class="ST1">length</span>; row++) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> col = <span class="number">0</span>; col < <span class="ST1">board</span>[row].<span class="ST1">length</span>; col++) {
|
||||
<span class="ST1">board</span>[row][col].deselectCell();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<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);
|
||||
}
|
||||
|
||||
<span class="comment">// Inner class</span>
|
||||
<span class="literal">class</span> Cell <span class="literal">extends</span> StackPane {
|
||||
|
||||
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST1">marked</span> = <span class="literal">false</span>;
|
||||
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST1">visited</span> = <span class="literal">false</span>;
|
||||
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST1">blocked</span> = <span class="literal">false</span>;
|
||||
|
||||
<span class="literal">double</span> <span class="ST1">width</span> = <span class="ST1">paneWidth</span> / <span class="number">8</span>;
|
||||
<span class="literal">double</span> <span class="ST1">height</span> = <span class="ST1">paneHeight</span> / <span class="number">8</span>;
|
||||
<span class="literal">private</span> Rectangle <span class="ST1">rectangle</span> = <span class="literal">new</span> Rectangle(<span class="number">0</span>, <span class="number">0</span>, <span class="ST1">w</span><span class="ST1">idth</span>, <span class="ST1">h</span><span class="ST1">eight</span>);
|
||||
|
||||
Line <span class="ST1">line1</span> = <span class="literal">new</span> Line(<span class="number">0</span>, <span class="number">0</span>, <span class="ST1">w</span><span class="ST1">idth</span>, <span class="ST1">h</span><span class="ST1">eight</span>);
|
||||
Line <span class="ST1">line2</span> = <span class="literal">new</span> Line(<span class="ST1">w</span><span class="ST1">idth</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="ST1">h</span><span class="ST1">eight</span>);
|
||||
|
||||
<span class="literal">public</span> Cell() {
|
||||
<span class="literal">this</span>.getChildren().add(<span class="ST1">r</span><span class="ST1">ectangle</span>);
|
||||
<span class="ST1">rectangle</span>.setFill(Color.<span class="ST3">WHITE</span>);
|
||||
<span class="ST1">rectangle</span>.setStroke(Color.<span class="ST3">BLACK</span>);
|
||||
|
||||
<span class="literal">this</span>.setOnMousePressed(e
|
||||
-> {
|
||||
<span class="ST1">marked</span> = !<span class="ST1">marked</span>;
|
||||
<span class="literal">if</span> (<span class="ST1">marked</span>) {
|
||||
mark();
|
||||
} <span class="literal">else</span> {
|
||||
unmark();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">mark</span>() {
|
||||
<span class="literal">this</span>.getChildren().addAll(<span class="ST1">l</span><span class="ST1">ine1</span>, <span class="ST1">l</span><span class="ST1">ine2</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">unmark</span>() {
|
||||
<span class="literal">this</span>.getChildren().remove(<span class="ST1">l</span><span class="ST1">ine1</span>);
|
||||
<span class="literal">this</span>.getChildren().remove(<span class="ST1">l</span><span class="ST1">ine2</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">marked</span>() {
|
||||
<span class="literal">return</span> <span class="ST1">marked</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">visit</span>() {
|
||||
<span class="ST1">visited</span> = <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">visited</span>() {
|
||||
<span class="literal">return</span> <span class="ST1">visited</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">blocked</span>() {
|
||||
<span class="literal">return</span> <span class="ST1">blocked</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">block</span>() {
|
||||
<span class="ST1">blocked</span> = <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">unblock</span>() {
|
||||
<span class="ST1">blocked</span> = <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">selectCell</span>() {
|
||||
<span class="literal">if</span> (<span class="ST1">rectangle</span>.getFill().equals(Color.<span class="ST3">GREEN</span>)) {
|
||||
<span class="ST1">rectangle</span>.setFill(Color.<span class="ST3">YELLOW</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="ST1">rectangle</span>.setFill(Color.<span class="ST3">RED</span>);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">selectCellGreen</span>() {
|
||||
<span class="literal">if</span> (<span class="ST1">rectangle</span>.getFill().equals(Color.<span class="ST3">RED</span>)) {
|
||||
<span class="ST1">rectangle</span>.setFill(Color.<span class="ST3">YELLOW</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="ST1">rectangle</span>.setFill(Color.<span class="ST3">GREEN</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">deselectCell</span>() {
|
||||
<span class="ST1">rectangle</span>.setFill(Color.<span class="ST3">WHITE</span>);
|
||||
<span class="ST1">blocked</span> = <span class="literal">false</span>;
|
||||
<span class="ST1">visited</span> = <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">resetCell</span>() {
|
||||
<span class="comment">//rectangle.setFill(Color.WHITE);</span>
|
||||
<span class="ST1">blocked</span> = <span class="literal">false</span>;
|
||||
<span class="ST1">visited</span> = <span class="literal">false</span>;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,82 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>SelectionSortR.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}
|
||||
.literal {color: #cc7832}
|
||||
.ST2 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.ST3 {color: #9876aa}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST4 {color: #9876aa; 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/MP3_Recursion_CalebFontenot/src/mp3_recursion_calebfontenot/SelectionSortR.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> edu.slcc.asdv.caleb.lab5.recursion2_calebfontenot;
|
||||
|
||||
<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> SelectionSortR {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">selectionSortR</span>(<span class="literal">int</span>[] arr) {
|
||||
<span class="ST2">selectionSortR</span>(arr, <span class="number">0</span>, arr.<span class="ST3">length</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">swap</span>(<span class="literal">int</span>[] arr, <span class="literal">int</span> i, <span class="literal">int</span> j) {
|
||||
<span class="literal">int</span> temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
<span class="ST2">printArray</span>(arr);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">selectionSortR</span>(<span class="literal">int</span>[] arr, <span class="literal">int</span> i, <span class="literal">int</span> n) {
|
||||
<span class="comment">// Selection sort</span>
|
||||
<span class="literal">int</span> min = i;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = i + <span class="number">1</span>; j < n; ++j) {
|
||||
<span class="literal">if</span> (arr[j] < arr[min]) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
<span class="ST2">swap</span>(arr, min, i);
|
||||
<span class="literal">if</span> (i + <span class="number">1</span> < n) {
|
||||
<span class="ST2">selectionSortR</span>(arr, i + <span class="number">1</span>, n);
|
||||
}
|
||||
}
|
||||
|
||||
<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">int</span>[] arr = {
|
||||
<span class="number">10</span>, <span class="number">1</span>, <span class="number">20</span>, <span class="number">3</span>
|
||||
};
|
||||
<span class="ST2">printArray</span>(arr);
|
||||
<span class="ST2">selectionSortR</span>(arr);
|
||||
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printArray</span>(<span class="literal">int</span>[] arr) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < arr.<span class="ST3">length</span>; ++i) {
|
||||
System.<span class="ST4">out</span>.print(arr[i] + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
System.<span class="ST4">out</span>.println();
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,61 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>StringPermuatation.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}
|
||||
.literal {color: #cc7832}
|
||||
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.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}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP3_Recursion_CalebFontenot/src/mp3_recursion_calebfontenot/StringPermuatation.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> mp3_recursion_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<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> StringPermuatation {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
||||
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter a string: </span><span class="string">"</span>);
|
||||
String s = input.nextLine();
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">The permuatation for </span><span class="string">"</span> + s + <span class="string">"</span><span class="string"> is:</span><span class="string">"</span>);
|
||||
<span class="ST3">displayPermutation</span>(s);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">displayPermutation</span>(String s) {
|
||||
<span class="ST3">displayPermutation</span>(<span class="string">"</span><span class="string">"</span>, s);
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">displayPermutation</span>(String s1, String s2) {
|
||||
<span class="literal">if</span> (s2.equals(<span class="string">"</span><span class="string">"</span>)) {
|
||||
System.<span class="ST2">out</span>.println(s1);
|
||||
}
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < s2.length(); ++i) {
|
||||
<span class="ST3">displayPermutation</span>(s1.concat(<span class="string">""</span> + s2.charAt(i)), s2.substring(<span class="number">0</span>, i).concat(s2.substring(i + <span class="number">1</span>)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -4,7 +4,6 @@
|
||||
*/
|
||||
package mp3_recursion_calebfontenot;
|
||||
|
||||
|
||||
import java.util.Optional;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
@@ -26,32 +25,29 @@ import javafx.stage.Stage;
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Maze extends Application
|
||||
{
|
||||
public class Maze extends Application {
|
||||
|
||||
double paneWidth = 440;
|
||||
double paneHeight = 440;
|
||||
double paneWidth = 600;
|
||||
double paneHeight = 600;
|
||||
|
||||
private Cell[][] board = new Cell[8][8];
|
||||
private Button btFindPath = new Button("Find Path Top Left to Bottom Right");
|
||||
private Button btFindPath2 = new Button("Find Path Top Right to Bottom Left");
|
||||
private Button btClearPath = new Button("Clear Path");
|
||||
private Label lblStatus = new Label();
|
||||
|
||||
@Override // Override the start method in the Application class
|
||||
public void start(Stage primaryStage)
|
||||
{
|
||||
public void start(Stage primaryStage) {
|
||||
GridPane gridPane = new GridPane();
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
for (int i = 0; i < 8; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
gridPane.add(board[i][j] = new Cell(), j, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HBox hBox = new HBox(5);
|
||||
hBox.setAlignment(Pos.CENTER);
|
||||
hBox.getChildren().addAll(btFindPath, btClearPath);
|
||||
hBox.getChildren().addAll(btFindPath, btFindPath2, btClearPath);
|
||||
|
||||
BorderPane pane = new BorderPane();
|
||||
pane.setTop(lblStatus);
|
||||
@@ -66,175 +62,217 @@ public class Maze extends Application
|
||||
primaryStage.show(); // Display the stage
|
||||
|
||||
btFindPath.setOnAction(e -> findPath());
|
||||
btFindPath2.setOnAction(e -> findPath2());
|
||||
btClearPath.setOnAction(e -> clearPath());
|
||||
showSampleMessage();
|
||||
|
||||
}
|
||||
|
||||
public void showSampleMessage()
|
||||
{
|
||||
public void showSampleMessage() {
|
||||
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
|
||||
|
||||
a.setTitle("Maze Information");
|
||||
a.setHeaderText(" Put this message in its proper place. ");
|
||||
a.setContentText("This square cannot have an X");
|
||||
Optional<ButtonType> result = a.showAndWait();
|
||||
if (result.get() == ButtonType.OK)
|
||||
{
|
||||
if (result.get() == ButtonType.OK) {
|
||||
System.out.println("OK ");
|
||||
}
|
||||
else if (result.get() == ButtonType.CANCEL)
|
||||
{
|
||||
} else if (result.get() == ButtonType.CANCEL) {
|
||||
System.out.println("CANCEL");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void findPath()
|
||||
{
|
||||
if (findPath(0, 0))
|
||||
{
|
||||
public void clearCells() {
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
board[i][j].resetCell();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void findPath() {
|
||||
clearCells();
|
||||
if (findPath(0, 0)) {
|
||||
lblStatus.setText("path found");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
lblStatus.setText("No path exists");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean findPath(int row, int col)
|
||||
{
|
||||
public void findPath2() {
|
||||
clearCells();
|
||||
if (findPath2(0, 7)) {
|
||||
lblStatus.setText("path found");
|
||||
} else {
|
||||
lblStatus.setText("No path exists");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean findPath(int row, int col) {
|
||||
board[row][col].visit();
|
||||
|
||||
if ((col == 7) && (row == 7))
|
||||
{
|
||||
if ((col == 7) && (row == 7)) {
|
||||
board[row][col].selectCell();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ((row > 0) && !board[row - 1][col].marked()
|
||||
&& !board[row - 1][col].blocked() && !board[row - 1][col].visited())
|
||||
{
|
||||
&& !board[row - 1][col].blocked() && !board[row - 1][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
if (findPath(row - 1, col))
|
||||
{
|
||||
if (findPath(row - 1, col)) {
|
||||
board[row][col].selectCell();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
}
|
||||
|
||||
if ((row < 7) && !board[row + 1][col].marked()
|
||||
&& !board[row + 1][col].blocked() && !board[row + 1][col].visited())
|
||||
{
|
||||
&& !board[row + 1][col].blocked() && !board[row + 1][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
if (findPath(row + 1, col))
|
||||
{
|
||||
if (findPath(row + 1, col)) {
|
||||
board[row][col].selectCell();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
}
|
||||
|
||||
if ((col > 0) && !board[row][col - 1].marked()
|
||||
&& !board[row][col - 1].blocked() && !board[row][col - 1].visited())
|
||||
{
|
||||
&& !board[row][col - 1].blocked() && !board[row][col - 1].visited()) {
|
||||
block(row, col);
|
||||
if (findPath(row, col - 1))
|
||||
{
|
||||
if (findPath(row, col - 1)) {
|
||||
board[row][col].selectCell();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
}
|
||||
|
||||
if ((col < 7) && !board[row][col + 1].marked()
|
||||
&& !board[row][col + 1].blocked() && !board[row][col + 1].visited())
|
||||
{
|
||||
&& !board[row][col + 1].blocked() && !board[row][col + 1].visited()) {
|
||||
block(row, col);
|
||||
if (findPath(row, col + 1))
|
||||
{
|
||||
if (findPath(row, col + 1)) {
|
||||
board[row][col].selectCell();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean findPath2(int row, int col) {
|
||||
board[row][col].visit();
|
||||
|
||||
if ((col == 0) && (row == 7)) {
|
||||
board[row][col].selectCellGreen();
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((row > 0) && !board[row - 1][col].marked()
|
||||
&& !board[row - 1][col].blocked() && !board[row - 1][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
if (findPath2(row - 1, col)) {
|
||||
board[row][col].selectCellGreen();
|
||||
return true;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
if ((row < 7) && !board[row + 1][col].marked()
|
||||
&& !board[row + 1][col].blocked() && !board[row + 1][col].visited()) {
|
||||
block(row, col);
|
||||
|
||||
if (findPath2(row + 1, col)) {
|
||||
board[row][col].selectCellGreen();
|
||||
return true;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
if ((col > 0) && !board[row][col - 1].marked()
|
||||
&& !board[row][col - 1].blocked() && !board[row][col - 1].visited()) {
|
||||
block(row, col);
|
||||
if (findPath2(row, col - 1)) {
|
||||
board[row][col].selectCellGreen();
|
||||
return true;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
if ((col < 7) && !board[row][col + 1].marked()
|
||||
&& !board[row][col + 1].blocked() && !board[row][col + 1].visited()) {
|
||||
block(row, col);
|
||||
if (findPath2(row, col + 1)) {
|
||||
board[row][col].selectCellGreen();
|
||||
return true;
|
||||
}
|
||||
|
||||
unblock(row, col);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Temporary block the neighbor to prevent neighboring path
|
||||
public void block(int row, int col)
|
||||
{
|
||||
if (row > 0)
|
||||
{
|
||||
public void block(int row, int col) {
|
||||
if (row > 0) {
|
||||
board[row - 1][col].block();
|
||||
}
|
||||
}
|
||||
|
||||
if (row < 7)
|
||||
{
|
||||
if (row < 7) {
|
||||
board[row + 1][col].block();
|
||||
}
|
||||
}
|
||||
|
||||
if (col > 0)
|
||||
{
|
||||
if (col > 0) {
|
||||
board[row][col - 1].block();
|
||||
}
|
||||
}
|
||||
|
||||
if (col < 7)
|
||||
{
|
||||
if (col < 7) {
|
||||
board[row][col + 1].block();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the temporary block
|
||||
public void unblock(int row, int col)
|
||||
{
|
||||
if (row > 0)
|
||||
{
|
||||
public void unblock(int row, int col) {
|
||||
if (row > 0) {
|
||||
board[row - 1][col].unblock();
|
||||
}
|
||||
}
|
||||
|
||||
if (row < 7)
|
||||
{
|
||||
if (row < 7) {
|
||||
board[row + 1][col].unblock();
|
||||
}
|
||||
}
|
||||
|
||||
if (col > 0)
|
||||
{
|
||||
if (col > 0) {
|
||||
board[row][col - 1].unblock();
|
||||
}
|
||||
}
|
||||
|
||||
if (col < 7)
|
||||
{
|
||||
if (col < 7) {
|
||||
board[row][col + 1].unblock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clearPath()
|
||||
{
|
||||
for (int row = 0; row < board.length; row++)
|
||||
{
|
||||
for (int col = 0; col < board[row].length; col++)
|
||||
{
|
||||
public void clearPath() {
|
||||
for (int row = 0; row < board.length; row++) {
|
||||
for (int col = 0; col < board[row].length; col++) {
|
||||
board[row][col].deselectCell();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
|
||||
// Inner class
|
||||
class Cell extends StackPane
|
||||
{
|
||||
class Cell extends StackPane {
|
||||
|
||||
private boolean marked = false;
|
||||
private boolean visited = false;
|
||||
@@ -247,79 +285,83 @@ public class Maze extends Application
|
||||
Line line1 = new Line(0, 0, width, height);
|
||||
Line line2 = new Line(width, 0, 0, height);
|
||||
|
||||
public Cell()
|
||||
{
|
||||
public Cell() {
|
||||
this.getChildren().add(rectangle);
|
||||
rectangle.setFill(Color.WHITE);
|
||||
rectangle.setStroke(Color.BLACK);
|
||||
|
||||
this.setOnMousePressed(e ->
|
||||
{
|
||||
this.setOnMousePressed(e
|
||||
-> {
|
||||
marked = !marked;
|
||||
if (marked)
|
||||
{
|
||||
if (marked) {
|
||||
mark();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
unmark();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void mark()
|
||||
{
|
||||
public void mark() {
|
||||
this.getChildren().addAll(line1, line2);
|
||||
}
|
||||
|
||||
public void unmark()
|
||||
{
|
||||
public void unmark() {
|
||||
this.getChildren().remove(line1);
|
||||
this.getChildren().remove(line2);
|
||||
}
|
||||
|
||||
public boolean marked()
|
||||
{
|
||||
public boolean marked() {
|
||||
return marked;
|
||||
}
|
||||
|
||||
public void visit()
|
||||
{
|
||||
public void visit() {
|
||||
visited = true;
|
||||
}
|
||||
|
||||
public boolean visited()
|
||||
{
|
||||
public boolean visited() {
|
||||
return visited;
|
||||
}
|
||||
|
||||
public boolean blocked()
|
||||
{
|
||||
public boolean blocked() {
|
||||
return blocked;
|
||||
}
|
||||
|
||||
public void block()
|
||||
{
|
||||
public void block() {
|
||||
blocked = true;
|
||||
}
|
||||
|
||||
public void unblock()
|
||||
{
|
||||
public void unblock() {
|
||||
blocked = false;
|
||||
}
|
||||
|
||||
public void selectCell()
|
||||
{
|
||||
rectangle.setFill(Color.RED);
|
||||
public void selectCell() {
|
||||
if (rectangle.getFill().equals(Color.GREEN)) {
|
||||
rectangle.setFill(Color.YELLOW);
|
||||
} else {
|
||||
rectangle.setFill(Color.RED);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void deselectCell()
|
||||
{
|
||||
public void selectCellGreen() {
|
||||
if (rectangle.getFill().equals(Color.RED)) {
|
||||
rectangle.setFill(Color.YELLOW);
|
||||
} else {
|
||||
rectangle.setFill(Color.GREEN);
|
||||
}
|
||||
}
|
||||
|
||||
public void deselectCell() {
|
||||
rectangle.setFill(Color.WHITE);
|
||||
blocked = false;
|
||||
visited = false;
|
||||
}
|
||||
|
||||
public void resetCell() {
|
||||
//rectangle.setFill(Color.WHITE);
|
||||
blocked = false;
|
||||
visited = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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 edu.slcc.asdv.caleb.lab5.recursion2_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class SelectionSortR {
|
||||
|
||||
public static void selectionSortR(int[] arr) {
|
||||
selectionSortR(arr, 0, arr.length);
|
||||
}
|
||||
|
||||
public static void swap(int[] arr, int i, int j) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
printArray(arr);
|
||||
}
|
||||
|
||||
public static void selectionSortR(int[] arr, int i, int n) {
|
||||
// Selection sort
|
||||
int min = i;
|
||||
for (int j = i + 1; j < n; ++j) {
|
||||
if (arr[j] < arr[min]) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
swap(arr, min, i);
|
||||
if (i + 1 < n) {
|
||||
selectionSortR(arr, i + 1, n);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {
|
||||
10, 1, 20, 3
|
||||
};
|
||||
printArray(arr);
|
||||
selectionSortR(arr);
|
||||
|
||||
}
|
||||
|
||||
public static void printArray(int[] arr) {
|
||||
for (int i = 0; i < arr.length; ++i) {
|
||||
System.out.print(arr[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
@@ -20,11 +20,14 @@ public class StringPermuatation {
|
||||
}
|
||||
|
||||
public static void displayPermutation(String s) {
|
||||
displayPermutation(" ", s);
|
||||
displayPermutation("", s);
|
||||
}
|
||||
public static void displayPermutation(String s1, String s2) {
|
||||
if (s2.equals("")) {
|
||||
|
||||
System.out.println(s1);
|
||||
}
|
||||
for (int i = 0; i < s2.length(); ++i) {
|
||||
displayPermutation(s1.concat("" + s2.charAt(i)), s2.substring(0, i).concat(s2.substring(i + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,93 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>GenericStack.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}
|
||||
.literal {color: #cc7832}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/GenericStack.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"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.ArrayList;
|
||||
<span class="literal">import</span> java.util.EmptyStackException;
|
||||
|
||||
<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> GenericStack<T> {
|
||||
|
||||
<span class="literal">private</span> T[] elements;
|
||||
ArrayList<T> elementsList = <span class="literal">new</span> ArrayList<T>();
|
||||
<span class="literal">private</span> <span class="literal">int</span> top;
|
||||
<span class="literal">static</span> <span class="literal">int</span> size = <span class="number">4</span>;
|
||||
|
||||
<span class="literal">public</span> GenericStack(<span class="literal">int</span> size) {
|
||||
elements = (T[]) <span class="literal">new</span> Object[size];
|
||||
}
|
||||
<span class="literal">public</span> GenericStack()
|
||||
{
|
||||
elements = (T[]) <span class="literal">new</span> Object[size];
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> push (T element) {
|
||||
<span class="literal">if</span> (top == size) {
|
||||
<span class="literal">throw</span> <span class="literal">new</span> StackOverflowError();
|
||||
}
|
||||
elements[top++] = element;
|
||||
elementsList.add(element);
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> T pop() {
|
||||
<span class="literal">if</span> (top == <span class="number">0</span>) {
|
||||
<span class="literal">throw</span> <span class="literal">new</span> EmptyStackException();
|
||||
}
|
||||
--top;
|
||||
elementsList.remove(top);
|
||||
<span class="literal">return</span> elements[top];
|
||||
}
|
||||
|
||||
<span class="literal">public</span> T peek() {
|
||||
<span class="literal">if</span> (top == <span class="number">0</span>) {
|
||||
<span class="literal">throw</span> <span class="literal">new</span> EmptyStackException();
|
||||
}
|
||||
<span class="literal">return</span> elements[top - <span class="number">1</span>];
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> isEmpty() {
|
||||
<span class="literal">boolean</span> returnBoolean = <span class="literal">false</span>;
|
||||
<span class="literal">if</span> (top == <span class="number">0</span>) {
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
<span class="literal">return</span> returnBoolean;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> String toString()
|
||||
{
|
||||
String returnString = <span class="string">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = top -<span class="number">1</span>; i >= <span class="number">0</span>; --i) {
|
||||
returnString += elements[i].toString() + <span class="string">"</span><span class="string">, </span><span class="string">"</span>;
|
||||
}
|
||||
<span class="literal">return</span> returnString;
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,70 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Max.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}
|
||||
.literal {color: #cc7832}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/Max.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> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Max {
|
||||
|
||||
public static Comparable max(Comparable o1, Comparable o2)
|
||||
{
|
||||
if (o1.compareTo(o2) > 0) {
|
||||
return o1;
|
||||
} else {
|
||||
return o2;
|
||||
}
|
||||
}
|
||||
public static <E extends Comparable<E>> E maxSafe(E e1, E e2) {
|
||||
if(e1.compareTo(e2) > 0) {
|
||||
return e1;
|
||||
} else {
|
||||
return e2;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println(max(1, 2));
|
||||
try {
|
||||
System.out.println(maxSafe(1, 2));
|
||||
System.out.println(maxSafe("abc", "ABC"));
|
||||
System.out.println();
|
||||
//System.out.println(maxSafe(1, "two"));
|
||||
|
||||
GenericStack stackUnsafe = new GenericStack();
|
||||
GenericStack<Integer> stackSafe = new GenericStack();
|
||||
stackSafe.push(1); stackSafe.push(2);
|
||||
System.out.println(stackSafe);
|
||||
stackUnsafe.push(1); stackUnsafe.push("two");
|
||||
System.out.println("This line compiles but crashes the program " + max(1, "two"));
|
||||
} catch (ClassCastException e) {
|
||||
System.err.println("RAW TYPES ARE UNSAFE " + e.getMessage()) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,56 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>NoWildCard.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}
|
||||
.literal {color: #cc7832}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/NoWildCard.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> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
<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> NoWildCard {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">double</span> max(GenericStack<Integer> stack) {
|
||||
<span class="literal">double</span> max = Double.MIN_VALUE;
|
||||
|
||||
<span class="literal">while</span> (!stack.isEmpty()) {
|
||||
<span class="literal">double</span> value = stack.pop().doubleValue();
|
||||
<span class="literal">if</span> (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> max;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
GenericStack<Integer> intStack = <span class="literal">new</span> GenericStack<>();
|
||||
intStack.push(<span class="number">1</span>);
|
||||
intStack.push(<span class="number">2</span>);
|
||||
intStack.push(-<span class="number">2</span>);
|
||||
System.out.print(<span class="string">"</span><span class="string">The max number is </span><span class="string">"</span> + max(intStack));
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,60 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WildCard.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}
|
||||
.literal {color: #cc7832}
|
||||
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.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}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/WildCard.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> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
<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> WildCard {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">double</span> <span class="ST1">max</span>(GenericStack<? <span class="literal">extends</span> Number> stack) {
|
||||
<span class="literal">double</span> max = Double.<span class="ST2">MIN_VALUE</span>;
|
||||
|
||||
<span class="literal">while</span> (!stack.isEmpty()) {
|
||||
<span class="literal">double</span> value = stack.pop().doubleValue();
|
||||
<span class="literal">if</span> (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> max;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
||||
GenericStack<Integer> intStack = <span class="literal">new</span> GenericStack<>();
|
||||
intStack.push(<span class="number">1</span>);
|
||||
intStack.push(<span class="number">2</span>);
|
||||
intStack.push(-<span class="number">2</span>);
|
||||
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">The max number is </span><span class="string">"</span> + <span class="ST3">max</span>(intStack));
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WildCard2.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}
|
||||
.literal {color: #cc7832}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/WildCard2.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> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
<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> WildCard2 {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> print(GenericStack<?> stack) {
|
||||
<span class="literal">while</span> (!stack.isEmpty()) {
|
||||
System.out.print(stack.pop() + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
GenericStack<Integer> intStack = <span class="literal">new</span> GenericStack<>();
|
||||
intStack.push(<span class="number">1</span>);
|
||||
intStack.push(<span class="number">2</span>);
|
||||
intStack.push(-<span class="number">2</span>);
|
||||
|
||||
print(intStack);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WildCardWithSuper.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}
|
||||
.literal {color: #cc7832}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/WildCardWithSuper.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> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
<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> WildCardWithSuper {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <T> <span class="literal">void</span> add(GenericStack<T> stack1, GenericStack<? <span class="literal">super</span> T> stack2) {
|
||||
<span class="literal">while</span>(!stack1.isEmpty()) {
|
||||
stack2.push(stack1.pop());
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
GenericStack<String> stack1 = <span class="literal">new</span> GenericStack<>();
|
||||
GenericStack<Object> stack2 = <span class="literal">new</span> GenericStack<>();
|
||||
stack2.push(<span class="string">"</span><span class="string">one</span><span class="string">"</span>);
|
||||
stack2.push(<span class="number">2</span>);
|
||||
stack1.push(<span class="string">"</span><span class="string">one</span><span class="string">"</span>);
|
||||
|
||||
add(stack1, stack2);
|
||||
WildCard2.print(stack2);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class GenericStack<T> {
|
||||
|
||||
private T[] elements;
|
||||
ArrayList<T> elementsList = new ArrayList<T>();
|
||||
private int top;
|
||||
static int size = 4;
|
||||
|
||||
public GenericStack(int size) {
|
||||
elements = (T[]) new Object[size];
|
||||
}
|
||||
public GenericStack()
|
||||
{
|
||||
elements = (T[]) new Object[size];
|
||||
}
|
||||
public boolean push (T element) {
|
||||
if (top == size) {
|
||||
throw new StackOverflowError();
|
||||
}
|
||||
elements[top++] = element;
|
||||
elementsList.add(element);
|
||||
return true;
|
||||
}
|
||||
|
||||
public T pop() {
|
||||
if (top == 0) {
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
--top;
|
||||
elementsList.remove(top);
|
||||
return elements[top];
|
||||
}
|
||||
|
||||
public T peek() {
|
||||
if (top == 0) {
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
return elements[top - 1];
|
||||
}
|
||||
public boolean isEmpty() {
|
||||
boolean returnBoolean = false;
|
||||
if (top == 0) {
|
||||
return true;
|
||||
}
|
||||
return returnBoolean;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String returnString = "";
|
||||
for (int i = top -1; i >= 0; --i) {
|
||||
returnString += elements[i].toString() + ", ";
|
||||
}
|
||||
return returnString;
|
||||
}
|
||||
}
|
@@ -34,6 +34,12 @@ public class Max {
|
||||
System.out.println(maxSafe("abc", "ABC"));
|
||||
System.out.println();
|
||||
//System.out.println(maxSafe(1, "two"));
|
||||
|
||||
GenericStack stackUnsafe = new GenericStack();
|
||||
GenericStack<Integer> stackSafe = new GenericStack();
|
||||
stackSafe.push(1); stackSafe.push(2);
|
||||
System.out.println(stackSafe);
|
||||
stackUnsafe.push(1); stackUnsafe.push("two");
|
||||
System.out.println("This line compiles but crashes the program " + max(1, "two"));
|
||||
} catch (ClassCastException e) {
|
||||
System.err.println("RAW TYPES ARE UNSAFE " + e.getMessage()) ;
|
||||
|
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class NoWildCard {
|
||||
|
||||
public static double max(GenericStack<Integer> stack) {
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
double value = stack.pop().doubleValue();
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
System.out.print("The max number is " + max(intStack));
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WildCard {
|
||||
|
||||
public static double max(GenericStack<? extends Number> stack) {
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
double value = stack.pop().doubleValue();
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
System.out.print("The max number is " + max(intStack));
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WildCard2 {
|
||||
public static void print(GenericStack<?> stack) {
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.print(stack.pop() + " ");
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
|
||||
print(intStack);
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WildCardWithSuper {
|
||||
public static <T> void add(GenericStack<T> stack1, GenericStack<? super T> stack2) {
|
||||
while(!stack1.isEmpty()) {
|
||||
stack2.push(stack1.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<String> stack1 = new GenericStack<>();
|
||||
GenericStack<Object> stack2 = new GenericStack<>();
|
||||
stack2.push("one");
|
||||
stack2.push(2);
|
||||
stack1.push("one");
|
||||
|
||||
add(stack1, stack2);
|
||||
WildCard2.print(stack2);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user