-
Notifications
You must be signed in to change notification settings - Fork 7
в Pairs тесте вылетает ошибка хз почему #3
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,36 @@ | ||
| package school.lemon.changerequest.java.generics.container; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedList; | ||
|
|
||
| /** | ||
| * Created by User on 02.01.2017. | ||
| */ | ||
| public class Container <T> { | ||
| ArrayList<T> container= new ArrayList<T>(); | ||
|
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. Use: |
||
| public Container (){ | ||
|
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. If you don't make any initialization in default constructor - remove it from code - it'll be generated automatically. |
||
| } | ||
| public int size(Container e){ | ||
|
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. Method |
||
| if(e==null)throw new NullPointerException(); | ||
| return container.size(); | ||
| } | ||
| public void clear(Container e){ | ||
|
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 |
||
| if(e==null)throw new NullPointerException(); | ||
| container.clear(); | ||
| } | ||
| public void add(T element){ | ||
|
|
||
| container.add(element); | ||
| } | ||
| public T get(int index){ | ||
| return container.get(index); | ||
| } | ||
| public void add(T element, int index){ | ||
| container.add(index, element); | ||
| } | ||
| public void remove(int index){ | ||
| container.remove(index); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package school.lemon.changerequest.java.generics.numberGenerators; | ||
|
|
||
| /** | ||
| * Created by User on 02.01.2017. | ||
| */ | ||
| public class DubleGenerator extends NumberGenerator { | ||
|
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.
|
||
|
|
||
|
|
||
| @Override | ||
| public Number[] NumberGenerator() { | ||
| Double[] doublerGenerator=new Double[10]; | ||
| for(int i=0;i<doublerGenerator.length;i++){doublerGenerator[i]=(Double)(Math.random()*100);} | ||
|
|
||
| return doublerGenerator; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public int size(NumberGenerator e){ return 10;} | ||
| public Double get(NumberGenerator e, int index){ | ||
| return (Double) (e.NumberGenerator()[index]); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package school.lemon.changerequest.java.generics.numberGenerators; | ||
|
|
||
| /** | ||
| * Created by User on 02.01.2017. | ||
| */ | ||
| public class IntegerGenerator extends NumberGenerator { | ||
| @Override | ||
| public Number[] NumberGenerator() { | ||
| Integer[] integerGenerator=new Integer[10]; | ||
| for(int i=0;i<integerGenerator.length;i++){integerGenerator[i]=(int)(Math.random()*100);} | ||
| return integerGenerator; | ||
| } | ||
| public int size(NumberGenerator e){ return 10;} | ||
| public Integer get(NumberGenerator e, int index){ | ||
| return (Integer)(e.NumberGenerator()[index]); | ||
| }} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package school.lemon.changerequest.java.generics.numberGenerators; | ||
|
|
||
| /** | ||
| * Created by User on 02.01.2017. | ||
| */ | ||
| abstract class NumberGenerator { | ||
|
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 NumberGenerator should use Generics and have some type declarations (i.e. |
||
| abstract Number[] NumberGenerator(); | ||
|
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. method name should be in lowercase and regarding to your task documentation, it should be |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package school.lemon.changerequest.java.generics.numberGenerators; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Random; | ||
|
|
||
| /** | ||
| * Created by User on 02.01.2017. | ||
| */ | ||
| public class Printer<T> { | ||
|
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 implementation do not fit the requirements:
|
||
|
|
||
|
|
||
| public void generateAndPrint() { | ||
| int numberOfMethods = 2; | ||
| double randomNumbers = Math.random() * numberOfMethods; | ||
| if (randomNumbers < 1) { | ||
| DubleGenerator dubleGeneratorForPrint = new DubleGenerator(); | ||
| System.out.println(toString(dubleGeneratorForPrint.NumberGenerator())); | ||
| } | ||
| if (randomNumbers > 1&&randomNumbers < 2) { | ||
| IntegerGenerator intGeneratorForPrint = new IntegerGenerator(); | ||
| System.out.println(toString(intGeneratorForPrint.NumberGenerator())); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public String toString(Number[] n) { | ||
| StringBuilder sb = new StringBuilder( ); | ||
| if(n instanceof Double[]){sb.append("Doubles:[");} | ||
| if(n instanceof Integer[]){sb.append("Integers:[");} | ||
| for (int i = 0; i < n.length; i++) { | ||
| sb.append(n[i]); | ||
| sb.append(", "); | ||
| } | ||
| sb.append("]"); | ||
| return sb.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package school.lemon.changerequest.java.generics.numberGenerators; | ||
|
|
||
| /** | ||
| * Created by User on 03.01.2017. | ||
| */ | ||
| public class Test { | ||
| public static void main(String[] args) { | ||
| Printer test=new Printer(); | ||
| int counter=10; | ||
|
|
||
| test.generateAndPrint(); | ||
| test.generateAndPrint(); | ||
| test.generateAndPrint(); | ||
| test.generateAndPrint(); | ||
| test.generateAndPrint(); | ||
| test.generateAndPrint(); | ||
| test.generateAndPrint(); | ||
| test.generateAndPrint(); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package school.lemon.changerequest.java.generics.pairUtils; | ||
|
|
||
| /** | ||
| * Created by User on 01.01.2017. | ||
| */ | ||
| public class Pair<K,V> { | ||
| private K key; | ||
| private V value; | ||
| public Pair (K key, 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 don't you assign values from constructor arguments to class fields? It should be smth like: 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,84 @@ | ||
| package school.lemon.changerequest.java.generics.pairUtils; | ||
|
|
||
| import java.util.HashSet; | ||
|
|
||
| /** | ||
| * Created by User on 01.01.2017. | ||
| */ | ||
| public class PairUtils<K, V> { | ||
|
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 class should be an utility class. It should not have any types (it's not generic class), it should be |
||
| private K key; | ||
| private V value; | ||
|
|
||
| public PairUtils(Pair a) { | ||
| this.key = (K) a.getKey(); | ||
| this.value = (V) a.getValue(); | ||
| } | ||
|
|
||
|
|
||
| public static boolean equals(Pair a, Pair b) { | ||
|
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 always use type definitions, i.e.: public static <K,V> boolean equals(Pair<K,V> a, Pair<K,V> b)This comment applies to all your methods definitions |
||
| if (a.getClass() == b.getClass()) return false; | ||
| if (a instanceof Pair && b instanceof Pair) { | ||
| if (!(a.getValue().getClass() == b.getValue().getClass())) return false; | ||
| if (!(a.getKey().getClass() == b.getKey().getClass())) return false; | ||
| if (!(a.getKey() == b.getKey())) return false; | ||
| if (!(a.getValue() == b.getValue())) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| public static int compareTo(Pair a, Pair b) { | ||
|
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 only compare keys. And key should implement
|
||
| if (a.getClass() == b.getClass()) throw new ClassCastException(); | ||
| if (a instanceof Pair && b instanceof Pair) { | ||
| if (!(a.getValue().getClass() == b.getValue().getClass())) throw new ClassCastException(); | ||
| if (!(a.getKey().getClass() == b.getKey().getClass())) throw new ClassCastException(); | ||
| if (a.getValue() == null | b.getValue() == null) throw new NullPointerException(); | ||
| if (a.getKey() == null | b.getKey() == null) throw new NullPointerException(); | ||
|
|
||
|
|
||
| } | ||
| return (int) ((Pair) a).getValue() - (int) ((Pair) b).getValue(); | ||
| } | ||
|
|
||
| public static <V> V[] getValues(Pair[] a) { | ||
| V[] resultl = (V[]) new Object[a.length]; | ||
| for (int i = 0; i < a.length; i++) { | ||
| resultl[i] = (V) a[i].getValue(); | ||
| } | ||
| return resultl; | ||
| } | ||
|
|
||
| public static <V> V[] getKeys(Pair[] a) { | ||
| V[] resultl = (V[]) new Object[a.length]; | ||
| for (int i = 0; i < a.length; i++) { | ||
| resultl[i] = (V) a[i].getKey(); | ||
| } | ||
| return resultl; | ||
| } | ||
|
|
||
| public static <P> int countGreaterThan(Pair[] a, P parameter) { | ||
|
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 parameter
|
||
| if (a == null | parameter == null) throw new NullPointerException(); | ||
| if (a.length == 0) return 0; | ||
| if (!(a[0].getKey().getClass() == parameter.getClass())) throw new ClassCastException(); | ||
| int counter = 0; | ||
| for (int i = 0; i < a.length; i++) { | ||
|
|
||
| if (((Integer) a[i].getKey() - (Integer) (parameter)) > 0) { | ||
| counter++; | ||
| } | ||
| } | ||
| return counter; | ||
| } | ||
| public static <V>int ContainsUniqueObjects(Pair[]a){ | ||
| if (a == null) throw new NullPointerException(); | ||
| if (a.length == 0) return 0; | ||
|
|
||
| HashSet<V> UniqueObjects= new HashSet<V>(); | ||
| for (int i=0;i<a.length;i++){ | ||
| UniqueObjects.add((V) a[i].getValue()); | ||
| } | ||
|
|
||
| return UniqueObjects.size(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package school.lemon.changerequest.java.generics.container; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Created by User on 03.01.2017. | ||
| */ | ||
| public class ContainerTest { | ||
| Container container = new Container(); | ||
|
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 create |
||
|
|
||
| @Test | ||
| public void testSizeWhenNothingAdded() { | ||
| int expected = 0; | ||
| int result = container.size(container); | ||
| Assert.assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSizeWhenSomethingAdded() { | ||
| container.add(1); | ||
| container.add(1); | ||
| int expected = 2; | ||
| int result = container.size(container); | ||
| Assert.assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test(expected = NullPointerException.class) | ||
| public void testSizeWhenContainerIsnotCreated() { | ||
| Container containerNotCreated = null; | ||
| containerNotCreated.size(containerNotCreated); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testClearShouldNotThrowExeption() { | ||
| container.clear(container); | ||
| } | ||
|
|
||
| @Test(expected = NullPointerException.class) | ||
| public void testClearShouldThrowExeption() { | ||
| container = null; | ||
| container.clear(container); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAddShouldAddNumberGiven() { | ||
| container.add(1); | ||
| int expected = 1; | ||
| int result = (int) container.get(0); | ||
| Assert.assertEquals(expected, result); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testAddShouldAddStringGiven() { | ||
| container.add("WHAY?"); | ||
| String expected = "WHAY?"; | ||
| String result = (String) container.get(0); | ||
| Assert.assertEquals(expected, result); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testGetShouldGetNumberGiven() { | ||
| container.add(1); | ||
| int expected = 1; | ||
| int result = (int) container.get(0); | ||
| Assert.assertEquals(expected, result); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testGetShouldGetStringGiven() { | ||
| container.add("WHAY?"); | ||
| String expected = "WHAY?"; | ||
| String result = (String) container.get(0); | ||
| Assert.assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void removeTestShouldRemoveObjectByIndex() { | ||
| container.add("WHAY?"); | ||
| container.remove(0); | ||
| int expected = 0; | ||
| int result = container.size(container); | ||
| Assert.assertEquals(expected, result); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package school.lemon.changerequest.java.generics.numberGenerators; | ||
|
|
||
| import org.junit.*; | ||
|
|
||
| /** | ||
| * Created by User on 03.01.2017. | ||
| */ | ||
| public class DoubleGeneratorTestUnit { | ||
|
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. There is no need to point that it's Unit test in class name, it should be just: |
||
| DubleGenerator generator= new DubleGenerator(); | ||
| private final int sizeOfGenerator=10; | ||
| @org.junit.Test | ||
| public void testSizeOfIntegerGenerator(){ | ||
| int expected =sizeOfGenerator; | ||
| int result= generator.size(generator); | ||
| Assert.assertEquals(expected,result); | ||
| } | ||
| @org.junit.Test | ||
| public void testWhatTypeOfNumberGeneratesShouldBeDouble(){ | ||
| Double[] test=new Double[]{1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0}; | ||
| Assert.assertEquals(generator.get(generator,1).getClass(),test[1].getClass()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package school.lemon.changerequest.java.generics.numberGenerators; | ||
|
|
||
| import org.junit.*; | ||
|
|
||
| /** | ||
| * Created by User on 03.01.2017. | ||
| */ | ||
| public class IntegerGeneratorTestUnit { | ||
|
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 class name |
||
| IntegerGenerator generator= new IntegerGenerator(); | ||
| private final int sizeOfGenerator=10; | ||
| @org.junit.Test | ||
| public void testSizeOfIntegerGenerator(){ | ||
| int expected =sizeOfGenerator; | ||
| int result= generator.size(generator); | ||
| Assert.assertEquals(expected,result); | ||
| } | ||
| @org.junit.Test | ||
| public void testWhatTypeOfNumberGeneratesShouldBeInteger(){ | ||
|
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. the better method name will be: |
||
| Integer[] test=new Integer[]{1,2,3,4,5,6,7,8,9,10}; | ||
| Assert.assertEquals(generator.get(generator,1).getClass(),test[1].getClass()); | ||
| } | ||
| } | ||
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.
You must not use Collections API in your Container implementation. Reuse your implementation from OOP homework: