that's it for today
This commit is contained in:
parent
b2232e052e
commit
d709a920cf
14
Semester 3/Assignments/RecursionDemo/pom.xml
Normal file
14
Semester 3/Assignments/RecursionDemo/pom.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>edu.slcc.asdv.caleb</groupId>
|
||||
<artifactId>RecursionDemo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<exec.mainClass>edu.slcc.asdv.caleb.recursiondemo.RecursionDemo</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.recursiondemo;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class RecursionDemo {
|
||||
|
||||
static void printNTimes(int nTimes, String message)
|
||||
{
|
||||
for (int i = 0; i < nTimes; ++i) {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPalendrome(String s)
|
||||
{
|
||||
String backwards = "";
|
||||
for (int i = s.length() - 1; i >= 0; i--) {
|
||||
backwards += s.charAt(i);
|
||||
}
|
||||
return s.equals(backwards);
|
||||
}
|
||||
|
||||
public static long fib(int n)
|
||||
{
|
||||
if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
}
|
||||
System.out.println(n);
|
||||
return fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
|
||||
static void printNTimesRecursion(int nTimes, String message)
|
||||
{
|
||||
if (nTimes == 0) {
|
||||
return;
|
||||
}
|
||||
System.out.println(message);
|
||||
printNTimesRecursion(--nTimes, message);
|
||||
}
|
||||
|
||||
public static boolean isPalendromeRecursion(String s) {
|
||||
if (s.length() == 0 || s.length() == 1) {
|
||||
return true;
|
||||
} else if (s.charAt(0) != s.charAt(s.length() - 1)) {
|
||||
return false;
|
||||
}
|
||||
return isPalendromeRecursion(s.substring(1, s.length() - 1));
|
||||
}
|
||||
|
||||
public static long factorial(int n)
|
||||
{
|
||||
long fact;
|
||||
if (n == 1) {
|
||||
return 1;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,3 @@
|
||||
artifactId=RecursionDemo
|
||||
groupId=edu.slcc.asdv.caleb
|
||||
version=1.0-SNAPSHOT
|
@ -0,0 +1 @@
|
||||
edu/slcc/asdv/caleb/recursiondemo/RecursionDemo.class
|
@ -0,0 +1 @@
|
||||
/home/caleb/ASDV-WebDev/Semester 2/RecursionDemo/src/main/java/edu/slcc/asdv/caleb/recursiondemo/RecursionDemo.java
|
Loading…
Reference in New Issue
Block a user