BigIntegerFib lol
This commit is contained in:
parent
780c56095f
commit
d84b96f688
3
.gitignore
vendored
3
.gitignore
vendored
@ -172,3 +172,6 @@
|
|||||||
/Semester 3/GenericStack/target/
|
/Semester 3/GenericStack/target/
|
||||||
/Semester 3/Assignments/lab6_generics_CalebFontenot/target/
|
/Semester 3/Assignments/lab6_generics_CalebFontenot/target/
|
||||||
/Semester 3/Exams/Projects/ProgramingExam1_CalebFontenot/target/
|
/Semester 3/Exams/Projects/ProgramingExam1_CalebFontenot/target/
|
||||||
|
/Semester 3/Assignments/MP3_Recursion_CalebFontenot/dist/
|
||||||
|
/BigIntegerFibonacci/nbproject/private/
|
||||||
|
/BigIntegerFibonacci/build/
|
||||||
|
73
BigIntegerFibonacci/build.xml
Normal file
73
BigIntegerFibonacci/build.xml
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!-- You may freely edit this file. See commented blocks below for -->
|
||||||
|
<!-- some examples of how to customize the build. -->
|
||||||
|
<!-- (If you delete it and reopen the project it will be recreated.) -->
|
||||||
|
<!-- By default, only the Clean and Build commands use this build script. -->
|
||||||
|
<!-- Commands such as Run, Debug, and Test only use this build script if -->
|
||||||
|
<!-- the Compile on Save feature is turned off for the project. -->
|
||||||
|
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
|
||||||
|
<!-- in the project's Project Properties dialog box.-->
|
||||||
|
<project name="BigIntegerFibonacci" default="default" basedir=".">
|
||||||
|
<description>Builds, tests, and runs the project BigIntegerFibonacci.</description>
|
||||||
|
<import file="nbproject/build-impl.xml"/>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
There exist several targets which are by default empty and which can be
|
||||||
|
used for execution of your tasks. These targets are usually executed
|
||||||
|
before and after some main targets. They are:
|
||||||
|
|
||||||
|
-pre-init: called before initialization of project properties
|
||||||
|
-post-init: called after initialization of project properties
|
||||||
|
-pre-compile: called before javac compilation
|
||||||
|
-post-compile: called after javac compilation
|
||||||
|
-pre-compile-single: called before javac compilation of single file
|
||||||
|
-post-compile-single: called after javac compilation of single file
|
||||||
|
-pre-compile-test: called before javac compilation of JUnit tests
|
||||||
|
-post-compile-test: called after javac compilation of JUnit tests
|
||||||
|
-pre-compile-test-single: called before javac compilation of single JUnit test
|
||||||
|
-post-compile-test-single: called after javac compilation of single JUunit test
|
||||||
|
-pre-jar: called before JAR building
|
||||||
|
-post-jar: called after JAR building
|
||||||
|
-post-clean: called after cleaning build products
|
||||||
|
|
||||||
|
(Targets beginning with '-' are not intended to be called on their own.)
|
||||||
|
|
||||||
|
Example of inserting an obfuscator after compilation could look like this:
|
||||||
|
|
||||||
|
<target name="-post-compile">
|
||||||
|
<obfuscate>
|
||||||
|
<fileset dir="${build.classes.dir}"/>
|
||||||
|
</obfuscate>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
For list of available properties check the imported
|
||||||
|
nbproject/build-impl.xml file.
|
||||||
|
|
||||||
|
|
||||||
|
Another way to customize the build is by overriding existing main targets.
|
||||||
|
The targets of interest are:
|
||||||
|
|
||||||
|
-init-macrodef-javac: defines macro for javac compilation
|
||||||
|
-init-macrodef-junit: defines macro for junit execution
|
||||||
|
-init-macrodef-debug: defines macro for class debugging
|
||||||
|
-init-macrodef-java: defines macro for class execution
|
||||||
|
-do-jar: JAR building
|
||||||
|
run: execution of project
|
||||||
|
-javadoc-build: Javadoc generation
|
||||||
|
test-report: JUnit report generation
|
||||||
|
|
||||||
|
An example of overriding the target for project execution could look like this:
|
||||||
|
|
||||||
|
<target name="run" depends="BigIntegerFibonacci-impl.jar">
|
||||||
|
<exec dir="bin" executable="launcher.exe">
|
||||||
|
<arg file="${dist.jar}"/>
|
||||||
|
</exec>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
Notice that the overridden target depends on the jar target and not only on
|
||||||
|
the compile target as the regular run target does. Again, for a list of available
|
||||||
|
properties which you can use, check the target you are overriding in the
|
||||||
|
nbproject/build-impl.xml file.
|
||||||
|
|
||||||
|
-->
|
||||||
|
</project>
|
BIN
BigIntegerFibonacci/dist/BigIntegerFibonacci.jar
vendored
Normal file
BIN
BigIntegerFibonacci/dist/BigIntegerFibonacci.jar
vendored
Normal file
Binary file not shown.
32
BigIntegerFibonacci/dist/README.TXT
vendored
Normal file
32
BigIntegerFibonacci/dist/README.TXT
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
========================
|
||||||
|
BUILD OUTPUT DESCRIPTION
|
||||||
|
========================
|
||||||
|
|
||||||
|
When you build an Java application project that has a main class, the IDE
|
||||||
|
automatically copies all of the JAR
|
||||||
|
files on the projects classpath to your projects dist/lib folder. The IDE
|
||||||
|
also adds each of the JAR files to the Class-Path element in the application
|
||||||
|
JAR files manifest file (MANIFEST.MF).
|
||||||
|
|
||||||
|
To run the project from the command line, go to the dist folder and
|
||||||
|
type the following:
|
||||||
|
|
||||||
|
java -jar "BigIntegerFibonacci.jar"
|
||||||
|
|
||||||
|
To distribute this project, zip up the dist folder (including the lib folder)
|
||||||
|
and distribute the ZIP file.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
* If two JAR files on the project classpath have the same name, only the first
|
||||||
|
JAR file is copied to the lib folder.
|
||||||
|
* Only JAR files are copied to the lib folder.
|
||||||
|
If the classpath contains other types of files or folders, these files (folders)
|
||||||
|
are not copied.
|
||||||
|
* If a library on the projects classpath also has a Class-Path element
|
||||||
|
specified in the manifest,the content of the Class-Path element has to be on
|
||||||
|
the projects runtime path.
|
||||||
|
* To set a main class in a standard Java project, right-click the project node
|
||||||
|
in the Projects window and choose Properties. Then click Run and enter the
|
||||||
|
class name in the Main Class field. Alternatively, you can manually type the
|
||||||
|
class name in the manifest Main-Class element.
|
3
BigIntegerFibonacci/manifest.mf
Normal file
3
BigIntegerFibonacci/manifest.mf
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
X-COMMENT: Main-Class will be added automatically by build
|
||||||
|
|
1799
BigIntegerFibonacci/nbproject/build-impl.xml
Normal file
1799
BigIntegerFibonacci/nbproject/build-impl.xml
Normal file
File diff suppressed because it is too large
Load Diff
8
BigIntegerFibonacci/nbproject/genfiles.properties
Normal file
8
BigIntegerFibonacci/nbproject/genfiles.properties
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
build.xml.data.CRC32=8269c52e
|
||||||
|
build.xml.script.CRC32=12feaf5a
|
||||||
|
build.xml.stylesheet.CRC32=f85dc8f2@1.107.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=8269c52e
|
||||||
|
nbproject/build-impl.xml.script.CRC32=f775d689
|
||||||
|
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.107.0.48
|
97
BigIntegerFibonacci/nbproject/project.properties
Normal file
97
BigIntegerFibonacci/nbproject/project.properties
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
annotation.processing.enabled=true
|
||||||
|
annotation.processing.enabled.in.editor=false
|
||||||
|
annotation.processing.processors.list=
|
||||||
|
annotation.processing.run.all.processors=true
|
||||||
|
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
|
||||||
|
application.title=BigIntegerFibonacci
|
||||||
|
application.vendor=caleb
|
||||||
|
build.classes.dir=${build.dir}/classes
|
||||||
|
build.classes.excludes=**/*.java,**/*.form
|
||||||
|
# This directory is removed when the project is cleaned:
|
||||||
|
build.dir=build
|
||||||
|
build.generated.dir=${build.dir}/generated
|
||||||
|
build.generated.sources.dir=${build.dir}/generated-sources
|
||||||
|
# Only compile against the classpath explicitly listed here:
|
||||||
|
build.sysclasspath=ignore
|
||||||
|
build.test.classes.dir=${build.dir}/test/classes
|
||||||
|
build.test.results.dir=${build.dir}/test/results
|
||||||
|
# Uncomment to specify the preferred debugger connection transport:
|
||||||
|
#debug.transport=dt_socket
|
||||||
|
debug.classpath=\
|
||||||
|
${run.classpath}
|
||||||
|
debug.modulepath=\
|
||||||
|
${run.modulepath}
|
||||||
|
debug.test.classpath=\
|
||||||
|
${run.test.classpath}
|
||||||
|
debug.test.modulepath=\
|
||||||
|
${run.test.modulepath}
|
||||||
|
# Files in build.classes.dir which should be excluded from distribution jar
|
||||||
|
dist.archive.excludes=
|
||||||
|
# This directory is removed when the project is cleaned:
|
||||||
|
dist.dir=dist
|
||||||
|
dist.jar=${dist.dir}/BigIntegerFibonacci.jar
|
||||||
|
dist.javadoc.dir=${dist.dir}/javadoc
|
||||||
|
dist.jlink.dir=${dist.dir}/jlink
|
||||||
|
dist.jlink.output=${dist.jlink.dir}/BigIntegerFibonacci
|
||||||
|
endorsed.classpath=
|
||||||
|
excludes=
|
||||||
|
includes=**
|
||||||
|
jar.compress=false
|
||||||
|
javac.classpath=
|
||||||
|
# Space-separated list of extra javac options
|
||||||
|
javac.compilerargs=
|
||||||
|
javac.deprecation=false
|
||||||
|
javac.external.vm=true
|
||||||
|
javac.modulepath=
|
||||||
|
javac.processormodulepath=
|
||||||
|
javac.processorpath=\
|
||||||
|
${javac.classpath}
|
||||||
|
javac.source=20
|
||||||
|
javac.target=20
|
||||||
|
javac.test.classpath=\
|
||||||
|
${javac.classpath}:\
|
||||||
|
${build.classes.dir}
|
||||||
|
javac.test.modulepath=\
|
||||||
|
${javac.modulepath}
|
||||||
|
javac.test.processorpath=\
|
||||||
|
${javac.test.classpath}
|
||||||
|
javadoc.additionalparam=
|
||||||
|
javadoc.author=false
|
||||||
|
javadoc.encoding=${source.encoding}
|
||||||
|
javadoc.html5=false
|
||||||
|
javadoc.noindex=false
|
||||||
|
javadoc.nonavbar=false
|
||||||
|
javadoc.notree=false
|
||||||
|
javadoc.private=false
|
||||||
|
javadoc.splitindex=true
|
||||||
|
javadoc.use=true
|
||||||
|
javadoc.version=false
|
||||||
|
javadoc.windowtitle=
|
||||||
|
# The jlink additional root modules to resolve
|
||||||
|
jlink.additionalmodules=
|
||||||
|
# The jlink additional command line parameters
|
||||||
|
jlink.additionalparam=
|
||||||
|
jlink.launcher=true
|
||||||
|
jlink.launcher.name=BigIntegerFibonacci
|
||||||
|
main.class=bigintegerfibonacci.BigIntegerFibonacci
|
||||||
|
manifest.file=manifest.mf
|
||||||
|
meta.inf.dir=${src.dir}/META-INF
|
||||||
|
mkdist.disabled=false
|
||||||
|
platform.active=Graal_JDK_20
|
||||||
|
run.classpath=\
|
||||||
|
${javac.classpath}:\
|
||||||
|
${build.classes.dir}
|
||||||
|
# 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=-Dfile.encoding=UTF-8 -Xms512m -Xmx32G -Xss128M
|
||||||
|
run.modulepath=\
|
||||||
|
${javac.modulepath}
|
||||||
|
run.test.classpath=\
|
||||||
|
${javac.test.classpath}:\
|
||||||
|
${build.test.classes.dir}
|
||||||
|
run.test.modulepath=\
|
||||||
|
${javac.test.modulepath}
|
||||||
|
source.encoding=UTF-8
|
||||||
|
src.dir=src
|
||||||
|
test.src.dir=test
|
16
BigIntegerFibonacci/nbproject/project.xml
Normal file
16
BigIntegerFibonacci/nbproject/project.xml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||||
|
<type>org.netbeans.modules.java.j2seproject</type>
|
||||||
|
<configuration>
|
||||||
|
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
|
||||||
|
<name>BigIntegerFibonacci</name>
|
||||||
|
<explicit-platform explicit-source-supported="true"/>
|
||||||
|
<source-roots>
|
||||||
|
<root id="src.dir"/>
|
||||||
|
</source-roots>
|
||||||
|
<test-roots>
|
||||||
|
<root id="test.src.dir"/>
|
||||||
|
</test-roots>
|
||||||
|
</data>
|
||||||
|
</configuration>
|
||||||
|
</project>
|
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
||||||
|
*/
|
||||||
|
package bigintegerfibonacci;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
public class BigIntegerFibonacci {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args the command line arguments
|
||||||
|
*/
|
||||||
|
private static BigInteger[] fibonacciCache;
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n = 100000;
|
||||||
|
fibonacciCache = new BigInteger[n + 1];
|
||||||
|
System.out.println("Calculating fibonacci for number " + n + "...");
|
||||||
|
// Precache value to prevent stack overflow
|
||||||
|
for (int i = 0; i <= n; ++i) {
|
||||||
|
fibonacci(i);
|
||||||
|
}
|
||||||
|
System.out.println(fibonacci(n));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BigInteger fibonacci(int n) {
|
||||||
|
// Base case
|
||||||
|
int compareInt = BigInteger.valueOf(n).compareTo(BigInteger.ONE);
|
||||||
|
//System.out.println("value is: " + compareInt);
|
||||||
|
|
||||||
|
if (compareInt == -1 || compareInt == 0){
|
||||||
|
return BigInteger.valueOf(n);
|
||||||
|
}
|
||||||
|
if (fibonacciCache[n] != null) {
|
||||||
|
return fibonacciCache[n];
|
||||||
|
}
|
||||||
|
BigInteger nthFibNumber = (fibonacci(n - 1).add(fibonacci(n - 2)));
|
||||||
|
fibonacciCache[n] = nthFibNumber;
|
||||||
|
return fibonacciCache[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
BigIntegerFibonacci/src/bigintegerfibonacci/StackSize.java
Normal file
35
BigIntegerFibonacci/src/bigintegerfibonacci/StackSize.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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 bigintegerfibonacci;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
public class StackSize {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
//System.out.println(fact(1 << 15));
|
||||||
|
System.out.println(infinateRecursion(0));
|
||||||
|
}
|
||||||
|
catch (StackOverflowError e) {
|
||||||
|
System.err.println("true recursion level was " + level);
|
||||||
|
System.err.println("reported recursion level was " +
|
||||||
|
e.getStackTrace().length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int level = 0;
|
||||||
|
public static long fact(int n) {
|
||||||
|
level++;
|
||||||
|
return n < 2 ? n : n * fact(n - 1);
|
||||||
|
}
|
||||||
|
public static long infinateRecursion(long n) {
|
||||||
|
level++;
|
||||||
|
return infinateRecursion(n++);
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
package mp3_recursion_calebfontenot;
|
||||||
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
@ -26,32 +25,29 @@ import javafx.stage.Stage;
|
|||||||
*
|
*
|
||||||
* @author caleb
|
* @author caleb
|
||||||
*/
|
*/
|
||||||
public class Maze extends Application
|
public class Maze extends Application {
|
||||||
{
|
|
||||||
|
|
||||||
double paneWidth = 440;
|
double paneWidth = 600;
|
||||||
double paneHeight = 440;
|
double paneHeight = 600;
|
||||||
|
|
||||||
private Cell[][] board = new Cell[8][8];
|
private Cell[][] board = new Cell[8][8];
|
||||||
private Button btFindPath = new Button("Find Path Top Left to Bottom Right");
|
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 Button btClearPath = new Button("Clear Path");
|
||||||
private Label lblStatus = new Label();
|
private Label lblStatus = new Label();
|
||||||
|
|
||||||
@Override // Override the start method in the Application class
|
@Override // Override the start method in the Application class
|
||||||
public void start(Stage primaryStage)
|
public void start(Stage primaryStage) {
|
||||||
{
|
|
||||||
GridPane gridPane = new GridPane();
|
GridPane gridPane = new GridPane();
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++) {
|
||||||
{
|
for (int j = 0; j < 8; j++) {
|
||||||
for (int j = 0; j < 8; j++)
|
|
||||||
{
|
|
||||||
gridPane.add(board[i][j] = new Cell(), j, i);
|
gridPane.add(board[i][j] = new Cell(), j, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HBox hBox = new HBox(5);
|
HBox hBox = new HBox(5);
|
||||||
hBox.setAlignment(Pos.CENTER);
|
hBox.setAlignment(Pos.CENTER);
|
||||||
hBox.getChildren().addAll(btFindPath, btClearPath);
|
hBox.getChildren().addAll(btFindPath, btFindPath2, btClearPath);
|
||||||
|
|
||||||
BorderPane pane = new BorderPane();
|
BorderPane pane = new BorderPane();
|
||||||
pane.setTop(lblStatus);
|
pane.setTop(lblStatus);
|
||||||
@ -66,175 +62,217 @@ public class Maze extends Application
|
|||||||
primaryStage.show(); // Display the stage
|
primaryStage.show(); // Display the stage
|
||||||
|
|
||||||
btFindPath.setOnAction(e -> findPath());
|
btFindPath.setOnAction(e -> findPath());
|
||||||
|
btFindPath2.setOnAction(e -> findPath2());
|
||||||
btClearPath.setOnAction(e -> clearPath());
|
btClearPath.setOnAction(e -> clearPath());
|
||||||
showSampleMessage();
|
showSampleMessage();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showSampleMessage()
|
public void showSampleMessage() {
|
||||||
{
|
|
||||||
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
|
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
|
||||||
|
|
||||||
a.setTitle("Maze Information");
|
a.setTitle("Maze Information");
|
||||||
a.setHeaderText(" Put this message in its proper place. ");
|
a.setHeaderText(" Put this message in its proper place. ");
|
||||||
a.setContentText("This square cannot have an X");
|
a.setContentText("This square cannot have an X");
|
||||||
Optional<ButtonType> result = a.showAndWait();
|
Optional<ButtonType> result = a.showAndWait();
|
||||||
if (result.get() == ButtonType.OK)
|
if (result.get() == ButtonType.OK) {
|
||||||
{
|
|
||||||
System.out.println("OK ");
|
System.out.println("OK ");
|
||||||
}
|
} else if (result.get() == ButtonType.CANCEL) {
|
||||||
else if (result.get() == ButtonType.CANCEL)
|
|
||||||
{
|
|
||||||
System.out.println("CANCEL");
|
System.out.println("CANCEL");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void clearCells() {
|
||||||
public void findPath()
|
for (int i = 0; i < 8; ++i) {
|
||||||
{
|
for (int j = 0; j < 8; j++) {
|
||||||
if (findPath(0, 0))
|
board[i][j].resetCell();
|
||||||
{
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findPath() {
|
||||||
|
clearCells();
|
||||||
|
if (findPath(0, 0)) {
|
||||||
lblStatus.setText("path found");
|
lblStatus.setText("path found");
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
lblStatus.setText("No path exists");
|
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();
|
board[row][col].visit();
|
||||||
|
|
||||||
if ((col == 7) && (row == 7))
|
if ((col == 7) && (row == 7)) {
|
||||||
{
|
|
||||||
board[row][col].selectCell();
|
board[row][col].selectCell();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((row > 0) && !board[row - 1][col].marked()
|
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);
|
block(row, col);
|
||||||
|
|
||||||
if (findPath(row - 1, col))
|
if (findPath(row - 1, col)) {
|
||||||
{
|
|
||||||
board[row][col].selectCell();
|
board[row][col].selectCell();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unblock(row, col);
|
unblock(row, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((row < 7) && !board[row + 1][col].marked()
|
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);
|
block(row, col);
|
||||||
|
|
||||||
if (findPath(row + 1, col))
|
if (findPath(row + 1, col)) {
|
||||||
{
|
|
||||||
board[row][col].selectCell();
|
board[row][col].selectCell();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unblock(row, col);
|
unblock(row, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((col > 0) && !board[row][col - 1].marked()
|
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);
|
block(row, col);
|
||||||
if (findPath(row, col - 1))
|
if (findPath(row, col - 1)) {
|
||||||
{
|
|
||||||
board[row][col].selectCell();
|
board[row][col].selectCell();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unblock(row, col);
|
unblock(row, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((col < 7) && !board[row][col + 1].marked()
|
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);
|
block(row, col);
|
||||||
if (findPath(row, col + 1))
|
if (findPath(row, col + 1)) {
|
||||||
{
|
|
||||||
board[row][col].selectCell();
|
board[row][col].selectCell();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unblock(row, col);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Temporary block the neighbor to prevent neighboring path
|
// Temporary block the neighbor to prevent neighboring path
|
||||||
public void block(int row, int col)
|
public void block(int row, int col) {
|
||||||
{
|
if (row > 0) {
|
||||||
if (row > 0)
|
|
||||||
{
|
|
||||||
board[row - 1][col].block();
|
board[row - 1][col].block();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row < 7)
|
if (row < 7) {
|
||||||
{
|
|
||||||
board[row + 1][col].block();
|
board[row + 1][col].block();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (col > 0)
|
if (col > 0) {
|
||||||
{
|
|
||||||
board[row][col - 1].block();
|
board[row][col - 1].block();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (col < 7)
|
if (col < 7) {
|
||||||
{
|
|
||||||
board[row][col + 1].block();
|
board[row][col + 1].block();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the temporary block
|
// Remove the temporary block
|
||||||
public void unblock(int row, int col)
|
public void unblock(int row, int col) {
|
||||||
{
|
if (row > 0) {
|
||||||
if (row > 0)
|
|
||||||
{
|
|
||||||
board[row - 1][col].unblock();
|
board[row - 1][col].unblock();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row < 7)
|
if (row < 7) {
|
||||||
{
|
|
||||||
board[row + 1][col].unblock();
|
board[row + 1][col].unblock();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (col > 0)
|
if (col > 0) {
|
||||||
{
|
|
||||||
board[row][col - 1].unblock();
|
board[row][col - 1].unblock();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (col < 7)
|
if (col < 7) {
|
||||||
{
|
|
||||||
board[row][col + 1].unblock();
|
board[row][col + 1].unblock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearPath()
|
public void clearPath() {
|
||||||
{
|
for (int row = 0; row < board.length; row++) {
|
||||||
for (int row = 0; row < board.length; row++)
|
for (int col = 0; col < board[row].length; col++) {
|
||||||
{
|
|
||||||
for (int col = 0; col < board[row].length; col++)
|
|
||||||
{
|
|
||||||
board[row][col].deselectCell();
|
board[row][col].deselectCell();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
launch(args);
|
launch(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inner class
|
// Inner class
|
||||||
class Cell extends StackPane
|
class Cell extends StackPane {
|
||||||
{
|
|
||||||
|
|
||||||
private boolean marked = false;
|
private boolean marked = false;
|
||||||
private boolean visited = false;
|
private boolean visited = false;
|
||||||
@ -247,79 +285,83 @@ public class Maze extends Application
|
|||||||
Line line1 = new Line(0, 0, width, height);
|
Line line1 = new Line(0, 0, width, height);
|
||||||
Line line2 = new Line(width, 0, 0, height);
|
Line line2 = new Line(width, 0, 0, height);
|
||||||
|
|
||||||
public Cell()
|
public Cell() {
|
||||||
{
|
|
||||||
this.getChildren().add(rectangle);
|
this.getChildren().add(rectangle);
|
||||||
rectangle.setFill(Color.WHITE);
|
rectangle.setFill(Color.WHITE);
|
||||||
rectangle.setStroke(Color.BLACK);
|
rectangle.setStroke(Color.BLACK);
|
||||||
|
|
||||||
this.setOnMousePressed(e ->
|
this.setOnMousePressed(e
|
||||||
{
|
-> {
|
||||||
marked = !marked;
|
marked = !marked;
|
||||||
if (marked)
|
if (marked) {
|
||||||
{
|
|
||||||
mark();
|
mark();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
unmark();
|
unmark();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void mark()
|
public void mark() {
|
||||||
{
|
|
||||||
this.getChildren().addAll(line1, line2);
|
this.getChildren().addAll(line1, line2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unmark()
|
public void unmark() {
|
||||||
{
|
|
||||||
this.getChildren().remove(line1);
|
this.getChildren().remove(line1);
|
||||||
this.getChildren().remove(line2);
|
this.getChildren().remove(line2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean marked()
|
public boolean marked() {
|
||||||
{
|
|
||||||
return marked;
|
return marked;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit()
|
public void visit() {
|
||||||
{
|
|
||||||
visited = true;
|
visited = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean visited()
|
public boolean visited() {
|
||||||
{
|
|
||||||
return visited;
|
return visited;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean blocked()
|
public boolean blocked() {
|
||||||
{
|
|
||||||
return blocked;
|
return blocked;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void block()
|
public void block() {
|
||||||
{
|
|
||||||
blocked = true;
|
blocked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unblock()
|
public void unblock() {
|
||||||
{
|
|
||||||
blocked = false;
|
blocked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void selectCell()
|
public void selectCell() {
|
||||||
{
|
if (rectangle.getFill().equals(Color.GREEN)) {
|
||||||
rectangle.setFill(Color.RED);
|
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);
|
rectangle.setFill(Color.WHITE);
|
||||||
blocked = false;
|
blocked = false;
|
||||||
visited = 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) {
|
public static void displayPermutation(String s) {
|
||||||
displayPermutation(" ", s);
|
displayPermutation("", s);
|
||||||
}
|
}
|
||||||
public static void displayPermutation(String s1, String s2) {
|
public static void displayPermutation(String s1, String s2) {
|
||||||
if (s2.equals("")) {
|
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(maxSafe("abc", "ABC"));
|
||||||
System.out.println();
|
System.out.println();
|
||||||
//System.out.println(maxSafe(1, "two"));
|
//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"));
|
System.out.println("This line compiles but crashes the program " + max(1, "two"));
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
System.err.println("RAW TYPES ARE UNSAFE " + e.getMessage()) ;
|
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);
|
||||||
|
}
|
||||||
|
}
|
BIN
Semester 3/ZIPs/MP3_Recursion_CalebFontenot.zip
Normal file
BIN
Semester 3/ZIPs/MP3_Recursion_CalebFontenot.zip
Normal file
Binary file not shown.
BIN
Semester 3/ZIPs/lab6_generics_CalebFontenot.zip
Normal file
BIN
Semester 3/ZIPs/lab6_generics_CalebFontenot.zip
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user