push changes
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>RecursionDemo.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: #808080; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST5 {color: #9876aa}
|
||||
.ST6 {color: #8a653b}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST3 {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/RecursionDemo/src/main/java/edu/slcc/asdv/caleb/recursiondemo/RecursionDemo.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"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.recursiondemo;
|
||||
|
||||
<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> RecursionDemo {
|
||||
|
||||
<span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printNTimes</span>(<span class="literal">int</span> nTimes, String message) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < nTimes; ++i) {
|
||||
System.<span class="ST2">out</span>.println(message);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST3">isPalendrome</span>(String s) {
|
||||
String backwards = <span class="string">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = s.length() - <span class="number">1</span>; i >= <span class="number">0</span>; i--) {
|
||||
backwards += s.charAt(i);
|
||||
}
|
||||
<span class="literal">return</span> s.equals(backwards);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">long</span> <span class="ST3">fib</span>(<span class="literal">int</span> n) {
|
||||
<span class="literal">if</span> (n == <span class="number">1</span> || n == <span class="number">2</span>) {
|
||||
<span class="literal">return</span> <span class="number">1</span>;
|
||||
}
|
||||
System.<span class="ST2">out</span>.println(n);
|
||||
<span class="literal">return</span> <span class="ST4">fib</span>(n - <span class="number">1</span>) + <span class="ST4">fib</span>(n - <span class="number">2</span>);
|
||||
}
|
||||
|
||||
<span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printNTimesRecursion</span>(<span class="literal">int</span> nTimes, String message) {
|
||||
<span class="literal">if</span> (nTimes == <span class="number">0</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
System.<span class="ST2">out</span>.println(message);
|
||||
<span class="ST4">printNTimesRecursion</span>(--nTimes, message);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST3">isPalendromeRecursion</span>(String s) {
|
||||
<span class="literal">if</span> (s.length() == <span class="number">0</span> || s.length() == <span class="number">1</span>) {
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (s.charAt(<span class="number">0</span>) != s.charAt(s.length() - <span class="number">1</span>)) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
<span class="literal">return</span> <span class="ST4">isPalendromeRecursion</span>(s.substring(<span class="number">1</span>, s.length() - <span class="number">1</span>));
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">long</span> <span class="ST3">factorial</span>(<span class="literal">int</span> n) {
|
||||
<span class="literal">long</span> fact;
|
||||
<span class="literal">if</span> (n == <span class="number">1</span>) {
|
||||
<span class="literal">return</span> <span class="number">1</span>;
|
||||
}
|
||||
fact = n * <span class="ST4">factorial</span>(n - <span class="number">1</span>);
|
||||
<span class="literal">return</span> fact;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">printArrayRecursively</span>(<span class="literal">int</span>[] arr, <span class="literal">int</span> index) {
|
||||
<span class="literal">if</span> (index == arr.<span class="ST5">length</span>) {
|
||||
<span class="literal">return</span>;
|
||||
} <span class="literal">else</span> {
|
||||
System.<span class="ST2">out</span>.print(arr[index] + <span class="string">"</span> <span class="string">"</span>);
|
||||
<span class="ST4">printArrayRecursively</span>(arr, index + <span class="number">1</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST6">arr</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">searched</span>
|
||||
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST6">index</span> <span class="comment">the</span> <span class="comment">index</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">to</span> <span class="comment">manipulate</span>
|
||||
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST6">currentMax</span> <span class="comment">the</span> <span class="comment">current</span> <span class="comment">maximum</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">array</span>
|
||||
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">maximum</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">array</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">findMaximumRecursive</span>(<span class="literal">int</span> arr[], <span class="literal">int</span> index, <span class="literal">int</span> currentMax) <span class="literal">throws</span> Exception {
|
||||
<span class="comment">//System.out.println("at index " + index);</span>
|
||||
<span class="literal">if</span> (index >= (arr.<span class="ST5">length</span>)) {
|
||||
<span class="comment">//throw new ArrayIndexOutOfBoundsException();</span>
|
||||
<span class="literal">return</span> currentMax;
|
||||
}
|
||||
<span class="literal">if</span> (arr[index] >= currentMax) {
|
||||
currentMax = arr[index];
|
||||
}
|
||||
currentMax = <span class="ST4">findMaximumRecursive</span>(arr, (index + <span class="number">1</span>), currentMax);
|
||||
<span class="literal">return</span> currentMax;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">findMaximumRecursive</span>(<span class="literal">int</span> arr[]) <span class="literal">throws</span> Exception {
|
||||
<span class="comment">// This method simply makes it possible to call findMaximumRecursive() without giving it currentMax or an index</span>
|
||||
<span class="literal">return</span> <span class="ST4">findMaximumRecursive</span>(arr, <span class="number">0</span>, Integer.<span class="ST2">MIN_VALUE</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) <span class="literal">throws</span> Exception {
|
||||
<span class="comment">//System.out.println(factorial(20));</span>
|
||||
<span class="comment">//printNTimesRecursion(1, "Hello Recursion");</span>
|
||||
<span class="comment">//System.out.println(fib(10));</span>
|
||||
<span class="comment">//System.out.println(isPalendrome("detartrated"));</span>
|
||||
<span class="comment">//System.out.println(isPalendromeRecursion("detartrated"));</span>
|
||||
<span class="comment">//int[] arr = {2, 3, 5, 6, 7, 8, 10, 32, 64, 128};</span>
|
||||
<span class="comment">//printArrayRecursively(arr, 0);</span>
|
||||
System.<span class="ST2">out</span>.println(<span class="ST4">f</span><span class="ST4">indMaximumRecursive</span>(<span class="literal">new</span> <span class="literal">int</span>[]{<span class="number">99</span>, <span class="number">1</span>, <span class="number">44</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">55</span>}));
|
||||
System.<span class="ST2">out</span>.println(<span class="ST4">f</span><span class="ST4">indMaximumRecursive</span>(<span class="literal">new</span> <span class="literal">int</span>[]{<span class="number">1</span>, <span class="number">44</span>, <span class="number">2</span>, <span class="number">3</span>}));
|
||||
System.<span class="ST2">out</span>.println(<span class="ST4">f</span><span class="ST4">indMaximumRecursive</span>(<span class="literal">new</span> <span class="literal">int</span>[]{<span class="number">1</span>, <span class="number">44</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">300</span>}));
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
18
Semester 3/Assignments/RecursionDemo/nb-configuration.xml
Normal file
18
Semester 3/Assignments/RecursionDemo/nb-configuration.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-shared-configuration>
|
||||
<!--
|
||||
This file contains additional configuration written by modules in the NetBeans IDE.
|
||||
The configuration is intended to be shared among all the users of project and
|
||||
therefore it is assumed to be part of version control checkout.
|
||||
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
|
||||
-->
|
||||
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
|
||||
<!--
|
||||
Properties that influence various parts of the IDE, especially code formatting and the like.
|
||||
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
|
||||
That way multiple projects can share the same settings (useful for formatting rules for example).
|
||||
Any value defined here will override the pom.xml file value but is only applicable to the current project.
|
||||
-->
|
||||
<netbeans.hint.jdkPlatform>JDK_20</netbeans.hint.jdkPlatform>
|
||||
</properties>
|
||||
</project-shared-configuration>
|
@@ -9,15 +9,13 @@ package edu.slcc.asdv.caleb.recursiondemo;
|
||||
*/
|
||||
public class RecursionDemo {
|
||||
|
||||
static void printNTimes(int nTimes, String message)
|
||||
{
|
||||
static void printNTimes(int nTimes, String message) {
|
||||
for (int i = 0; i < nTimes; ++i) {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPalendrome(String s)
|
||||
{
|
||||
public static boolean isPalendrome(String s) {
|
||||
String backwards = "";
|
||||
for (int i = s.length() - 1; i >= 0; i--) {
|
||||
backwards += s.charAt(i);
|
||||
@@ -25,8 +23,7 @@ public class RecursionDemo {
|
||||
return s.equals(backwards);
|
||||
}
|
||||
|
||||
public static long fib(int n)
|
||||
{
|
||||
public static long fib(int n) {
|
||||
if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
}
|
||||
@@ -34,8 +31,7 @@ public class RecursionDemo {
|
||||
return fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
|
||||
static void printNTimesRecursion(int nTimes, String message)
|
||||
{
|
||||
static void printNTimesRecursion(int nTimes, String message) {
|
||||
if (nTimes == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -51,9 +47,8 @@ public class RecursionDemo {
|
||||
}
|
||||
return isPalendromeRecursion(s.substring(1, s.length() - 1));
|
||||
}
|
||||
|
||||
public static long factorial(int n)
|
||||
{
|
||||
|
||||
public static long factorial(int n) {
|
||||
long fact;
|
||||
if (n == 1) {
|
||||
return 1;
|
||||
@@ -61,24 +56,51 @@ public class RecursionDemo {
|
||||
fact = n * factorial(n - 1);
|
||||
return fact;
|
||||
}
|
||||
|
||||
|
||||
public static void printArrayRecursively(int[] arr, int index) {
|
||||
if (index == arr.length) {
|
||||
return;
|
||||
} else {
|
||||
System.out.print(arr[index] + " ");
|
||||
printArrayRecursively(arr, index+1);
|
||||
printArrayRecursively(arr, index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println(factorial(20));
|
||||
printNTimesRecursion(1, "Hello Recursion");
|
||||
/**
|
||||
*
|
||||
* @param arr the array to be searched
|
||||
* @param index the index of the array to manipulate
|
||||
* @param currentMax the current maximum of the array
|
||||
* @return the maximum of the array
|
||||
*/
|
||||
public static int findMaximumRecursive(int arr[], int index, int currentMax) throws Exception {
|
||||
//System.out.println("at index " + index);
|
||||
if (index >= (arr.length)) {
|
||||
//throw new ArrayIndexOutOfBoundsException();
|
||||
return currentMax;
|
||||
}
|
||||
if (arr[index] >= currentMax) {
|
||||
currentMax = arr[index];
|
||||
}
|
||||
currentMax = findMaximumRecursive(arr, (index + 1), currentMax);
|
||||
return currentMax;
|
||||
}
|
||||
|
||||
public static int findMaximumRecursive(int arr[]) throws Exception {
|
||||
// This method simply makes it possible to call findMaximumRecursive() without giving it currentMax or an index
|
||||
return findMaximumRecursive(arr, 0, Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//System.out.println(factorial(20));
|
||||
//printNTimesRecursion(1, "Hello Recursion");
|
||||
//System.out.println(fib(10));
|
||||
System.out.println(isPalendrome("detartrated"));
|
||||
System.out.println(isPalendromeRecursion("detartrated"));
|
||||
int[] arr = {2, 3, 5, 6, 7, 8, 10, 32, 64, 128};
|
||||
printArrayRecursively(arr, 0);
|
||||
//System.out.println(isPalendrome("detartrated"));
|
||||
//System.out.println(isPalendromeRecursion("detartrated"));
|
||||
//int[] arr = {2, 3, 5, 6, 7, 8, 10, 32, 64, 128};
|
||||
//printArrayRecursively(arr, 0);
|
||||
System.out.println(findMaximumRecursive(new int[]{99, 1, 44, 2, 3, 55}));
|
||||
System.out.println(findMaximumRecursive(new int[]{1, 44, 2, 3}));
|
||||
System.out.println(findMaximumRecursive(new int[]{1, 44, 2, 3, 300}));
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
@@ -1 +1 @@
|
||||
/home/caleb/ASDV-WebDev/Semester 2/RecursionDemo/src/main/java/edu/slcc/asdv/caleb/recursiondemo/RecursionDemo.java
|
||||
/home/caleb/ASDV-Java/Semester 3/Assignments/RecursionDemo/src/main/java/edu/slcc/asdv/caleb/recursiondemo/RecursionDemo.java
|
||||
|
Reference in New Issue
Block a user