Skip to content

Commit 5b0ad9f

Browse files
committed
Added Patterns, CeaserCipher Encryption, Check if array is sorted, and ToDo List, Fixed Link of Bubble Sort in the README.md
1 parent 7976819 commit 5b0ad9f

14 files changed

+436
-1
lines changed

Programs/CeaserCipher.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package Programs;
2+
3+
import java.util.Scanner;
4+
5+
public class CeaserCipher {
6+
public static void main(String[] args) {
7+
System.out.println("""
8+
--------------------------------
9+
CEASER CIPHER ENCRYPT/ DECRYPT
10+
--------------------------------
11+
""");
12+
Scanner sc = new Scanner(System.in);
13+
System.out.println("Encrypt or Decrypt:\n 1- Encryption\n 2- Decryption");
14+
int type = sc.nextInt();
15+
for (int j = 1; j > 0; j++) {
16+
if (type == 1) {
17+
ceaserCipherEncryption();
18+
break;
19+
} else if (type == 2) {
20+
ceaserCipherDecryption();
21+
break;
22+
} else {
23+
System.out.print("Please enter a valid number (1 or 2): ");
24+
type = sc.nextInt();
25+
}
26+
}
27+
}
28+
public static void ceaserCipherEncryption() {
29+
Scanner textInput = new Scanner(System.in);
30+
System.out.print("PlainText: ");
31+
String textToEncrypt = textInput.nextLine();
32+
System.out.print("shifting key: ");
33+
int keyToUse = textInput.nextInt();
34+
String cipher = "";
35+
for (int i = 0; true; i++) {
36+
if (keyToUse > 50 && keyToUse <= 0) {
37+
System.out.print("Please Enter key value <= 50: ");
38+
keyToUse = textInput.nextInt();
39+
} else {
40+
for (int j = 0; j < textToEncrypt.length(); j++) {
41+
char temp = textToEncrypt.charAt(j);
42+
if (temp >= 'A' && temp <= 'Z') {
43+
temp = (char) (temp + keyToUse);
44+
if (temp > 'Z') { // go back to A in Ascii
45+
temp = (char) (temp + 'A' - 'Z' - 1);
46+
47+
}
48+
cipher += temp;
49+
} else if (temp >= 'a' && temp <= 'z') {
50+
temp = (char) (temp + keyToUse);
51+
if (temp > 'z') { // go back to a in Ascii
52+
temp = (char) (temp + 'a' - 'z' - 1);
53+
54+
}
55+
cipher += temp;
56+
} else
57+
cipher += temp;
58+
59+
}
60+
}
61+
System.out.println("Enrypted text is: " + cipher);
62+
break;
63+
64+
}
65+
}
66+
67+
68+
public static void ceaserCipherDecryption() {
69+
Scanner textInput = new Scanner(System.in);
70+
System.out.print("Encrypted Text: ");
71+
String textToEncrypt = textInput.nextLine();
72+
System.out.print("shifting key: ");
73+
int keyToUse = textInput.nextInt();
74+
String cipher = "";
75+
for (int i = 0; true; i++) {
76+
if (keyToUse > 50 && keyToUse <= 0) {
77+
System.out.print("Please Enter key value <= 50: ");
78+
keyToUse = textInput.nextInt();
79+
} else {
80+
for (int j = 0; j < textToEncrypt.length(); j++) {
81+
char temp = textToEncrypt.charAt(j);
82+
if (temp >= 'A' && temp <= 'Z') {
83+
temp = (char) (temp - keyToUse);
84+
if (temp < 'A') { // go back to A in Ascii
85+
temp = (char) (temp - 'A' + 'Z' + 1);
86+
87+
}
88+
cipher += temp;
89+
} else if (temp >= 'a' && temp <= 'z') {
90+
temp = (char) (temp - keyToUse);
91+
if (temp < 'a') { // go back to a in Ascii
92+
temp = (char) (temp - 'a' + 'z' + 1);
93+
}
94+
cipher += temp;
95+
} else
96+
cipher += temp;
97+
}
98+
}
99+
System.out.println("Decrypted text is: " + cipher);
100+
break;
101+
}
102+
}
103+
104+
}

Programs/Check_If_Sorted.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Programs;
2+
3+
public class Check_If_Sorted {
4+
public static void main(String[] args) {
5+
int[] toCheck = {1, 2, 3, 5, 16, 8};
6+
int pointer = 0;
7+
System.out.println(checkIfSorted(toCheck, pointer));
8+
}
9+
10+
private static boolean checkIfSorted(int[] toCheck, int pointer) {
11+
if (pointer == toCheck.length - 1) {
12+
return true;
13+
}
14+
return toCheck[pointer] < toCheck[pointer + 1] && checkIfSorted(toCheck, pointer + 1);
15+
}
16+
}

Programs/ToDoList.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package Programs;
2+
3+
import java.util.ArrayList;
4+
import java.util.Scanner;
5+
6+
public class ToDoList {
7+
public static void main(String[] args) {
8+
ArrayList<String> tasks = new ArrayList<>();
9+
list(tasks);
10+
}
11+
12+
public static void list(ArrayList<String> tasks) {
13+
Scanner sc = new Scanner(System.in);
14+
System.out.print("Function you want to apply(1-4):\n"
15+
+ "1- View\n"
16+
+ "2- Add\n"
17+
+ "3- Delete All Tasks\n"
18+
+ "> ");
19+
int desiredFunction = sc.nextInt();
20+
choose(desiredFunction, tasks, sc);
21+
}
22+
23+
public static void choose(int desiredFunction, ArrayList<String> tasks, Scanner sc) {
24+
if (desiredFunction == 1) {
25+
viewTask(tasks);
26+
} else if (desiredFunction == 2) {
27+
addTask(tasks);
28+
} else if (desiredFunction == 3) {
29+
deleteTasks(tasks);
30+
} else {
31+
System.out.println("Please choose in the range of 1 till 3 only!");
32+
list(tasks);
33+
}
34+
}
35+
36+
static void addTask(ArrayList<String> tasks) {
37+
Scanner sc = new Scanner(System.in);
38+
System.out.print("Task to save\n> ");
39+
tasks.add(sc.nextLine());
40+
System.out.print("Add more?\n 1- Yes\n 2- No\n> ");
41+
int yn = sc.nextInt();
42+
if (yn == 1) {
43+
addTask(tasks);
44+
} else if (yn == 2){
45+
list(tasks);
46+
} else {
47+
System.out.println("Invalid number!");
48+
addTask(tasks);
49+
}
50+
}
51+
52+
static void viewTask(ArrayList<String> tasks) {
53+
if (tasks.isEmpty()) {
54+
System.out.println("""
55+
--------------------
56+
No current tasks!
57+
--------------------""");
58+
} else {
59+
System.out.println("""
60+
--------------------
61+
Tasks
62+
--------------------""");
63+
int counter = 1;
64+
for (int i = 0; i < tasks.size(); i++) {
65+
System.out.println(counter + "- " + tasks.get(i));
66+
counter++;
67+
}
68+
}
69+
System.out.println("\nSelect from the following commands:");
70+
list(tasks);
71+
}
72+
73+
static void deleteTasks(ArrayList<String> tasks) {
74+
tasks.clear();
75+
}
76+
}

Programs/patterns/Pattern1.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Programs.patterns;
2+
3+
public class Pattern1 {
4+
public static void main(String[] args) {
5+
pattern1(4);
6+
}
7+
// *
8+
// * *
9+
// * * *
10+
// * * * *
11+
private static void pattern1(int n) {
12+
// for every row, run the col
13+
for (int row = 0; row <= n; row++) {
14+
for (int col = 0; col < row; col++) {
15+
System.out.print("* ");
16+
}
17+
System.out.println();
18+
}
19+
}
20+
}
21+

Programs/patterns/Pattern2.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Programs.patterns;
2+
3+
public class Pattern2 {
4+
public static void main(String[] args) {
5+
patter2(4);
6+
}
7+
// * * * *
8+
// * * * *
9+
// * * * *
10+
// * * * *
11+
// * * * *
12+
private static void patter2(int n) {
13+
// for every row, run the col
14+
for (int row = 0; row <= n; row++) {
15+
for (int col = 0; col < n; col++) {
16+
System.out.print("* ");
17+
}
18+
System.out.println();
19+
}
20+
}
21+
}

Programs/patterns/Pattern3.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Programs.patterns;
2+
3+
public class Pattern3 {
4+
public static void main(String[] args) {
5+
pattern3(4);
6+
}
7+
// * * * *
8+
// * * *
9+
// * *
10+
// *
11+
12+
static void pattern3(int n) {
13+
for (int row = 1; row <= n; row++) {
14+
// for every row, run the col
15+
for (int col = 1; col <= n-row+1; col++) {
16+
System.out.print("* ");
17+
}
18+
// when one row is printed, we need to add a newline
19+
System.out.println();
20+
}
21+
}
22+
}

Programs/patterns/Pattern4.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Programs.patterns;
2+
3+
public class Pattern4 {
4+
public static void main(String[] args) {
5+
pattern4(4);
6+
}
7+
// 1
8+
// 1 2
9+
// 1 2 3
10+
// 1 2 3 4
11+
static void pattern4(int n) {
12+
for (int row = 1; row <= n; row++) {
13+
// for every row, run the col
14+
for (int col = 1; col <= row; col++) {
15+
System.out.print(col + " ");
16+
}
17+
// when one row is printed, we need to add a newline
18+
System.out.println();
19+
}
20+
}
21+
}

Programs/patterns/Pattern5.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Programs.patterns;
2+
3+
public class Pattern5 {
4+
public static void main(String[] args) {
5+
pattern5(4);
6+
}
7+
// *
8+
// * *
9+
// * * *
10+
// * * * *
11+
// * * *
12+
// * *
13+
// *
14+
static void pattern5(int n) {
15+
for (int row = 0; row < 2 * n; row++) {
16+
int totalColsInRow = row > n ? 2 * n - row: row;
17+
for (int col = 0; col < totalColsInRow; col++) {
18+
System.out.print("* ");
19+
}
20+
System.out.println();
21+
}
22+
}
23+
24+
}

Programs/patterns/Pattern6.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Programs.patterns;
2+
3+
public class Pattern6 {
4+
public static void main(String[] args) {
5+
pattern6(4);
6+
}
7+
// *
8+
// * *
9+
// * * *
10+
// * * * *
11+
// * * *
12+
// * *
13+
// *
14+
private static void pattern6(int n) {
15+
for (int row = 0; row < 2 * n; row++) {
16+
int totalColsInRow = row > n ? 2 * n - row: row;
17+
18+
int noOfSpaces = n - totalColsInRow;
19+
for (int s = 0; s < noOfSpaces; s++) {
20+
System.out.print(" ");
21+
}
22+
23+
for (int col = 0; col < totalColsInRow; col++) {
24+
System.out.print("* ");
25+
}
26+
System.out.println();
27+
}
28+
}
29+
}

Programs/patterns/Pattern7.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Programs.patterns;
2+
3+
public class Pattern7 {
4+
public static void main(String[] args) {
5+
pattern7(4);
6+
}
7+
// 1
8+
// 2 1 2
9+
// 3 2 1 2 3
10+
//4 3 2 1 2 3 4
11+
// 3 2 1 2 3
12+
// 2 1 2
13+
// 1
14+
static void pattern7(int n) {
15+
for (int row = 1; row <= 2 * n; row++) {
16+
17+
int c = row > n ? 2 * n - row: row;
18+
19+
for (int space = 0; space < n-c; space++) {
20+
System.out.print(" ");
21+
}
22+
23+
for (int col = c; col >= 1; col--) {
24+
System.out.print(col + " ");
25+
}
26+
for (int col = 2; col <= c; col++) {
27+
System.out.print(col + " ");
28+
}
29+
30+
System.out.println();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)