MP2 is pog
parent
0d37491e6d
commit
de8e3acf40
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,61 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>NestedLoopsIndexes.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST4 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/NestedLoopsIndexes.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="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> NestedLoopsIndexes {
|
||||
<span class="literal">static</span> <span class="literal">final</span> <span class="literal">int</span> <span class="ST2">ROWS</span> = <span class="number">3</span>;
|
||||
<span class="literal">static</span> <span class="literal">final</span> <span class="literal">int</span> <span class="ST2">COLUMNS</span> = <span class="number">5</span>;
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">nestedLoopsIndexesR</span>(<span class="literal">int</span> i, <span class="literal">int</span> j) {
|
||||
<span class="literal">if</span> (j == <span class="ST2">COLUMNS</span>) {
|
||||
System.<span class="ST2">out</span>.println();
|
||||
<span class="literal">return</span>;
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (i == <span class="ST2">ROWS</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
System.<span class="ST2">out</span>.print(i + <span class="string">"</span><span class="string">, </span><span class="string">"</span> + j + <span class="string">"</span> <span class="string">"</span>);
|
||||
<span class="ST4">nestedLoopsIndexesR</span>(i, ++j);
|
||||
<span class="literal">if</span> (i + <span class="number">1</span> == j) {
|
||||
<span class="ST4">nestedLoopsIndexesR</span>(++i, <span class="number">0</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="ST4">nestedLoopsIndexesR</span>(<span class="number">0</span>, <span class="number">0</span>);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,58 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>OccurancesOfChar.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/OccurancesOfChar.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.lab5.recursion2_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> OccurancesOfChar {
|
||||
<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);
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a string: </span><span class="string">"</span>);
|
||||
String s = input.nextLine();
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a character: </span><span class="string">"</span>);
|
||||
<span class="literal">char</span> ch =input.nextLine().charAt(<span class="number">0</span>);
|
||||
<span class="literal">int</span> times = count(s, ch);
|
||||
System.out.println(ch + <span class="string">"</span><span class="string"> appears </span><span class="string">"</span> + times
|
||||
+ (times > <span class="number">1</span> ? <span class="string">"</span><span class="string"> times </span><span class="string">"</span> : <span class="string">"</span><span class="string"> time </span><span class="string">"</span>) + <span class="string">"</span><span class="string">in </span><span class="string">"</span> + s);
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(String str, <span class="literal">char</span> a) {
|
||||
<span class="literal">int</span> result = <span class="number">0</span>;
|
||||
<span class="literal">if</span> (str.length() > <span class="number">0</span>) {
|
||||
result = count(str.substring(<span class="number">1</span>), a) +
|
||||
(( str.charAt(<span class="number">0</span>) == a) ? <span class="number">1</span> : <span class="number">0</span>);
|
||||
}
|
||||
<span class="literal">return</span> result;
|
||||
}
|
||||
}
|
||||
|
||||
</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>OccurencesOfSpecifiedCharacterInArray.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/OccurencesOfSpecifiedCharacterInArray.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.lab5.recursion2_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> OccurencesOfSpecifiedCharacterInArray {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
||||
{
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a string: </span><span class="string">"</span>);
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.in);
|
||||
String s = input.nextLine();
|
||||
<span class="literal">char</span>[] items = s.toCharArray();
|
||||
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a character: </span><span class="string">"</span>);
|
||||
<span class="literal">char</span> ch = input.nextLine().trim().charAt(<span class="number">0</span>);
|
||||
|
||||
System.out.println(ch + <span class="string">"</span><span class="string"> appears </span><span class="string">"</span> + count(items, ch) + <span class="string">"</span><span class="string"> times.</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(<span class="literal">char</span>[] chars, <span class="literal">char</span> ch) {
|
||||
<span class="literal">return</span> count(chars, chars.length - <span class="number">1</span>, ch);
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(<span class="literal">char</span>[] chars, <span class="literal">int</span> high, <span class="literal">char</span> ch) {
|
||||
<span class="literal">if</span> (high >= <span class="number">0</span>) {
|
||||
<span class="literal">return</span> count(chars, high - <span class="number">1</span>, ch) + (ch == chars[high] ? <span class="number">1</span> : <span class="number">0</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> <span class="number">0</span>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,55 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>ReverseInt.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/ReverseInt.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.lab5.recursion2_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> ReverseInt {
|
||||
<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);
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter an integer: </span><span class="string">"</span>);
|
||||
<span class="literal">int</span> i = input.nextInt();
|
||||
System.out.print(<span class="string">"</span><span class="string">The reversal of </span><span class="string">"</span> + i + <span class="string">"</span><span class="string"> is </span><span class="string">"</span>);
|
||||
reverseDisplay(i);
|
||||
System.out.println();
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> reverseDisplay(<span class="literal">int</span> value) {
|
||||
<span class="literal">if</span> (value != <span class="number">0</span>) {
|
||||
System.out.print(value % <span class="number">10</span>);
|
||||
value = value / <span class="number">10</span>;
|
||||
reverseDisplay(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.ST4 {color: #9876aa}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST5 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST3 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_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="ST1">@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="ST2">selectionSortR</span>(<span class="literal">int</span>[] arr)
|
||||
{
|
||||
<span class="ST3">selectionSortR</span>(arr, <span class="number">0</span>, arr.<span class="ST4">length</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">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="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">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="ST3">swap</span>(arr, min, i);
|
||||
<span class="literal">if</span> ( i + <span class="number">1</span> < n) {
|
||||
<span class="ST3">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="ST2">main</span>(String[] args)
|
||||
{
|
||||
<span class="literal">int</span>[] arr = {
|
||||
<span class="number">8</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">4</span>, -<span class="number">1</span>, <span class="number">50</span>, <span class="number">49</span>
|
||||
};
|
||||
<span class="ST3">selectionSortR</span>(arr);
|
||||
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < arr.<span class="ST4">length</span>; ++i) {
|
||||
System.<span class="ST5">out</span>.print(arr[i] + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
System.<span class="ST5">out</span>.println();
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,62 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>SumOfDigits.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/SumOfDigits.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.lab5.recursion2_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> SumOfDigits {
|
||||
|
||||
<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);
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter an integer: </span><span class="string">"</span>);
|
||||
<span class="literal">int</span> i = input.nextInt();
|
||||
System.out.print(<span class="string">"</span><span class="string">The sum of digits in </span><span class="string">"</span> + i
|
||||
+ <span class="string">"</span><span class="string"> is </span><span class="string">"</span> + sumDigits(i));
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">long</span> sumDigits(<span class="literal">long</span> n)
|
||||
{
|
||||
<span class="comment">// Base case: if the number is a single digit, return it</span>
|
||||
<span class="literal">if</span> (n < <span class="number">10</span>) {
|
||||
<span class="literal">return</span> n;
|
||||
} <span class="literal">else</span> {
|
||||
<span class="comment">// Recursive case: sum the last digit and call the function on the remaining digits</span>
|
||||
<span class="literal">long</span> lastDigit = n % <span class="number">10</span>;
|
||||
<span class="literal">long</span> remainingDigits = n / <span class="number">10</span>;
|
||||
<span class="literal">return</span> lastDigit + sumDigits(remainingDigits);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,51 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>SumSeries1.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/SumSeries1.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.lab5.recursion2_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> SumSeries1 {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
||||
{
|
||||
System.out.printf(<span class="string">"</span><span class="string">%-10s%15s</span><span class="literal">\n</span><span class="string">"</span>, <span class="string">"</span><span class="string">1</span><span class="string">"</span>, <span class="string">"</span><span class="string">m(i)</span><span class="string">"</span>);
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i <= <span class="number">10</span>; ++i) {
|
||||
System.out.printf(<span class="string">"</span><span class="string">%-10d%-15.6f</span><span class="literal">\n</span><span class="string">"</span>,i, m(i));
|
||||
}
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">double</span> m(<span class="literal">int</span> i) {
|
||||
<span class="literal">if</span> (i == <span class="number">1</span>) {
|
||||
<span class="literal">return</span> <span class="number">1</span>;
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> m(i - <span class="number">1</span>) + <span class="number">1.0</span> / i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,51 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>SumSeries2.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/SumSeries2.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.lab5.recursion2_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> SumSeries2 {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
||||
{
|
||||
System.out.printf(<span class="string">"</span><span class="string">%-10s%15s</span><span class="literal">\n</span><span class="string">"</span>, <span class="string">"</span><span class="string">i</span><span class="string">"</span>, <span class="string">"</span><span class="string">m(i)</span><span class="string">"</span>);
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i <= <span class="number">10</span>; ++i) {
|
||||
System.out.printf(<span class="string">"</span><span class="string">%-10d%-15.6f</span><span class="literal">\n</span><span class="string">"</span>,i, m(i));
|
||||
}
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">double</span> m(<span class="literal">int</span> i) {
|
||||
<span class="literal">if</span> (i == <span class="number">1</span>) {
|
||||
<span class="literal">return</span> <span class="number">1.0</span> / <span class="number">3</span>;
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> m(i - <span class="number">1</span>) + (<span class="literal">double</span>) i / (<span class="number">2</span> * i + <span class="number">1</span>);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,59 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>UpperCaseInArray.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/UpperCaseInArray.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.lab5.recursion2_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> UpperCaseInArray {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
||||
{
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a string: </span><span class="string">"</span>);
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.in);
|
||||
String s = input.nextLine();
|
||||
<span class="literal">char</span>[] items = s.toCharArray();
|
||||
System.out.println(<span class="string">"</span><span class="string">The number of uppercase letters is </span><span class="string">"</span>
|
||||
+ count(items));
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(<span class="literal">char</span>[] chars) {
|
||||
<span class="literal">return</span> count(chars, chars.length - <span class="number">1</span>);
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(<span class="literal">char</span>[] chars, <span class="literal">int</span> high) {
|
||||
<span class="literal">if</span> (high >= <span class="number">0</span>) {
|
||||
<span class="literal">return</span> count(chars, high - <span class="number">1</span>) + (Character.isUpperCase(chars[high]) ? <span class="number">1</span> : <span class="number">0</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> <span class="number">0</span>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -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.lab5.recursion2_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class NestedLoopsIndexes {
|
||||
static final int ROWS = 3;
|
||||
static final int COLUMNS = 5;
|
||||
|
||||
public static void nestedLoopsIndexesR(int i, int j) {
|
||||
if (j == COLUMNS) {
|
||||
System.out.println();
|
||||
return;
|
||||
} else if (i == ROWS) {
|
||||
return;
|
||||
}
|
||||
System.out.print(i + ", " + j + " ");
|
||||
nestedLoopsIndexesR(i, ++j);
|
||||
if (i + 1 == j) {
|
||||
nestedLoopsIndexesR(++i, 0);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
nestedLoopsIndexesR(0, 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class OccurencesOfSpecifiedCharacterInArray {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.print("Enter a string: ");
|
||||
Scanner input = new Scanner(System.in);
|
||||
String s = input.nextLine();
|
||||
char[] items = s.toCharArray();
|
||||
|
||||
System.out.print("Enter a character: ");
|
||||
char ch = input.nextLine().trim().charAt(0);
|
||||
|
||||
System.out.println(ch + " appears " + count(items, ch) + " times.");
|
||||
}
|
||||
public static int count(char[] chars, char ch) {
|
||||
return count(chars, chars.length - 1, ch);
|
||||
}
|
||||
public static int count(char[] chars, int high, char ch) {
|
||||
if (high >= 0) {
|
||||
return count(chars, high - 1, ch) + (ch == chars[high] ? 1 : 0);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
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 = {
|
||||
8, 2, 1, 1, 7, 4, -1, 50, 49
|
||||
};
|
||||
selectionSortR(arr);
|
||||
|
||||
for (int i = 0; i < arr.length; ++i) {
|
||||
System.out.print(arr[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class SumOfDigits {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter an integer: ");
|
||||
int i = input.nextInt();
|
||||
System.out.print("The sum of digits in " + i
|
||||
+ " is " + sumDigits(i));
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static long sumDigits(long n)
|
||||
{
|
||||
// Base case: if the number is a single digit, return it
|
||||
if (n < 10) {
|
||||
return n;
|
||||
} else {
|
||||
// Recursive case: sum the last digit and call the function on the remaining digits
|
||||
long lastDigit = n % 10;
|
||||
long remainingDigits = n / 10;
|
||||
return lastDigit + sumDigits(remainingDigits);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class UpperCaseInArray {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.print("Enter a string: ");
|
||||
Scanner input = new Scanner(System.in);
|
||||
String s = input.nextLine();
|
||||
char[] items = s.toCharArray();
|
||||
System.out.println("The number of uppercase letters is "
|
||||
+ count(items));
|
||||
}
|
||||
|
||||
public static int count(char[] chars) {
|
||||
return count(chars, chars.length - 1);
|
||||
}
|
||||
public static int count(char[] chars, int high) {
|
||||
if (high >= 0) {
|
||||
return count(chars, high - 1) + (Character.isUpperCase(chars[high]) ? 1 : 0);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue