Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Member

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:

Create class container the same as for https://github.com/ChangeRequest/oop.hw1 but instead of Integer it should work with elements of any type.

ArrayList<T> container= new ArrayList<T>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use: List<T> container = new ArrayList<T>(); - in this way you'll use more generic type - List and if you'll need to change implementation to any other type, that implements List interface - it'll be easy for you. (It's common practice to use the most generic type you could.

public Container (){
Copy link
Member

Choose a reason for hiding this comment

The 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){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method size should return size of your underlying implementation (in your case size of variable container, but not some container that you receive).

if(e==null)throw new NullPointerException();
return container.size();
}
public void clear(Container e){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for clear as for size

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double not Duble



@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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your NumberGenerator should use Generics and have some type declarations (i.e. NumberGenerator<T>)

abstract Number[] NumberGenerator();
Copy link
Member

Choose a reason for hiding this comment

The 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 generateNumbers.
It should have public visibility and should return some generic type, declared with class declaration (some T)


}
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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your implementation do not fit the requirements:

Create class Printer, which contains field NumberGenerator and method generateAndPrint. It should generate numbers and print their sum.



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) {
Copy link
Member

Choose a reason for hiding this comment

The 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> {
Copy link
Member

Choose a reason for hiding this comment

The 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 final, it should not have any fields or constructors(it could only have private constructor without parameters).

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) {
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should only compare keys. And key should implement Comparable interface. Read the docs again:

  1. compareTo - compare two pairs. Key types should implement java.lang.Comparable;

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your parameter P should be the same as Key of Pair:

  1. countGreaterThan - count pairs with keys greater than specified parameter. Key type and specified parameter type should be the same;

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();
}

}
Empty file.
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create @Before fixture and re-initialize container before each test to be sure, that there won't be any side-effects (like you'll add value in one test and it will be in available in other test)


@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 {
Copy link
Member

Choose a reason for hiding this comment

The 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: DoubleGeneratorTest

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 {
Copy link
Member

Choose a reason for hiding this comment

The 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(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the better method name will be: shouldGenerateNumberOfIntegerType

Integer[] test=new Integer[]{1,2,3,4,5,6,7,8,9,10};
Assert.assertEquals(generator.get(generator,1).getClass(),test[1].getClass());
}
}
Loading