Skip to content

Commit 0fe75e6

Browse files
committed
add exercises
1 parent 5c94ebe commit 0fe75e6

5 files changed

Lines changed: 157 additions & 15 deletions

File tree

‎ch08/src/Exercise8_5.java‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.Map;
24

35
public class Exercise8_5 {
4-
5-
public static void sieve(int n) {
6-
int[] start = new int[n];
7-
boolean[] bool = new boolean[n];
8-
for (int i = 0; i < start.length; i++) {
9-
start[i] = i;
10-
}
11-
for (int i = 0; i < bool.length; i++) {
12-
if (start[i] == i * i) {
13-
bool[i] = false;
14-
} else {
6+
7+
public static void sieve(int limit) {
8+
9+
int[] nums = new int[limit];
10+
for (int i = 0; i < nums.length; i++) {
11+
nums[i] = i;
12+
}
13+
System.out.println(Arrays.toString(nums));
14+
15+
boolean[] bool = new boolean[limit];
16+
for (int i = 2; i < bool.length; i++) {
1517
bool[i] = true;
1618
}
17-
}
19+
20+
// Mark all composite numbers
21+
for (int i = 2; i < (bool.length / 2); i++) {
22+
if (bool[i]) {
23+
// bool[i] = false;
24+
int multiple = 2;
25+
while (i * multiple < bool.length) {
26+
bool [i * multiple] = false;
27+
multiple++;
28+
}
29+
}
30+
}
1831

19-
System.out.println(Arrays.toString(start));
20-
System.out.println(Arrays.toString(bool));
21-
}
32+
33+
Map myMap = new HashMap(bool.length);
34+
for (int i = 0; i < nums.length; i++) {
35+
myMap.put(nums[i], bool[i]);
36+
}
37+
38+
39+
System.out.println(Arrays.toString(bool));
40+
System.out.println(myMap);
41+
}
2242

2343
public static void main(String[] args) {
2444
sieve(120);

‎ch08/src/Histogram2.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
4+
/**
5+
* Example code related to histograms.
6+
*/
7+
public class Histogram2 {
8+
9+
/**
10+
* Returns an array of random integers.
11+
*/
12+
public static int[] randomArray(int size) {
13+
Random random = new Random();
14+
int[] a = new int[size];
15+
for (int i = 0; i < a.length; i++) {
16+
a[i] = random.nextInt(100);
17+
}
18+
return a;
19+
}
20+
21+
public static void main(String[] args) {
22+
23+
int[] scores = randomArray(100);
24+
25+
// making a histogram
26+
int[] counts = new int[100];
27+
for (int i = 0; i < scores.length; i++) {
28+
int index = scores[i];
29+
counts[index]++;
30+
}
31+
32+
System.out.println(Arrays.toString(scores));
33+
System.out.println(Arrays.toString(counts));
34+
35+
}
36+
37+
}

‎ch09/src/Exercise9_1.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
public class Exercise9_1 {
3+
public static void main(String[] args) {
4+
boolean myB1 = false;
5+
char myChar1 = 'm';
6+
int myInt1 = 1;
7+
double myD1 = 1.0;
8+
String myString1 = "MyString1";
9+
10+
boolean myB2 = true;
11+
char myChar2 = 'n';
12+
int myInt2 = 2;
13+
double myD2 = 2.0;
14+
String myString2 = "MyString2";
15+
16+
System.out.println(myD2 + myInt1);
17+
System.out.println((myString1 + myD2).getClass().getName());
18+
}
19+
}

‎ch09/src/Exercise9_2.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Arrays;
2+
3+
public class Exercise9_2 {
4+
5+
public static void letterHist(String lettersCount) {
6+
7+
String temp = lettersCount.toLowerCase();
8+
9+
char[] myCharsFromString = temp.toCharArray();
10+
11+
// creating alfabet array
12+
char[] alfabet = new char[25];
13+
char c = 'a';
14+
for (int i = 0; i < alfabet.length; i++) {
15+
alfabet[i] = c;
16+
c++;
17+
}
18+
19+
// making a histogram
20+
int[] counts = new int[25];
21+
22+
for (int i = 0; i < myCharsFromString.length; i++) {
23+
char index = myCharsFromString[i];
24+
for (int j = 0; j < alfabet.length; j++) {
25+
if (alfabet[j] == index) {
26+
counts[j]++;
27+
}
28+
}
29+
30+
}
31+
32+
System.out.println(Arrays.toString(alfabet));
33+
System.out.println(Arrays.toString(counts));
34+
35+
}
36+
37+
public static void main(String[] args) {
38+
letterHist("My test");
39+
40+
}
41+
}

‎ch11/src/MyMath.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
public class MyMath {
3+
4+
// Nai maluk obsht delitel
5+
public static int nod(int a, int b) {
6+
while (b != 0) {
7+
int t = b;
8+
b = a % b;
9+
a = t;
10+
}
11+
return a;
12+
}
13+
14+
// Nai malko obshto kratno
15+
public static int nok(int a, int b) {
16+
int nod = nod(a, b);
17+
int result = a * b;
18+
return result / nod;
19+
}
20+
21+
public static void main(String[] args) {
22+
System.out.println(nod(1071, 462));
23+
System.out.println(nok(1071, 462));
24+
}
25+
}

0 commit comments

Comments
 (0)