MP2 is pog

master
Caleb Fontenot 2023-09-06 14:34:16 +07:00
parent 0d37491e6d
commit de8e3acf40
44 changed files with 834 additions and 36 deletions

@ -107,7 +107,7 @@ manifest.custom.permissions=
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=JDK_8__System_
platform.active=JDK_1.8
run.classpath=\
${dist.jar}:\
${javac.classpath}:\

@ -17,8 +17,12 @@ import javafx.stage.Stage;
*/
public class BallControl extends Application {
private boolean mouseFocus = false;
private int racketPosition = 0;
@Override
public void start(Stage primaryStage) throws Exception {
public void start(Stage primaryStage) throws Exception
{
BouncingBall bouncingBall = new BouncingBall();
// Create a scene and place it in the stage
@ -27,19 +31,22 @@ public class BallControl extends Application {
primaryStage.setScene(scene);
primaryStage.show();
bouncingBall.requestFocus();
bouncingBall.showInfoLabel();
bouncingBall.setOnMouseEntered(e
-> {
mouseFocus = true;
bouncingBall.hideInfoLabel();
bouncingBall.play();
});
bouncingBall.setOnMouseExited(e -> {
bouncingBall.showInfoLabel();
bouncingBall.pause();
mouseFocus = false;
});
bouncingBall.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
public void handle(MouseEvent event)
{
double mouseX = event.getSceneX() - (bouncingBall.RACKET_LENGTH / 2); // set relative to center of racket
bouncingBall.moveRacket(mouseX);
}
@ -49,6 +56,20 @@ public class BallControl extends Application {
// Increase and decrease animation
bouncingBall.setOnKeyPressed(e
-> {
if (e.getCode() == KeyCode.LEFT && mouseFocus == false) {
bouncingBall.hideInfoLabel();
if (racketPosition > 0) {
racketPosition = racketPosition - 30;
}
bouncingBall.moveRacket(racketPosition);
}
if (e.getCode() == KeyCode.RIGHT && mouseFocus == false) {
bouncingBall.hideInfoLabel();
if (racketPosition < bouncingBall.getWidth()) {
racketPosition = racketPosition + 30;
}
bouncingBall.moveRacket(racketPosition);
}
if (e.getCode() == KeyCode.UP) {
bouncingBall.increaseSpeed();
} else if (e.getCode() == KeyCode.DOWN) {
@ -59,7 +80,8 @@ public class BallControl extends Application {
);
}
public static void main(String[] args) {
public static void main(String[] args)
{
launch(args);
}
}

@ -28,6 +28,7 @@ import javafx.util.Duration;
public class BouncingBall extends Pane {
GridPane textPane = new GridPane();
private final int COLLISION_COOLDOWN = 20;
private long timeSinceLastCollisionEvent = 0;
final double RACKET_LENGTH = 100;
final double radius = 20;
@ -41,33 +42,43 @@ public class BouncingBall extends Pane {
private Label playerScoreLabel = new Label("Player Score: 0");
private Label computerScoreLabel = new Label("Computer Score: 0");
private Timeline animation;
private int computerScore, playerScore = 0;
public BouncingBall() {
public BouncingBall()
{
circle.setFill(Color.RED); // Set ball color
racket.setFill(Color.BLUE);
textPane.add(racketTime, 0, 0);
textPane.add(ballCords, 0, 1);
textPane.add(playerScoreLabel, 0, 2);
textPane.add(computerScoreLabel, 0, 3);
getChildren().addAll(circle, racket, textPane, infoLabel); // Place a ball into this pane
getChildren().addAll(circle, racket, textPane); // Place a ball into this pane
racket.relocate(0, 580);
infoLabel.relocate(getHeight() / 4.0, getWidth() / 2.0);
// Create an animation for moving the ball
animation = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
public void handle(ActionEvent t)
{
timeSinceLastCollisionEvent++;
racketTime.setText("Frames since last collision: " + timeSinceLastCollisionEvent);
moveBall();
if (y >= (getHeight() - 20) && (timeSinceLastCollisionEvent > 500)) {
// Process collisions
if (y >= (getHeight() - 20) && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) {
timeSinceLastCollisionEvent = 0;
playSound("collision");
incrementComputerScore();
}
if ((x <= 20 && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) || ((x >= getWidth() - 20) && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN))) {
timeSinceLastCollisionEvent = 0;
playSound("collision");
}
if (y <= 20 && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) {
timeSinceLastCollisionEvent = 0;
playSound("collision");
}
}
})
);
@ -76,20 +87,23 @@ public class BouncingBall extends Pane {
}
private boolean processRacketCollision() {
private boolean processRacketCollision()
{
boolean racketCollision = racket.getBoundsInParent().intersects(circle.getBoundsInParent());
if (racketCollision && (timeSinceLastCollisionEvent > 500)) { // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.
if (racketCollision && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) { // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.
System.out.println("Racket collision detected!");
timeSinceLastCollisionEvent = 0;
incrementPlayerScore();
playSound("racket");
return true;
} else {
return false;
}
}
private void moveBall() {
private void moveBall()
{
// Check boundaries
if (x < radius || x > getWidth() - radius) {
@ -107,33 +121,40 @@ public class BouncingBall extends Pane {
ballCoordsToLabel();
}
public void play() {
public void play()
{
animation.play();
}
public void pause() {
public void pause()
{
animation.pause();
}
public void increaseSpeed() {
public void increaseSpeed()
{
animation.setRate(animation.getRate() * 1.5);
System.out.println(animation.getRate());
}
public void decreaseSpeed() {
public void decreaseSpeed()
{
animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0);
System.out.println(animation.getRate());
}
public DoubleProperty rateProperty() {
public DoubleProperty rateProperty()
{
return animation.rateProperty();
}
public void moveRacket(double x) {
public void moveRacket(double x)
{
racket.relocate(x, 580);
}
public void showInfoLabel() {
public void showInfoLabel()
{
double paneHeight = getHeight();
double paneWidth = getWidth();
getChildren().add(infoLabel);
@ -141,34 +162,58 @@ public class BouncingBall extends Pane {
infoLabel.relocate(paneWidth / 4.0, paneHeight / 2.0);
}
public void hideInfoLabel() {
public void hideInfoLabel()
{
getChildren().remove(infoLabel);
}
private void incrementPlayerScore() {
private void incrementPlayerScore()
{
playerScoreLabel.setText("Player score: " + ++playerScore);
playSound();
}
private void incrementComputerScore() {
private void incrementComputerScore()
{
computerScoreLabel.setText("Computer score: " + ++computerScore);
//playSound();
}
private void ballCoordsToLabel() {
private void ballCoordsToLabel()
{
ballCords.setText("Ball coords: " + x + ", " + y);
}
private void playSound() {
private void playSound(String sampleType)
{
String sample = "";
int randInt = (int) (Math.random() * 2);
String sample;
if (randInt == 0) {
sample = "5";
} else {
sample = "7";
switch (sampleType) {
case "collision":
switch (randInt) {
case 0:
sample = "05";
break;
case 1:
sample = "07";
break;
}
break;
case "racket":
switch (randInt) {
case 0:
sample = "10";
break;
case 1:
sample = "12";
break;
}
}
Media sound = new Media(new File("sound/Sample_000"+sample+".wav").toURI().toString());
Media sound = new Media(new File("sound/Sample_00" + sample + ".wav").toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.setStartTime(Duration.ZERO);
mediaPlayer.play();
//mediaPlayer.setOnEndOfMedia();
}

@ -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">&quot;</span><span class="string">, </span><span class="string">&quot;</span> + j + <span class="string">&quot;</span> <span class="string">&quot;</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">&quot;</span><span class="string">Enter a string: </span><span class="string">&quot;</span>);
String s = input.nextLine();
System.out.print(<span class="string">&quot;</span><span class="string">Enter a character: </span><span class="string">&quot;</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">&quot;</span><span class="string"> appears </span><span class="string">&quot;</span> + times
+ (times &gt; <span class="number">1</span> ? <span class="string">&quot;</span><span class="string"> times </span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string"> time </span><span class="string">&quot;</span>) + <span class="string">&quot;</span><span class="string">in </span><span class="string">&quot;</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() &gt; <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">&quot;</span><span class="string">Enter a string: </span><span class="string">&quot;</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">&quot;</span><span class="string">Enter a character: </span><span class="string">&quot;</span>);
<span class="literal">char</span> ch = input.nextLine().trim().charAt(<span class="number">0</span>);
System.out.println(ch + <span class="string">&quot;</span><span class="string"> appears </span><span class="string">&quot;</span> + count(items, ch) + <span class="string">&quot;</span><span class="string"> times.</span><span class="string">&quot;</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 &gt;= <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">&quot;</span><span class="string">Enter an integer: </span><span class="string">&quot;</span>);
<span class="literal">int</span> i = input.nextInt();
System.out.print(<span class="string">&quot;</span><span class="string">The reversal of </span><span class="string">&quot;</span> + i + <span class="string">&quot;</span><span class="string"> is </span><span class="string">&quot;</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 &lt; n; ++j) {
<span class="literal">if</span> (arr[j] &lt; arr[min]) {
min = j;
}
}
<span class="ST3">swap</span>(arr, min, i);
<span class="literal">if</span> ( i + <span class="number">1</span> &lt; 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 &lt; arr.<span class="ST4">length</span>; ++i) {
System.<span class="ST5">out</span>.print(arr[i] + <span class="string">&quot;</span> <span class="string">&quot;</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">&quot;</span><span class="string">Enter an integer: </span><span class="string">&quot;</span>);
<span class="literal">int</span> i = input.nextInt();
System.out.print(<span class="string">&quot;</span><span class="string">The sum of digits in </span><span class="string">&quot;</span> + i
+ <span class="string">&quot;</span><span class="string"> is </span><span class="string">&quot;</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 &lt; <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">&quot;</span><span class="string">%-10s%15s</span><span class="literal">\n</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">1</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">m(i)</span><span class="string">&quot;</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">10</span>; ++i) {
System.out.printf(<span class="string">&quot;</span><span class="string">%-10d%-15.6f</span><span class="literal">\n</span><span class="string">&quot;</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">&quot;</span><span class="string">%-10s%15s</span><span class="literal">\n</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">i</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">m(i)</span><span class="string">&quot;</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">10</span>; ++i) {
System.out.printf(<span class="string">&quot;</span><span class="string">%-10d%-15.6f</span><span class="literal">\n</span><span class="string">&quot;</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">&quot;</span><span class="string">Enter a string: </span><span class="string">&quot;</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">&quot;</span><span class="string">The number of uppercase letters is </span><span class="string">&quot;</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 &gt;= <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;
}
}
}