Skip to content

Commit 35b8738

Browse files
committed
Day 6
1 parent 0870d66 commit 35b8738

29 files changed

+637
-5
lines changed

ch-18/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-18/src/ComputeFactorials.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*
5+
* Case Study: Compute Factorials
6+
7+
* 0! = 1;
8+
* n! = n × (n - 1)!; n > 0
9+
*
10+
*/
11+
public class ComputeFactorials {
12+
public static void main(String[] args) {
13+
Scanner scanner = new Scanner(System.in);
14+
System.out.println("Compute factorial of the number: ");
15+
int n = scanner.nextInt();
16+
17+
System.out.println(n + " factorial is equal to: " + computeFactorial(n));
18+
}
19+
20+
public static int computeFactorial(int n) {
21+
if(n == 0) {
22+
return 1;
23+
} else if (n == 1) {
24+
return 1;
25+
} else {
26+
return n * computeFactorial(n - 1);
27+
}
28+
}
29+
}

ch-18/src/ComputeGCD.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*
5+
* *18.3 (Compute greatest common divisor using recursion)
6+
7+
* The gcd(m, n) can also
8+
* be defined recursively as follows:
9+
* ■ If m % n is 0, gcd(m, n) is n.
10+
* ■ Otherwise, gcd(m, n) is gcd(n, m % n).
11+
* Write a recursive method to find the GCD. Write a test program that prompts the
12+
* user to enter two integers and displays their GCD.
13+
*
14+
*/
15+
public class ComputeGCD {
16+
public static void main(String[] args) {
17+
Scanner scanner = new Scanner(System.in);
18+
System.out.println("Enter first number:");
19+
int m = scanner.nextInt();
20+
System.out.println("Enter second number: ");
21+
int n = scanner.nextInt();
22+
23+
System.out.println("GCD: " + computeGCD(m, n));
24+
}
25+
26+
public static int computeGCD(int m, int n) {
27+
if(m % n == 0) {
28+
return n;
29+
} else {
30+
return computeGCD(n, m % n);
31+
}
32+
}
33+
}

ch-18/src/FindDirectorySize.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.File;
2+
import java.io.FileInputStream;
3+
import java.io.IOException;
4+
import java.util.Scanner;
5+
6+
/**
7+
*
8+
* Case Study: Find Directory Size
9+
*
10+
*/
11+
public class FindDirectorySize {
12+
public static void main(String[] args) {
13+
Scanner scanner = new Scanner(System.in);
14+
System.out.println("Enter directory path: ");
15+
File dir = new File(scanner.nextLine());
16+
17+
System.out.println("Directory has a total size of: " + getDirSize(dir));
18+
}
19+
20+
public static long getDirSize(File dir) {
21+
long count = 0;
22+
if(!dir.isDirectory()) {
23+
count += dir.length();
24+
}
25+
26+
File[] fileList = dir.listFiles();
27+
28+
for(int i = 0; fileList != null && i < fileList.length; i++) {
29+
count += getDirSize(fileList[i]);
30+
}
31+
32+
return count;
33+
}
34+
}

ch-18/src/Main.java

-5
This file was deleted.

ch-18/src/ReverseString.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*
5+
* *18.9 (Print the characters in a string reversely)
6+
7+
* Write a recursive method that displays a string reversely on the console using the following header:
8+
* public static void reverseDisplay(String value)
9+
* For example, reverseDisplay("abcd") displays dcba. Write a test program
10+
* that prompts the user to enter a string and displays its reversal
11+
*
12+
*/
13+
public class ReverseString {
14+
public static void main(String[] args) {
15+
Scanner scanner = new Scanner(System.in);
16+
System.out.println("Enter string to reverse: ");
17+
String s = scanner.nextLine();
18+
19+
reverseDisplay(s);
20+
}
21+
22+
public static void reverseDisplay(String value){
23+
if (value.length() == 0) {
24+
return;
25+
} else {
26+
System.out.print(value.charAt(value.length() - 1));
27+
reverseDisplay(value.substring(0, value.length() - 1));
28+
}
29+
}
30+
}

ch-18/src/StringPermutation.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
/**
5+
*
6+
* **18.25 (String permutation)
7+
* Write a recursive method to print all the permutations of a
8+
* string. For example, for the string abc, the permuation is
9+
* abc
10+
* acb
11+
* bac
12+
* bca
13+
* cab
14+
* cba
15+
*
16+
*/
17+
public class StringPermutation {
18+
public static void main(String[] args) {
19+
Scanner scanner = new Scanner(System.in);
20+
System.out.print("Enter a string: ");
21+
String input = scanner.nextLine();
22+
displayPermutation(input);
23+
}
24+
25+
public static void displayPermutation(String s) {
26+
displayPermutation("", s);
27+
}
28+
29+
public static void displayPermutation(String s1, String s2) {
30+
if (s2.isEmpty()) {
31+
System.out.println(s1);
32+
} else {
33+
for (int i = 0; i < s2.length(); i++) {
34+
char currentChar = s2.charAt(i);
35+
String newS1 = s1 + currentChar;
36+
String newS2 = s2.substring(0, i) + s2.substring(i + 1);
37+
displayPermutation(newS1, newS2);
38+
}
39+
}
40+
}
41+
}

ch-19/.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

ch-19/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-19/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-19/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-19/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-19/ch-19.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

ch-19/src/BinarySearch.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class BinarySearch {
2+
public static void main(String[] args) {
3+
Integer[] integers = {1,2,3,4,5,6,7,8,9,10};
4+
System.out.println("Index of key is: " + binarySearch(integers, 4));
5+
}
6+
7+
public static <E extends Comparable<E>> int binarySearch(E[] list, E key) {
8+
int low = 0;
9+
int high = list.length - 1;
10+
boolean found = false;
11+
int keyIndex = -1;
12+
13+
while(!found) {
14+
int mid = (low + high) / 2;
15+
if(list[mid].compareTo(key) == 0) {
16+
keyIndex = mid;
17+
found = true;
18+
} else if (list[mid].compareTo(key) > 0) {
19+
high = mid - 1;
20+
} else {
21+
low = mid + 1;
22+
}
23+
}
24+
25+
return keyIndex;
26+
}
27+
}

ch-19/src/ComparableSort.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
*
3+
* Case Study: Sorting an array of comparable objects
4+
*
5+
*/
6+
public class ComparableSort {
7+
public static void main(String[] args) {
8+
Integer[] intArray = {1,2,3,4,5};
9+
Double[] doubleArray = {3.4,1.3,-22.1};
10+
Character[] charArray = {'a', 'j', 'r'};
11+
String[] stringArray = {"Tom", "Susan", "Kim"};
12+
13+
sort(intArray);
14+
sort(doubleArray);
15+
sort(charArray);
16+
sort(stringArray);
17+
18+
System.out.println("Sorted Integer array: ");
19+
printList(intArray);
20+
System.out.println("Sorted Double array: ");
21+
printList(doubleArray);
22+
System.out.println("Sorted Char array: ");
23+
printList(charArray);
24+
System.out.println("Sorted String array: ");
25+
printList(stringArray);
26+
}
27+
28+
public static <E extends Comparable<E>> void sort(E[] list) {
29+
E currentMin;
30+
int currentMinIndex;
31+
32+
for(int i = 0; i < list.length; i++) {
33+
currentMin = list[i];
34+
currentMinIndex = i;
35+
36+
for(int j = i + 1; j < list.length; j++){
37+
if(currentMin.compareTo(list[j]) > 0) {
38+
currentMin = list[j];
39+
currentMinIndex = j;
40+
}
41+
}
42+
43+
if(currentMinIndex != i) {
44+
list[currentMinIndex] = list[i];
45+
list[i] = currentMin;
46+
}
47+
}
48+
}
49+
50+
public static void printList(Object[] list) {
51+
for (Object o : list) System.out.print(o + " ");
52+
System.out.println();
53+
}
54+
}

ch-19/src/DistinctElements.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
3+
/**
4+
*
5+
* 19.3 (Distinct elements in ArrayList)
6+
7+
* Write the following method that returns a new
8+
* ArrayList. The new list contains the non-duplicate elements from the original list.
9+
*
10+
*/
11+
public class DistinctElements {
12+
public static void main(String[] args) {
13+
ArrayList<Integer> integers = new ArrayList<Integer>() {
14+
{
15+
add(1);
16+
add(1);
17+
add(2);
18+
add(3);
19+
add(2);
20+
add(4);
21+
}
22+
};
23+
24+
integers = removeDuplicates(integers);
25+
26+
System.out.println(integers);
27+
}
28+
29+
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
30+
ArrayList<E> newList = new ArrayList<>();
31+
32+
for(E o: list) {
33+
if (!newList.contains(o)) {
34+
newList.add(o);
35+
}
36+
}
37+
38+
return newList;
39+
}
40+
}

0 commit comments

Comments
 (0)