-
Notifications
You must be signed in to change notification settings - Fork 7
Taks 1 and 2 done. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package school.lemon.changerequest.java.generics.container; | ||
|
|
||
| /** | ||
| * Created by Yaroslav Pavlinskiy on 26.12.2016. | ||
| */ | ||
| public interface Container<E> { | ||
| int INITIAL_ARRAY_SIZE = 10; | ||
|
|
||
| /** | ||
| * Get current size of container | ||
| * @return current size of container | ||
| */ | ||
| int size(); | ||
|
|
||
| /** | ||
| * Clear container | ||
| */ | ||
| void clear(); | ||
|
|
||
| /** | ||
| * Get element by {@code index} | ||
| * @param index of the element | ||
| * @return element at specified {@code index} or {@code null} in case {@code index < 0 || index > size} | ||
| */ | ||
| E get(int index); | ||
|
|
||
| /** | ||
| * Add element at the end of container | ||
| * @param element to add | ||
| */ | ||
| void add(E element); | ||
|
|
||
| /** | ||
| * Add element at specified {@code index} of container | ||
| * In case {@code index < 0 || index > size} - do not add element. | ||
| * @param element to add | ||
| * @param index of the element | ||
| * @return true if element was successfully added, otherwise - false. | ||
| */ | ||
| boolean add(E element,int index); | ||
|
|
||
| /** | ||
| * Removes element in specified index | ||
| * @param index of the element | ||
| * @return true if element was successfully deleted, otherwise - false. | ||
| */ | ||
| boolean remove(int index); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package school.lemon.changerequest.java.generics.container; | ||
|
|
||
| /** | ||
| * Created by Yaroslav Pavlinskiy on 26.12.2016. | ||
| */ | ||
| public class ContainerImpl<E> implements Container<E>{ | ||
| private int size; | ||
| private E[] array; | ||
|
|
||
| ContainerImpl() | ||
| { | ||
| this.array = (E[]) new Object[10]; | ||
| } | ||
|
|
||
| public int size() | ||
| { | ||
| return this.size; | ||
| } | ||
|
|
||
| public void clear() | ||
| { | ||
| this.size = 0; | ||
| } | ||
|
|
||
| public void add(E element) | ||
| { | ||
| checkSize(); | ||
| array[size++] = element; | ||
| } | ||
|
|
||
| public E get(int index) | ||
| { | ||
| if(index < 0 || index >= size()) | ||
| return null; | ||
| return array[index]; | ||
| } | ||
|
|
||
| public boolean add(E element, int I) | ||
| { | ||
| if (I < 0 || I > size) | ||
| return false; | ||
| checkSize(); | ||
| System.arraycopy(array, I, array, I + 1, size - I); | ||
| array[I] = element; | ||
| ++size; | ||
| return true; | ||
| } | ||
|
|
||
| public boolean remove(int index) { | ||
| if (index < 0 || index >= size) | ||
| return false; | ||
| System.arraycopy(array, index + 1, array, index, size - index - 1); | ||
| --size; | ||
| return true; | ||
| } | ||
|
|
||
| private void checkSize() | ||
| { | ||
| if(size == array.length) | ||
| { | ||
| E[] newArray = (E[]) new Object[array.length * 2]; | ||
| System.arraycopy(array,0,newArray,0,array.length); | ||
| array = newArray; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package school.lemon.changerequest.java.generics.pair; | ||
|
|
||
| /** | ||
| * Created by Yaroslav Pavlinskiy on 22.12.2016. | ||
| */ | ||
| public class Pair <K,V> { | ||
| protected V value; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need fields to be |
||
| protected K key; | ||
|
|
||
| public Pair(K key,V value) { | ||
| this.value = value; | ||
| this.key = key; | ||
| } | ||
|
|
||
| public V getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public void setValue(V value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public K getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| public void setKey(K key) { | ||
| this.key = key; | ||
| } | ||
|
|
||
| public boolean contains(K key) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is quite weird, cause it not really check whether given pair |
||
| { | ||
| if(this.getKey() == key) | ||
| return true; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package school.lemon.changerequest.java.generics.pair; | ||
|
|
||
| import java.lang.reflect.Array; | ||
| import java.util.Comparator; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Created by Yaroslav Pavlinskiy on 22.12.2016. | ||
| */ | ||
| public final class PairUtils | ||
| { | ||
| public static <K,V> boolean equals(Pair<K,V> a,Pair<K,V> b) | ||
| { | ||
| if(a == b) | ||
| return true; | ||
| if(a == null || b == null) | ||
| return false; | ||
| if(a.getKey() == b.getKey() && a.getValue() == b.getValue()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use |
||
| return true; | ||
| return false; | ||
| } | ||
|
|
||
| public static <K extends Comparable<K>,V> int compareTo(Pair<K,V> a,Pair<K,V> b) | ||
| { | ||
| return a.getKey().compareTo(b.getKey()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about NullPointerException checks ? (Please add them for all methods) |
||
| } | ||
|
|
||
| public static <K,V> V[] getValue(Pair<K,V>[] a) | ||
| { | ||
| V[] arrayOfValue = (V[]) new Object[a.length]; | ||
| for(int i = 0; i < a.length; i++) | ||
| { | ||
| arrayOfValue[i] = (V) a[i].getValue(); | ||
| } | ||
| return arrayOfValue; | ||
| } | ||
|
|
||
| public static <K,V> K[] getKey(Pair<K,V>[] a) | ||
| { | ||
| K[] arrayOfKey = (K[]) new Object[a.length]; | ||
| for(int i = 0; i < a.length; i++) | ||
| { | ||
| arrayOfKey[i] = (K) a[i].getKey(); | ||
| } | ||
| return arrayOfKey; | ||
| } | ||
|
|
||
| public static <K extends Comparable<K>,V> int countGreaterThan (Pair<K,V>[] a, K key) | ||
| { | ||
| int count = 0; | ||
| for(int i = 0; i < a.length; i++) | ||
| { | ||
| if(key.compareTo(a[i].getKey()) == -1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CompareTo can return any negative number, not only |
||
| count++; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| public static <K,V> boolean containsUniqueObjects(Pair<K,V>[] a) | ||
| { | ||
| for(int i = 0; i < a.length; i++) | ||
| { | ||
| for(int j = i+1; j < a.length; j++) | ||
| { | ||
| if(a[i].getKey().equals(a[j].getKey())) | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package school.lemon.changerequest.java.generics.pair; | ||
|
|
||
| import java.lang.reflect.Array; | ||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * Created by Yaroslav Pavlinskiy on 22.12.2016. | ||
| */ | ||
| public class TestingApp { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your Demo class should provide demo for all possible operations with 2 or 3 different pair types ( for example: <String,Integer>, <Double, Float>, <Integer, Container>) |
||
| public static void main(String[] args) { | ||
| Pair p1 = new Pair<>(1,"k1"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your pairs should be initialized with some Generics, you should not use raw notation, so please, use smth like: Pair<Integer,String> p1 = new Pair<>(1,"k1"); |
||
| Pair p2 = new Pair<>(2,"k2"); | ||
| Pair p4 = new Pair<>(3,"k3"); | ||
|
|
||
| Pair[] pA = new Pair[3]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for array. you shouldn't use raw types. |
||
| pA[0] = p1; | ||
| pA[1] = p2; | ||
| pA[2] = p4; | ||
| //Test for compareTo | ||
| System.out.println(PairUtils.compareTo(p1,p2)); | ||
| //Test for getValue | ||
| System.out.println("Value: "+Arrays.asList(PairUtils.getValue(pA))); | ||
| //Test for getKey | ||
| System.out.println("Key: "+Arrays.asList(PairUtils.getKey(pA))); | ||
| //Test for countGraterThan | ||
| System.out.println(PairUtils.countGreaterThan(pA,1)); | ||
| //Test for check unique objects | ||
| System.out.println(PairUtils.containsUniqueObjects(pA)); | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that such class cast will work. You cannot cast
Objectto TypeE[].Please provide Demo class for Container to check whether your implementation work correctly.
Provide demo for all operations for some 2 different Generic type, for example:
StringandFloat.