Skip to content

Commit d4c6e0e

Browse files
committed
cohort 42.1 hw #18, lesson #19
1 parent a035f63 commit d4c6e0e

4 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package lesson18.homework;
2+
3+
/**
4+
* AIT-TR, cohort 42.1, Java Basic, hw #18
5+
*
6+
* @author Sergey Iryupin
7+
* @version 26-Feb-24
8+
*/
9+
public class HomeWork18 {
10+
public static void main(String[] args) {
11+
// task #1
12+
Simple s1 = new Simple();
13+
System.out.println(Simple.getCount());
14+
Simple s2 = new Simple();
15+
System.out.println(Simple.getCount());
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lesson18.homework;
2+
3+
public class Simple {
4+
private static int count;
5+
6+
public Simple() {
7+
count++;
8+
}
9+
10+
public static int getCount() {
11+
return count;
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lesson19;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* AIT-TR, cohort 42.1, Java Basic, Lesson #19
7+
*
8+
* @version 26-Feb-24
9+
*/
10+
public class Lesson19 {
11+
public static void main(String[] args) {
12+
RubberArray ra = new RubberArray();
13+
ra.add(8);
14+
ra.add(12);
15+
ra.add(-1);
16+
ra.add(3);
17+
ra.add(25);
18+
System.out.println(ra);
19+
System.out.println(ra.get(2));
20+
ra.remove(2);
21+
System.out.println(ra);
22+
}
23+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package lesson19;
2+
3+
public class RubberArray {
4+
5+
private final int INIT_DATA_SIZE = 10;
6+
private final float RESIZE_KOEF = 1.5f;
7+
private int[] data;
8+
private int length;
9+
10+
public RubberArray() {
11+
data = new int[INIT_DATA_SIZE];
12+
length = 0;
13+
}
14+
15+
public int get(int idx) {
16+
return data[idx];
17+
}
18+
19+
public void add(int value) {
20+
if (length == data.length) {
21+
// create new array, length *= 1.5
22+
int[] newData = new int[(int) (data.length * RESIZE_KOEF)];
23+
// move all items to new array
24+
for (int i = 0; i < data.length; i++) {
25+
newData[i] = data[i];
26+
}
27+
// change link to new array
28+
data = newData;
29+
}
30+
// add value
31+
data[length] = value;
32+
length++;
33+
}
34+
35+
public void add(int value, int idx) {
36+
// TODO implement
37+
}
38+
39+
public void remove(int idx) {
40+
// move elements right to left from idx
41+
for (int i = idx; i < data.length - 1; i++) {
42+
data[i] = data[i + 1];
43+
}
44+
length--;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
// TODO use StringBuilder instead of String
50+
String str = "[";
51+
for (int i = 0; i < length; i++) {
52+
str += String.valueOf(data[i]);
53+
if (i < length - 1) {
54+
str += ", ";
55+
}
56+
}
57+
return str + "]";
58+
}
59+
}

0 commit comments

Comments
 (0)