Skip to content

Commit 44d102f

Browse files
committed
cohort 42.1 hw #21, lesson #22
1 parent dc7ca42 commit 44d102f

10 files changed

Lines changed: 294 additions & 3 deletions

File tree

BerlinAIT/Cohort42.1/src/lesson19/homework/RubberArray.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package lesson19.homework;
22

3-
import java.util.Arrays;
4-
53
public class RubberArray {
64

75
private final int INIT_DATA_SIZE = 10;
@@ -14,6 +12,10 @@ public RubberArray() {
1412
length = 0;
1513
}
1614

15+
public int size() {
16+
return length;
17+
}
18+
1719
public int get(int idx) {
1820
return data[idx];
1921
}
@@ -39,6 +41,9 @@ public void add(int value) {
3941
}
4042

4143
public void add(int value, int idx) {
44+
if (idx > length) {
45+
throw new IndexOutOfBoundsException();
46+
}
4247
extendArrayIfNeed();
4348
// insert value
4449
// 0;1;2;3;4;5
@@ -47,12 +52,14 @@ public void add(int value, int idx) {
4752
for (int i = length; i > idx; i--) {
4853
data[i] = data[i - 1];
4954
}
50-
System.out.println(Arrays.toString(data));
5155
data[idx] = value;
5256
length++;
5357
}
5458

5559
public void remove(int idx) {
60+
if (idx > length) {
61+
throw new IndexOutOfBoundsException();
62+
}
5663
// move elements right to left from idx
5764
for (int i = idx; i < data.length - 1; i++) {
5865
data[i] = data[i + 1];
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package lesson22;
2+
3+
public class Horse {
4+
private String name;
5+
6+
public Horse(String name) {
7+
this.name = name;
8+
}
9+
10+
public void run() {
11+
System.out.println("run...");
12+
}
13+
14+
public void walk() {
15+
System.out.println("walk...");
16+
}
17+
18+
public void hidden() {
19+
System.out.println("Horse hidden...");
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "{" +
25+
"name='" + name + '\'' +
26+
'}';
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package lesson22;
2+
3+
/**
4+
* AIT-TR, cohort 42.1, Java Basic, Lesson #22
5+
*
6+
* @version 4-Mar-24
7+
*/
8+
public class Lesson22 {
9+
public static void main(String[] args) {
10+
Horse horse = new Horse("Tunder");
11+
System.out.println(horse);
12+
horse.run();
13+
horse.walk();
14+
15+
Pegas pegas = new Pegas("Pegas");
16+
System.out.println(pegas);
17+
pegas.run();
18+
pegas.walk();
19+
pegas.fly();
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package lesson22;
2+
3+
public class Pegas extends Horse {
4+
private int wingsCount;
5+
6+
public Pegas(String name) {
7+
super(name);
8+
}
9+
10+
public void hidden() {
11+
super.hidden();
12+
}
13+
14+
public void fly() {
15+
System.out.println("fly...");
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package lesson22;
2+
3+
public class Rectangle extends Shape2D {
4+
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lesson22;
2+
3+
public class Shape2D {
4+
protected int a, b;
5+
6+
public double perimeter() {
7+
return (a + b) * 2;
8+
}
9+
10+
public double area() {
11+
return a * b;
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package lesson22;
2+
3+
public class Square extends Shape2D {
4+
5+
@Override
6+
public double perimeter() {
7+
return a * 4;
8+
}
9+
10+
@Override
11+
public double area() {
12+
return a * a;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package lesson22;
2+
3+
public class TriangleRectangle extends Shape2D {
4+
5+
@Override
6+
public double perimeter() {
7+
return a + b + Math.sqrt(a*a + b*b);
8+
}
9+
10+
@Override
11+
public double area() {
12+
return a * b / 2.;
13+
}
14+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package lesson21.homework;
2+
3+
import java.util.Arrays;
4+
5+
public class RubberArray {
6+
7+
private final int INIT_DATA_SIZE = 10;
8+
private final float EXTEND_INDEX = 1.5f;
9+
private int[] data;
10+
private int length;
11+
12+
public RubberArray() {
13+
data = new int[INIT_DATA_SIZE];
14+
length = 0;
15+
}
16+
17+
public int size() {
18+
return length;
19+
}
20+
21+
public int get(int idx) {
22+
return data[idx];
23+
}
24+
25+
private void extendArrayIfNeed() {
26+
if (length == data.length) {
27+
// create new array, length * EXTEND_INDEX
28+
int[] newData = new int[(int) (data.length * EXTEND_INDEX)];
29+
// move all items to new array
30+
for (int i = 0; i < data.length; i++) {
31+
newData[i] = data[i];
32+
}
33+
// change link to new array
34+
data = newData;
35+
}
36+
}
37+
38+
public void add(int value) {
39+
extendArrayIfNeed();
40+
// add value
41+
data[length] = value;
42+
length++;
43+
}
44+
45+
public void add(int value, int idx) {
46+
if (idx > length) {
47+
throw new IndexOutOfBoundsException();
48+
}
49+
extendArrayIfNeed();
50+
// insert value
51+
// 0;1;2;3;4;5
52+
// 8,4,3,2,1,1 -> i == 5
53+
// 8,4,3,2,2,1 -> i == 4
54+
for (int i = length; i > idx; i--) {
55+
data[i] = data[i - 1];
56+
}
57+
data[idx] = value;
58+
length++;
59+
}
60+
61+
public void remove(int idx) {
62+
if (idx > length) {
63+
throw new IndexOutOfBoundsException();
64+
}
65+
// move elements right to left from idx
66+
for (int i = idx; i < data.length - 1; i++) {
67+
data[i] = data[i + 1];
68+
}
69+
length--;
70+
}
71+
72+
public boolean contains(int value) {
73+
return indexOf(value) != -1;
74+
}
75+
76+
public int indexOf(int value) {
77+
for (int i = 0; i < length; i++) {
78+
if (data[i] == value) {
79+
return i;
80+
}
81+
}
82+
return -1;
83+
}
84+
85+
@Override
86+
public String toString() {
87+
StringBuilder sb = new StringBuilder("[");
88+
for (int i = 0; i < length; i++) {
89+
sb.append(data[i]);
90+
if (i < length - 1) {
91+
sb.append(", ");
92+
}
93+
}
94+
return sb.append("]").toString();
95+
}
96+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package lesson21.homework;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* AIT-TR, cohort 42.1, Java Basic, hw #21
9+
*
10+
* @author Sergey Iryupin
11+
* @version 4-mar-24
12+
*/
13+
public class RubberArrayTest {
14+
15+
private RubberArray rubberArray;
16+
17+
@BeforeEach
18+
public void init() {
19+
rubberArray = new RubberArray();
20+
}
21+
22+
@Test
23+
public void testGet() {
24+
rubberArray.add(12);
25+
Assertions.assertEquals(12, rubberArray.get(0));
26+
}
27+
28+
@Test
29+
public void testAdd() {
30+
Assertions.assertEquals(0,rubberArray.size());
31+
rubberArray.add(25);
32+
Assertions.assertEquals(1, rubberArray.size());
33+
Assertions.assertEquals(25, rubberArray.get(0));
34+
}
35+
36+
@Test
37+
public void testAddByIndex() {
38+
rubberArray.add(25);
39+
rubberArray.add(25);
40+
rubberArray.add(7, 1);
41+
Assertions.assertEquals(3, rubberArray.size());
42+
Assertions.assertEquals(7, rubberArray.get(1));
43+
}
44+
45+
@Test
46+
public void testRemove() {
47+
rubberArray.add(-1);
48+
rubberArray.add(3);
49+
Assertions.assertEquals(2, rubberArray.size());
50+
rubberArray.remove(1);
51+
Assertions.assertEquals(1, rubberArray.size());
52+
}
53+
54+
@Test
55+
public void testContains() {
56+
rubberArray.add(-1);
57+
rubberArray.add(3);
58+
Assertions.assertTrue(rubberArray.contains(-1));
59+
Assertions.assertFalse(rubberArray.contains(5));
60+
}
61+
62+
@Test
63+
public void testIndexOf() {
64+
rubberArray.add(-1);
65+
rubberArray.add(3);
66+
Assertions.assertEquals(1, rubberArray.indexOf(3));
67+
Assertions.assertEquals(-1, rubberArray.indexOf(5));
68+
}
69+
70+
@Test
71+
public void testToString() {
72+
rubberArray.add(-1);
73+
rubberArray.add(3);
74+
Assertions.assertEquals("[-1, 3]", rubberArray.toString());
75+
}
76+
}

0 commit comments

Comments
 (0)