diff --git a/.checkstyle b/.checkstyle new file mode 100644 index 00000000..5783bc0d --- /dev/null +++ b/.checkstyle @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java b/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java index 0db25d25..a7693715 100644 --- a/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java +++ b/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.github.pedrovgs.binarytree; /** @@ -63,14 +64,24 @@ public boolean hasRight() { } @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof BinaryNode)) return false; + if (this == o) { + return true; + } + if (!(o instanceof BinaryNode)) { + return false; + } BinaryNode that = (BinaryNode) o; - if (!data.equals(that.data)) return false; - if (left != null ? !left.equals(that.left) : that.left != null) return false; - if (right != null ? !right.equals(that.right) : that.right != null) return false; + if (!data.equals(that.data)) { + return false; + } + if (left != null ? !left.equals(that.left) : that.left != null) { + return false; + } + if (right != null ? !right.equals(that.right) : that.right != null) { + return false; + } return true; } diff --git a/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java b/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java new file mode 100644 index 00000000..522b6ab6 --- /dev/null +++ b/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.pedrovgs.equations; + +/** + * This class contains an algorithm that solves 2nd grade equations. + * @author ShoeMaker + * + */ + +public class Grade2Equation { + + /** + * Method solves 2nd grade equations. + * @param a from a*x^2 + * @param b from b*x + * @param c the fixed number + * @return An array with the solution/solutions of the equation. + * @throws IllegalArgumentException there is no Real solution to the equation. + */ + public Double[] solve(double a, double b, double c) { + + double d = (b * b) - (4 * a * c); + if (d < 0) { + throw new IllegalArgumentException("There is no Real solution to this equation."); + } + Double[] x = new Double[2]; + x[0] = (-b + Math.sqrt(d)) / (2 * a); + x[1] = (-b - Math.sqrt(d)) / (2 * a); + return x; + } +} \ No newline at end of file diff --git a/src/main/java/com/github/pedrovgs/files/TxtToList.java b/src/main/java/com/github/pedrovgs/files/TxtToList.java new file mode 100644 index 00000000..17215079 --- /dev/null +++ b/src/main/java/com/github/pedrovgs/files/TxtToList.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.pedrovgs.files; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + +/** + * This class contains an algorithm that reads txt files and turns them to Lists for further use. + * @author ShoeMaker + * + */ + +public class TxtToList { + + /** + * Method gets a txt file's path. It opens the file and reads it, + * then tries to get the txt lines and return them as a List. + * @param filepath The file's path. + * @return A List with the file's lines. + * @throws IllegalArgumentException when an IOException occurs or the file is empty. + */ + public List readFileToList(String filepath) { + + List datas = new ArrayList(); + BufferedReader br = null; + String line = null; + try { + br = new BufferedReader(new FileReader(filepath)); + line = br.readLine(); + while (line != null) { + datas.add(line); + line = br.readLine(); + } + br.close(); + } catch (IOException e) { + throw new IllegalArgumentException("Something went wrong. Wrong path or file doesn't exist."); + } + if (datas.size() == 0) { + throw new IllegalArgumentException("File was empty."); + } + return datas; + } +} \ No newline at end of file diff --git a/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java b/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java index 377701e0..6b49855c 100644 --- a/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java +++ b/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.github.pedrovgs.linkedlist; /** @@ -50,12 +51,18 @@ public void setNext(ListNode next) { } @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof ListNode)) return false; + if (this == o) { + return true; + } + if (!(o instanceof ListNode)) { + return false; + } ListNode listNode = (ListNode) o; - if (!data.equals(listNode.data)) return false; + if (!data.equals(listNode.data)) { + return false; + } return true; } diff --git a/src/main/java/com/github/pedrovgs/pair/Pair.java b/src/main/java/com/github/pedrovgs/pair/Pair.java index 58a3bce7..affd0c34 100644 --- a/src/main/java/com/github/pedrovgs/pair/Pair.java +++ b/src/main/java/com/github/pedrovgs/pair/Pair.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.github.pedrovgs.pair; /** @@ -31,13 +32,21 @@ public Pair(A a, B b) { } @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Pair)) return false; + if (this == o) { + return true; + } + if (!(o instanceof Pair)) { + return false; + } Pair pair = (Pair) o; - if (!a.equals(pair.a)) return false; - if (!b.equals(pair.b)) return false; + if (!a.equals(pair.a)) { + return false; + } + if (!b.equals(pair.b)) { + return false; + } return true; } diff --git a/src/main/java/com/github/pedrovgs/statistics/Sample.java b/src/main/java/com/github/pedrovgs/statistics/Sample.java new file mode 100644 index 00000000..92bba7f1 --- /dev/null +++ b/src/main/java/com/github/pedrovgs/statistics/Sample.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.pedrovgs.statistics; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + + + +public class Sample { + + private List sample; + + public Sample(List sample) { + this.sample = sample; + } + + /** + * Method calculates the frequencies of the Sample. + * @return A Map of the frequencies of every object in the sample. + * @throws IllegalArgumentException when the sample is empty. + */ + public Map frequencies() { + + if (sample.size() == 0) { + throw new IllegalArgumentException("Sample is empty !"); + } + + Map freq = new HashMap(); + double count; + for (int i = 0; i < sample.size(); i++) { + + count = freq.containsKey(sample.get(i)) ? freq.get(sample.get(i)) : 0.0; + freq.put(sample.get(i), count + 1); + + } + return freq; + } + + /** + * Method calculates the avg of the Sample. + * @return The avg. + * @throws IllegalArgumentException when sample is empty or when the Sample is not quantitative. + */ + public double avg() { + + if (sample.size() == 0) { + throw new IllegalArgumentException("Sample is empty !"); + } + + double avg = 0; + try { + for (int i = 0; i < sample.size(); i++) { + avg += Double.parseDouble(sample.get(i).toString()); + } + } catch (Exception e) { + throw new IllegalArgumentException("The sample is not quantitative."); + } + return avg / sample.size(); + } + + /** + * Method calculates the median of the Sample. + * @return The median. + * @throws IllegalArgumentException when sample is empty or when the Sample is not quantitative. + */ + public double median() { + + if (sample.size() == 0) { + throw new IllegalArgumentException("Sample is empty !"); + } + + double median; + double x; + double y; + try { + if (sample.size() % 2 == 0) { + x = Double.parseDouble(sample.get(sample.size() / 2 - 1).toString()); + y = Double.parseDouble(sample.get(sample.size() / 2).toString()); + median = (x + y) / 2; + } else { + median = Double.parseDouble(sample.get(sample.size() / 2).toString()); + } + } catch (Exception e) { + throw new IllegalArgumentException("The sample is not quantitative."); + } + + return median; + } + + /** + * Method calculates the variance of the Sample. + * @return The variance. + * @throws IllegalArgumentException when sample is empty or when the Sample is not quantitative. + */ + public double variance() { + + if (sample.size() == 0) { + throw new IllegalArgumentException("Sample is empty !"); + } + + double var = 0; + try { + for (int i = 0; i < sample.size(); i++) { + var += Math.pow((Double.parseDouble(sample.get(i).toString())) - avg(), 2); + } + } catch (Exception e) { + throw new IllegalArgumentException("The sample is not quantitative."); + } + return var = var / (sample.size() - 1); + } + + /** + * Method calculates the standardDeviation of the Sample. + * @return The standardDeviation. + * @throws IllegalArgumentException when sample is empty or when the Sample is not quantitative. + */ + public double standardDeviation() { + + if (sample.size() == 0) { + throw new IllegalArgumentException("Sample is empty !"); + } + + return Math.pow(variance(), 1.0 / 2.0); + } +} \ No newline at end of file diff --git a/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java b/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java new file mode 100644 index 00000000..6db44174 --- /dev/null +++ b/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.pedrovgs.equations; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + + +/** + * This class contains tests for Grade2Equation.solve method. + * @author ShoeMaker + * + */ + +public class Grade2EquationTest { + + Grade2Equation equation = new Grade2Equation(); + Double[] xvalue = {-1.0, -1.0}; + + /** + * Tests a normal case of an equation. + */ + @Test + public void test_solve() { + Assert.assertArrayEquals(xvalue, equation.solve(1, 2, 1)); + } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * Tests a case of an unsolvable equation. + */ + @Test + public void test_solve_IllegalArgumentException() { + + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("There is no Real solution to this equation."); + equation.solve(2, 3, 4); + } +} \ No newline at end of file diff --git a/src/test/java/com/github/pedrovgs/files/TxtToListTest.java b/src/test/java/com/github/pedrovgs/files/TxtToListTest.java new file mode 100644 index 00000000..f9ebca90 --- /dev/null +++ b/src/test/java/com/github/pedrovgs/files/TxtToListTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.pedrovgs.files; + +import java.util.Arrays; + +import java.util.List; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; + +import org.junit.rules.ExpectedException; + + +/** + * This class contains tests for TxtToList.readFileToList method. + * @author ShoeMaker + * + */ +public class TxtToListTest { + + //Files we are going to use for the test + TxtToList ttl = new TxtToList(); + String path = "src/test/resources/grades.txt"; + String empty = "src/test/resources/empty.txt"; + + //Expected outcome of the readFileToList test + String[] outcome = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; + List expected = Arrays.asList(outcome); + + /** + * This tests if the readFileToList method works correctly. + */ + @Test + public void test_readFileToList() { + Assert.assertEquals(expected, ttl.readFileToList(path)); + } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * Tests a case of IOException in readFileToList method. + */ + @Test + public void test_readFile_IOexception() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Something went wrong. Wrong path or file doesn't exist."); + ttl.readFileToList("asfasdf"); + } + + /** + * Tests a case of reading an empty file in readFileToList method. + */ + @Test + public void test_readFile_FileEmpty() { + + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("File was empty."); + ttl.readFileToList(empty); + } + + +} \ No newline at end of file diff --git a/src/test/java/com/github/pedrovgs/statistics/SampleTest.java b/src/test/java/com/github/pedrovgs/statistics/SampleTest.java new file mode 100644 index 00000000..5c6c60c6 --- /dev/null +++ b/src/test/java/com/github/pedrovgs/statistics/SampleTest.java @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.pedrovgs.statistics; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + + +/** + * This class contains tests for Sample class. + * @author ShoeMaker + * + */ + +public class SampleTest { + + List file = new ArrayList(); + + /** + * Tests a normal case of frequencies in a sample. + */ + @Test + public void test_frequencies() { + + for (int i = 1; i < 4; i++) { + file.add(i); + } + + Map freq = new HashMap(); + freq.put(1, 1.0); + freq.put(2, 1.0); + freq.put(3, 1.0); + Sample grades = new Sample(file); + Assert.assertEquals(freq, grades.frequencies()); + } + + /** + * Tests a normal case of avg in a sample. + */ + @Test + public void test_avg() { + + for (int i = 1; i < 4; i++) { + file.add(i); + } + Sample grades = new Sample(file); + + Assert.assertEquals(2.0, grades.avg(), 0); + } + + /** + * Tests a normal case of median in a sample. + */ + @Test + public void test_median() { + + for (int i = 1; i < 4; i++) { + file.add(i); + } + Sample grades = new Sample(file); + + Assert.assertEquals(2.0, grades.median(), 0); + } + + /** + * Tests a normal case of variance in a sample. + */ + @Test + public void test_variance() { + + for (int i = 1; i < 4; i++) { + file.add(i); + } + Sample grades = new Sample(file); + + Assert.assertEquals(1.0, grades.variance(), 0); + } + + /** + * Tests a normal case of standardDeviation in a sample. + */ + @Test + public void standardDeviation() { + + for (int i = 1; i < 4; i++) { + file.add(i); + } + Sample grades = new Sample(file); + + Assert.assertEquals(1.0, grades.standardDeviation(), 0); + } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * Tests a case of an Empty Sample in frequencies method. + */ + @Test + public void test_frequencies_EmptySample() { + + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Sample is empty !"); + grades.frequencies(); + } + + /** + * Tests a case of an Uncountable Sample in avg method. + */ + @Test + public void test_avg_Uncountable() { + + file.add("a"); + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("The sample is not quantitative."); + grades.avg(); + } + + /** + * Tests a case of an Empty Sample in avg method. + */ + @Test + public void test_avg_EmptySample() { + + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Sample is empty !"); + grades.avg(); + } + + /** + * Tests a case of an Uncountable Sample in median method. + */ + @Test + public void test_median_Uncountable() { + + file.add("a"); + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("The sample is not quantitative."); + grades.median(); + } + + /** + * Tests a case of an Empty Sample in median method. + */ + @Test + public void test_median_EmptySample() { + + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Sample is empty !"); + grades.median(); + } + + /** + * Tests a case of an Uncountable Sample in variance method. + */ + @Test + public void test_variance_Uncountable() { + + file.add("a"); + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("The sample is not quantitative."); + grades.variance(); + } + + /** + * Tests a case of an Empty Sample in variance method. + */ + @Test + public void test_variance_EmptySample() { + + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Sample is empty !"); + grades.variance(); + } + + /** + * Tests a case of an Uncountable Sample in standardDeviation method. + */ + @Test + public void test_standardDeviation_Uncountable() { + + file.add("a"); + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("The sample is not quantitative."); + grades.standardDeviation(); + } + + /** + * Tests a case of an Empty Sample in standardDeviation method. + */ + @Test + public void test_standardDeviation_EmptySample() { + + Sample grades = new Sample(file); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Sample is empty !"); + grades.standardDeviation(); + } +} diff --git a/src/test/resources/empty.txt b/src/test/resources/empty.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/resources/grades.txt b/src/test/resources/grades.txt new file mode 100644 index 00000000..e372ac9e --- /dev/null +++ b/src/test/resources/grades.txt @@ -0,0 +1,11 @@ +1 +3 +4 +6 +2 +4 +7 +8 +10 +9 +5 \ No newline at end of file