Skip to content

Commit 255409c

Browse files
committed
First commit
0 parents  commit 255409c

File tree

24 files changed

+1012
-0
lines changed

24 files changed

+1012
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
**/*.iml
3+
**/target

arrayWrapper/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-core-base-exercises</artifactId>
7+
<groupId>bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>arrayLongWrapper</artifactId>
13+
14+
15+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.mathodcoast;
2+
3+
public interface ArrayLongWrapper {
4+
5+
int add(long element);
6+
7+
void add(int index, long element);
8+
9+
long get(int index);
10+
11+
void set(int index, long element);
12+
13+
void remove(int index);
14+
15+
int find(long element);
16+
17+
long[] toArray();
18+
19+
String toString();
20+
21+
int size();
22+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.mathodcoast;
2+
3+
import java.util.Arrays;
4+
import java.util.Objects;
5+
6+
/**
7+
* {@link ArrayLongWrapperImpl} is an implementation of {@link ArrayLongWrapper} interface. It is a data structure based
8+
* on array.
9+
* */
10+
public class ArrayLongWrapperImpl implements ArrayLongWrapper {
11+
12+
private long[] dataArray;
13+
private int size;
14+
15+
public ArrayLongWrapperImpl(int buffer) {
16+
dataArray = new long[buffer];
17+
}
18+
19+
public ArrayLongWrapperImpl() {
20+
dataArray = new long[5];
21+
}
22+
23+
public static ArrayLongWrapper of(long... elements) {
24+
ArrayLongWrapper arrayLongWrapper = new ArrayLongWrapperImpl(elements.length);
25+
for(long element: elements){
26+
arrayLongWrapper.add(element);
27+
}
28+
return arrayLongWrapper;
29+
}
30+
31+
@Override
32+
public int add(long element) {
33+
increaseDataArrayIfFull();
34+
dataArray[size] = element;
35+
return size++;
36+
}
37+
38+
private void increaseDataArrayIfFull() {
39+
if (dataArray.length <= size) {
40+
dataArray = getTrimmedArrayToSize(dataArray.length*2);
41+
}
42+
}
43+
44+
@Override
45+
public void add(int index,long element) {
46+
increaseDataArrayIfFull();
47+
System.arraycopy(dataArray, index, dataArray, index + 1, size - index);
48+
dataArray[index] = element;
49+
size++;
50+
}
51+
52+
@Override
53+
public long get(int index) {
54+
Objects.checkIndex(index,size);
55+
return dataArray[index];
56+
}
57+
58+
@Override
59+
public void set(int index,long element) {
60+
Objects.checkIndex(index,size);
61+
dataArray[index] = element;
62+
}
63+
64+
@Override
65+
public void remove(int index) {
66+
if (index == size - 1){
67+
dataArray = getTrimmedArrayToSize(size - 1);
68+
}else {
69+
System.arraycopy(dataArray,index + 1,dataArray,index,size - index - 1);
70+
}
71+
size--;
72+
}
73+
74+
@Override
75+
public int find(long element) {
76+
int i = 0;
77+
int index = -1;
78+
while(i < size){
79+
if (dataArray[i] == element) {
80+
index = i;
81+
break;
82+
}
83+
i++;
84+
}
85+
return index;
86+
}
87+
88+
@Override
89+
public long[] toArray() {
90+
return getTrimmedArrayToSize(size);
91+
}
92+
93+
private long[] getTrimmedArrayToSize(int size) {
94+
return Arrays.copyOf(dataArray,size);
95+
}
96+
97+
@Override
98+
public String toString() {
99+
return Arrays.toString(getTrimmedArrayToSize(size));
100+
}
101+
102+
@Override
103+
public int size() {
104+
return size;
105+
}
106+
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package com.mathodcoast;
2+
3+
import org.junit.Test;
4+
import org.junit.internal.runners.statements.ExpectException;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.JUnit4;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
@RunWith( JUnit4.class )
13+
public class ArrayLongWrapperTest {
14+
15+
private ArrayLongWrapper arrayLongWrapper = new ArrayLongWrapperImpl();
16+
17+
@Test
18+
public void testAddElementIntoEmptyArray(){
19+
20+
arrayLongWrapper.add(10);
21+
22+
long getA = arrayLongWrapper.get(0);
23+
24+
assertEquals(10,getA);
25+
}
26+
27+
@Test
28+
public void testAddElements(){
29+
ArrayLongWrapper arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78);
30+
31+
int size = arrayLongWrapper.size();
32+
33+
assertEquals(15,arrayLongWrapper.get(0));
34+
assertEquals(69,arrayLongWrapper.get(1));
35+
assertEquals(58,arrayLongWrapper.get(2));
36+
assertEquals(78,arrayLongWrapper.get(3));
37+
38+
assertEquals(4,size);
39+
}
40+
41+
@Test
42+
public void testAddElementToFullArray(){
43+
ArrayLongWrapper arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,68);
44+
arrayLongWrapper.add(89);
45+
46+
int size = arrayLongWrapper.size();
47+
48+
assertEquals(89, arrayLongWrapper.get(5));
49+
assertEquals(6,size);
50+
}
51+
52+
@Test
53+
public void testAddElementByIndex(){
54+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,68);
55+
56+
arrayLongWrapper.add(50);
57+
arrayLongWrapper.add(2,10);
58+
int size = arrayLongWrapper.size();
59+
60+
assertEquals(10, arrayLongWrapper.get(2));
61+
assertEquals(68, arrayLongWrapper.get(5));
62+
assertEquals(7,size);
63+
}
64+
65+
@Test
66+
public void testGetElementsByIndex(){
67+
arrayLongWrapper.add(10);
68+
arrayLongWrapper.add(15);
69+
arrayLongWrapper.add(20);
70+
71+
long getA = arrayLongWrapper.get(0);
72+
long getB = arrayLongWrapper.get(1);
73+
long getC = arrayLongWrapper.get(2);
74+
75+
int size = arrayLongWrapper.size();
76+
77+
assertEquals(10,getA);
78+
assertEquals(15,getB);
79+
assertEquals(20,getC);
80+
81+
assertEquals(3,size);
82+
}
83+
84+
@Test( expected = IndexOutOfBoundsException.class)
85+
public void testGetFirstElementFromEmptyArray(){
86+
arrayLongWrapper.get(0);
87+
}
88+
89+
@Test( expected = IndexOutOfBoundsException.class)
90+
public void testGetElementByNegativeIndex(){
91+
arrayLongWrapper.get(-1);
92+
}
93+
94+
@Test( expected = IndexOutOfBoundsException.class)
95+
public void testGetElementByIndexEqualsToArraySize(){
96+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78);
97+
arrayLongWrapper.get(4);
98+
}
99+
100+
@Test
101+
public void testSetElementByIndex(){
102+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78);
103+
104+
arrayLongWrapper.set(2,10);
105+
106+
assertEquals(10,arrayLongWrapper.get(2));
107+
assertEquals(78,arrayLongWrapper.get(3));
108+
assertEquals(4,arrayLongWrapper.size());
109+
}
110+
111+
@Test(expected = IndexOutOfBoundsException.class)
112+
public void testSetElementByIndexEqualToSize(){
113+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78);
114+
115+
arrayLongWrapper.set(4,10);
116+
}
117+
118+
@Test
119+
public void testRemoveElementByIndex(){
120+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,100);
121+
122+
arrayLongWrapper.remove(2);
123+
124+
assertEquals(78,arrayLongWrapper.get(2));
125+
assertEquals(69,arrayLongWrapper.get(1));
126+
assertEquals(4,arrayLongWrapper.size());
127+
}
128+
129+
@Test(expected = IndexOutOfBoundsException.class)
130+
public void testRemoveElementByIndexEqualToSize(){
131+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78);
132+
133+
arrayLongWrapper.remove(4);
134+
}
135+
136+
@Test(expected = IndexOutOfBoundsException.class)
137+
public void testRemoveLastElementByIndex(){
138+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,100);
139+
140+
arrayLongWrapper.remove(4);
141+
142+
assertEquals(78,arrayLongWrapper.get(3));
143+
assertEquals(4,arrayLongWrapper.size());
144+
arrayLongWrapper.get(4);
145+
146+
System.out.println(arrayLongWrapper.toString());
147+
}
148+
149+
@Test(expected = IndexOutOfBoundsException.class)
150+
public void testRemoveElementByIndexOutOfBounds(){
151+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,100);
152+
153+
arrayLongWrapper.remove(6);
154+
}
155+
156+
@Test
157+
public void testFindIndexOfElement(){
158+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,100);
159+
160+
int index = arrayLongWrapper.find(58);
161+
assertEquals(2,index);
162+
}
163+
164+
@Test
165+
public void testFindNotExistingElement(){
166+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,100);
167+
168+
int index = arrayLongWrapper.find(60);
169+
assertEquals(-1,index);
170+
}
171+
172+
@Test
173+
public void testToArray(){
174+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,100);
175+
arrayLongWrapper.add(265);
176+
177+
long[] result = arrayLongWrapper.toArray();
178+
long[] expected = {15,69,58,78,100,265};
179+
180+
assertEquals(Arrays.toString(expected),Arrays.toString(result));
181+
}
182+
183+
@Test
184+
public void testToString(){
185+
arrayLongWrapper = ArrayLongWrapperImpl.of(15,69,58,78,100);
186+
arrayLongWrapper.add(265);
187+
188+
String result = arrayLongWrapper.toString();
189+
long[] expected = {15,69,58,78,100,265};
190+
191+
assertEquals(Arrays.toString(expected),result);
192+
}
193+
194+
@Test
195+
public void testSize(){
196+
arrayLongWrapper.add(10);
197+
arrayLongWrapper.add(15);
198+
arrayLongWrapper.add(20);
199+
200+
int size = arrayLongWrapper.size();
201+
202+
assertEquals(3,size);
203+
}
204+
205+
@Test
206+
public void testSizeOfEmptyArrayWrapper(){
207+
assertEquals(0,arrayLongWrapper.size());
208+
}
209+
}

arrays/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-core-base-exercises</artifactId>
7+
<groupId>bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>arrays</artifactId>
13+
14+
15+
</project>

0 commit comments

Comments
 (0)