-
Notifications
You must be signed in to change notification settings - Fork 7
generics.hw1 done #4
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,74 @@ | ||
| package school.lemon.changerequest.java.generics.Container; | ||
|
|
||
|
|
||
| public class Container<T> implements ContainerInterface<T> { | ||
|
|
||
| private T[] array; | ||
| private int size; | ||
|
|
||
| public Container() { | ||
| this.array = (T[]) new Object[INITIAL_ARRAY_SIZE]; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public int size() { | ||
| return size; | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| size = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public T get(int index) { | ||
| if (index < 0 || index >= size) { | ||
| return null; | ||
| } else return array[index]; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean add(T element, int index) { | ||
| if (index < 0 || index > size) return false; | ||
| checkSize(); | ||
| System.arraycopy(array, index, array, index + 1, size - index); | ||
| array[index] = element; | ||
| ++size; | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| 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; | ||
| } | ||
|
|
||
| @Override | ||
| public void add(T element) { | ||
| checkSize(); | ||
| array[size++] = element; | ||
|
|
||
| } | ||
|
|
||
| public void checkSize() { | ||
| if (size == array.length) { | ||
|
|
||
| T[] newArray = (T[]) new Object[array.length * 2]; | ||
| System.arraycopy(array, 0, newArray, 0, array.length); | ||
| array = newArray; | ||
| } | ||
| } | ||
|
|
||
| public String toString() { | ||
| StringBuilder containerToString = new StringBuilder(); | ||
| for (int i = 0; i < size; i++) { | ||
| containerToString.append(array[i]).append(" "); | ||
| } | ||
| return String.valueOf(containerToString); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package school.lemon.changerequest.java.generics.Container; | ||
|
|
||
|
|
||
| public class ContainerApp { | ||
| public static void main(String[] args) { | ||
|
|
||
| Container<Integer> container = new Container<>(); | ||
| container.add("word"); | ||
| container.add(234); | ||
| container.add("<some element>"); | ||
| container.add(.4432); | ||
| container.add(5); | ||
| container.add(6); | ||
| container.add(7); | ||
| container.add(8); | ||
| container.add(9); | ||
| container.add(10); | ||
| System.out.format("Add element in container: %s \n", container.toString()); | ||
| container.add("add"); | ||
| System.out.format("Add element in index bigger then size: %s \n", container.toString()); | ||
| System.out.format("Get element by index: %s \n", container.get(0)); | ||
| System.out.format("Get element by index bigger then size: %s \n", container.get(15)); | ||
| container.add(33, 11); | ||
| System.out.format("Add element by index: %s \n", container.toString()); | ||
| container.remove(3); | ||
| System.out.format("Remove element by index: %s \n", container.toString()); | ||
| container.clear(); | ||
| System.out.format("Clear container: %s \n", container.toString()); | ||
| System.out.format("Try to get element after clear container: %s \n", container.get(0)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package school.lemon.changerequest.java.generics.Container; | ||
|
|
||
|
|
||
| public interface ContainerInterface<T> { | ||
|
|
||
| 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} | ||
| */ | ||
| T get(int index); | ||
|
|
||
|
|
||
| /** | ||
| * 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(T 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); | ||
|
|
||
| public void add(T element); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| public class Demo { | ||
| public static void main(String[] args) { | ||
| DoubleGenerator doubleGenerator = new DoubleGenerator(); | ||
| Printer printer1 = new Printer(doubleGenerator); | ||
| IntegerGenerator integerGenerator = new IntegerGenerator(); | ||
| Printer printer2 = new Printer(integerGenerator); | ||
| printer1.generateAndPrint(); | ||
| printer2.generateAndPrint(); | ||
|
|
||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class DoubleGenerator extends NumberGenerator<Double> { | ||
|
|
||
|
|
||
| @Override | ||
| public Double[] generateNumbers() { | ||
| Double[] arrayDouble = new Double[DEFAULT_SIZE_TEN]; | ||
| for (int i = 0; i < DEFAULT_SIZE_TEN; i++) { | ||
| arrayDouble[i] = new Random().nextDouble(); | ||
| } | ||
| return arrayDouble; | ||
| } | ||
|
|
||
| @Override | ||
| public DoubleSumCalculator getSumCalculator() { | ||
| return new DoubleSumCalculator(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| public class DoubleSumCalculator extends SumCalculator<Double> { | ||
| @Override | ||
| public Double calculateSum(Double[] array) { | ||
| Double sum = .0; | ||
| for (Double anArray : array) { | ||
| sum += anArray; | ||
| } | ||
| return sum; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class IntegerGenerator extends NumberGenerator<Integer> { | ||
|
|
||
| @Override | ||
| public Integer[] generateNumbers() { | ||
| Integer[] arrayInt = new Integer[DEFAULT_SIZE_TEN]; | ||
| for (int i = 0; i < DEFAULT_SIZE_TEN; i++) { | ||
| arrayInt[i] = new Random().nextInt(); | ||
| } | ||
| return arrayInt; | ||
| } | ||
|
|
||
| @Override | ||
| public IntegerSumCalculator getSumCalculator() { | ||
| return new IntegerSumCalculator(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| public class IntegerSumCalculator extends SumCalculator<Integer> { | ||
| @Override | ||
| public Integer calculateSum(Integer[] array) { | ||
| Integer sum = 0; | ||
| for (Integer anArray : array) { | ||
| sum += anArray; | ||
| } | ||
| return sum; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| public abstract class NumberGenerator<T extends Number> { | ||
| static int DEFAULT_SIZE_TEN = 10; | ||
|
|
||
| public abstract T[] generateNumbers(); | ||
|
|
||
| abstract SumCalculator getSumCalculator(); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| public class Printer<T extends Number> { | ||
| NumberGenerator<T> numberGenerator; | ||
|
Contributor
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. Make these fields |
||
| SumCalculator<T> sum; | ||
|
|
||
| public Printer(NumberGenerator<T> numberGenerator) { | ||
| this.numberGenerator = numberGenerator; | ||
| if (numberGenerator instanceof IntegerGenerator) { | ||
|
Contributor
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 receive it from generator. |
||
| this.sum = (SumCalculator<T>) new IntegerSumCalculator(); | ||
| } else { | ||
| this.sum = (SumCalculator<T>) new DoubleSumCalculator(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public void generateAndPrint() { | ||
| T[] arrayNumbers = numberGenerator.generateNumbers(); | ||
| numberGenerator.getSumCalculator(); | ||
|
Contributor
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 call this method here? Move it to constructor. |
||
| System.out.println(sum.calculateSum(arrayNumbers)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package school.lemon.changerequest.java.generics.Generator; | ||
|
|
||
| public abstract class SumCalculator<T extends Number> { | ||
|
|
||
| public abstract T calculateSum(T[] array); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package school.lemon.changerequest.java.generics.Pairs; | ||
|
|
||
| /** | ||
| * Created by lbrdev on 21.02.2017. | ||
| * Project: generics.hw1 | ||
| */ | ||
| public class Pair<K, V> { | ||
|
|
||
| public K key; | ||
| public V value; | ||
|
|
||
| public Pair(K key, V value) { | ||
| this.key = key; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public K getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| public V getValue() { | ||
| return value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package school.lemon.changerequest.java.generics.Pairs; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * Created by lbrdev on 21.02.2017. | ||
| * Project: generics.hw1 | ||
| */ | ||
| public class PairApp { | ||
|
|
||
| public static void main(String[] args) { | ||
| Pair<Integer, String> pair1 = new Pair<>(1, "Key1"); | ||
| Pair<Integer, String> pair2 = new Pair<>(1, "Key1"); | ||
| Pair<Integer, String> pair3 = new Pair<>(3, "Key2"); | ||
| Pair<Integer, String> pair4 = new Pair<>(4, "Key3"); | ||
| Pair[] pairs = new Pair[4]; | ||
| pairs[0] = pair1; | ||
| pairs[1] = pair2; | ||
| pairs[2] = pair3; | ||
| pairs[3] = pair4; | ||
| System.out.println(PairUtils.compareTo(pair1, pair2)); | ||
| System.out.println(PairUtils.compareTo(pair1, pair3)); | ||
| System.out.println(PairUtils.compareTo(pair4, pair2)); | ||
| System.out.println(PairUtils.equals(pair1, pair2)); | ||
| System.out.println(PairUtils.equals(pair1, pair3)); | ||
| System.out.println(Arrays.asList(PairUtils.getValues(pairs))); | ||
| System.out.println(Arrays.asList(PairUtils.getKeys(pairs))); | ||
| System.out.println(PairUtils.countGreaterThan(pairs, 1)); | ||
| System.out.println(PairUtils.countGreaterThan(pairs, 0)); | ||
| System.out.println(PairUtils.containsUniqueObjects(pairs)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package school.lemon.changerequest.java.generics.Pairs; | ||
|
|
||
| /** | ||
| * Created by lbrdev on 21.02.2017. | ||
| * Project: generics.hw1 | ||
| */ | ||
| public final class PairUtils { | ||
|
|
||
| public static <K, V> boolean equals(Pair<K, V> pair1, Pair<K, V> pair2) { | ||
| return pair1.getKey().equals(pair2.getKey()) && pair1.getValue().equals(pair2.getValue()); | ||
| } | ||
|
|
||
| public static <K extends Comparable<K>, V> int compareTo(Pair<K, V> pair1, Pair<K, V> pair2) { | ||
| return pair1.getKey().compareTo(pair2.getKey()); | ||
| } | ||
|
|
||
| public static <K, V> V[] getValues(Pair<K, V>[] pairs) { | ||
| V[] valuesArray = (V[]) new Object[pairs.length]; | ||
| for (int i = 0; i < pairs.length; i++) { | ||
| valuesArray[i] = pairs[i].getValue(); | ||
| } | ||
| return valuesArray; | ||
| } | ||
|
|
||
| public static <K, V> K[] getKeys(Pair<K, V>[] pairs) { | ||
| K[] keysArray = (K[]) new Object[pairs.length]; | ||
| for (int i = 0; i < pairs.length; i++) { | ||
| keysArray[i] = pairs[i].getKey(); | ||
| } | ||
| return keysArray; | ||
| } | ||
|
|
||
| public static <K extends Comparable<K>, V> int countGreaterThan(Pair<K, V>[] pairs, K parameter) { | ||
| int counter = 0; | ||
| for (Pair<K, V> pair : pairs) { | ||
| if (parameter.compareTo(pair.getKey()) == 1) { | ||
| counter++; | ||
| } | ||
| } | ||
| return counter; | ||
| } | ||
|
|
||
| public static <K, V> boolean containsUniqueObjects(Pair<K, V>[] pairs) { | ||
| for (int a = 0; a < pairs.length; a++ ){ | ||
|
Contributor
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.
|
||
| for (int i = a + 1; i < pairs.length; i++) { | ||
| if (pairs[a].getKey().equals(pairs[i].getKey())) return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } | ||
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.
protectedorpublicis preferred.