From 71299189524af6e44284d483dffa6b4d5f2abdb8 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Fri, 11 May 2018 17:11:35 +0300 Subject: [PATCH 01/12] Added a new package with an Algorithm that takes a txt file and converts it into a list for further use. Also added the test class and some txt files for the testing. --- .../github/pedrovgs/txtToList/TxtToList.java | 69 +++++++++++++++++++ .../pedrovgs/txtToList/TxtToListTest.java | 59 ++++++++++++++++ src/test/resources/empty.txt | 0 src/test/resources/grades.txt | 11 +++ 4 files changed, 139 insertions(+) create mode 100644 src/main/java/com/github/pedrovgs/txtToList/TxtToList.java create mode 100644 src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java create mode 100644 src/test/resources/empty.txt create mode 100644 src/test/resources/grades.txt diff --git a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java new file mode 100644 index 00000000..d14be432 --- /dev/null +++ b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java @@ -0,0 +1,69 @@ +/* + * 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.txtToList; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.List; +import java.util.ArrayList; +import java.io.IOException; +import java.nio.file.Paths; + +/** + * This class contains and 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 An List with the file's lines. + * @throws IllegalArgumentException when an IOException occurs or the file is empty. + */ + + public List readFileToList(String filepath) { + + filepath = Paths.get(".").toAbsolutePath().normalize().toString() + filepath; + List datas = new ArrayList(); + BufferedReader br = null; + String line = null; + try { + br = new BufferedReader(new FileReader(filepath)); + line = br.readLine(); + while (line != null) { + + if (line != null) { + datas.add(line); + } + line = br.readLine(); + } + + br.close(); + } catch (IOException e) { + throw new IllegalArgumentException("Something went wrong while reading the file. Probably 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/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java new file mode 100644 index 00000000..48692701 --- /dev/null +++ b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java @@ -0,0 +1,59 @@ +package com.github.pedrovgs.txtToList; + +import org.junit.Test; + +import org.junit.Assert; +import org.junit.rules.ExpectedException; +import org.junit.Rule; +import java.util.List; +import java.util.Arrays; + +/** + * 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"; + + String[] a = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; //Expected outcome of the readFileToList test + List b = Arrays.asList(a); + /** + * This tests if the readFileToList method works correctly. + */ + @Test + public void test_readFileToList() { + Assert.assertEquals(b, 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 while reading the file. Probably 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/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 From 6c687b4bf8c95bf6d1792ef7b2f357ad35957543 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Fri, 11 May 2018 22:53:07 +0300 Subject: [PATCH 02/12] Changed the line that gives the path of the working directory, it may solve the build error. --- src/main/java/com/github/pedrovgs/txtToList/TxtToList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java index d14be432..28278c7f 100644 --- a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java +++ b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java @@ -41,7 +41,7 @@ public class TxtToList { public List readFileToList(String filepath) { - filepath = Paths.get(".").toAbsolutePath().normalize().toString() + filepath; + filepath = Paths.get("").toAbsolutePath() + filepath; List datas = new ArrayList(); BufferedReader br = null; String line = null; From 94d3078366ab6e4a168788b814af7d95a783d887 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sat, 12 May 2018 10:34:54 +0300 Subject: [PATCH 03/12] Fixed a condition that was checked twice for no reason --- src/main/java/com/github/pedrovgs/txtToList/TxtToList.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java index 28278c7f..3f1b87dc 100644 --- a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java +++ b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java @@ -50,9 +50,9 @@ public List readFileToList(String filepath) { line = br.readLine(); while (line != null) { - if (line != null) { - datas.add(line); - } + + datas.add(line); + line = br.readLine(); } From 0f6e1cf6681e019260268dcc5f454d4cbf3aed13 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Mon, 14 May 2018 20:31:12 +0300 Subject: [PATCH 04/12] Changed paths of the test files, probably solves the build errors --- src/main/java/com/github/pedrovgs/txtToList/TxtToList.java | 4 ++-- .../java/com/github/pedrovgs/txtToList/TxtToListTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java index 3f1b87dc..95030e58 100644 --- a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java +++ b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java @@ -21,7 +21,7 @@ import java.util.List; import java.util.ArrayList; import java.io.IOException; -import java.nio.file.Paths; + /** * This class contains and algorithm that reads txt files and turns them to Lists for further use. @@ -41,7 +41,7 @@ public class TxtToList { public List readFileToList(String filepath) { - filepath = Paths.get("").toAbsolutePath() + filepath; + List datas = new ArrayList(); BufferedReader br = null; String line = null; diff --git a/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java index 48692701..7e58c998 100644 --- a/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java +++ b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java @@ -17,8 +17,8 @@ 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"; + String path = "src/test/resources/grades.txt"; + String empty ="src/test/resources/empty.txt"; String[] a = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; //Expected outcome of the readFileToList test List b = Arrays.asList(a); From aea26f94c6e8f425f9f423e9ed4569a42d0d8359 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 13:28:43 +0300 Subject: [PATCH 05/12] Made some fixes in the documentation --- .../com/github/pedrovgs/txtToList/TxtToList.java | 10 +++++----- .../github/pedrovgs/txtToList/TxtToListTest.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java index 95030e58..abd929f1 100644 --- a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java +++ b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java @@ -18,24 +18,24 @@ import java.io.BufferedReader; import java.io.FileReader; -import java.util.List; -import java.util.ArrayList; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** - * This class contains and algorithm that reads txt files and turns them to Lists for further use. + * 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 An List with the file's lines. + * @return A List with the file's lines. * @throws IllegalArgumentException when an IOException occurs or the file is empty. */ diff --git a/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java index 7e58c998..a721a88c 100644 --- a/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java +++ b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java @@ -1,3 +1,19 @@ +/* + * 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.txtToList; import org.junit.Test; From 92da7fce15140e371bca262f947215490697bd1a Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 13:30:32 +0300 Subject: [PATCH 06/12] Added a class that solves 2nd grade equations, plus made some tests for it --- .../grade2Equation/Grade2Equation.java | 48 ++++++++++++++++ .../grade2Equation/Grade2EquationTest.java | 57 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java create mode 100644 src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java diff --git a/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java b/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java new file mode 100644 index 00000000..abe7444b --- /dev/null +++ b/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java @@ -0,0 +1,48 @@ +/* + * 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.grade2Equation; + +/** + * This class contains an algorithm that solves 2nd grade equations. + * @author ShoeMaker + * + */ + +public class Grade2Equation { + + /** + * Method solves 2nd grade equations. + * @param a + * @param b + * @param c + * @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; + + } +} diff --git a/src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java b/src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java new file mode 100644 index 00000000..64fa0f47 --- /dev/null +++ b/src/test/java/com/github/pedrovgs/grade2Equation/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.grade2Equation; + +import org.junit.Test; + +import org.junit.Assert; +import org.junit.rules.ExpectedException; +import org.junit.Rule; + +/** + * This class contains tests for Grade2Equation.solve method. + * @author ShoeMaker + * + */ + +public class Grade2EquationTest { + + Grade2Equation equation = new Grade2Equation(); + Double x[] = {-1.0, -1.0}; + + /** + * Tests a normal case of an equation. + */ + @Test + public void test_solve() { + Assert.assertArrayEquals(x, 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); + } +} From 8b6950701bef93452fa812361eb32a40fb2d744e Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 13:32:58 +0300 Subject: [PATCH 07/12] Added a class that represents a Sample, it contains classic statistic methods for the sample. Also made tests for it. --- .../github/pedrovgs/statistics/Sample.java | 158 ++++++++++++ .../pedrovgs/statistics/SampleTest.java | 228 ++++++++++++++++++ 2 files changed, 386 insertions(+) create mode 100644 src/main/java/com/github/pedrovgs/statistics/Sample.java create mode 100644 src/test/java/com/github/pedrovgs/statistics/SampleTest.java 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..f0a2f0b2 --- /dev/null +++ b/src/main/java/com/github/pedrovgs/statistics/Sample.java @@ -0,0 +1,158 @@ +/* + * 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.List; +import java.util.Map; +import java.util.ArrayList; +import java.util.HashMap; + + +public class Sample { + + private List sample; + + public static void main(String[] args) { + + List file = new ArrayList(); + + for (int i = 1; i < 4; i++) { + file.add(Integer.toString(i)); + } + Sample grades = new Sample(file); + Map freq = grades.frequencies(); + System.out.println(freq); + System.out.println(grades.avg()); + System.out.println(grades.median()); + System.out.println(grades.variance()); + System.out.println(grades.standardDeviation()); + + } + + 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 the 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 the 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 the 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 the 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/statistics/SampleTest.java b/src/test/java/com/github/pedrovgs/statistics/SampleTest.java new file mode 100644 index 00000000..d07eb589 --- /dev/null +++ b/src/test/java/com/github/pedrovgs/statistics/SampleTest.java @@ -0,0 +1,228 @@ +/* + * 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 org.junit.Test; + +import java.util.List; +import java.util.Map; +import java.util.ArrayList; +import java.util.HashMap; + +import org.junit.Assert; +import org.junit.rules.ExpectedException; +import org.junit.Rule; + +/** + * 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); + } + Sample grades = new Sample(file); + Map freq = new HashMap(); + freq.put(1, 1.0); + freq.put(2, 1.0); + freq.put(3, 1.0); + 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(); + } + +} From 635672687729cc5e35add0b73e4c829cb3db45ae Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 14:43:11 +0300 Subject: [PATCH 08/12] Fixed some checkstyle issues --- .../grade2Equation/Grade2Equation.java | 42 +- .../github/pedrovgs/statistics/Sample.java | 252 ++++++----- .../github/pedrovgs/txtToList/TxtToList.java | 63 ++- .../grade2Equation/Grade2EquationTest.java | 56 +-- .../pedrovgs/statistics/SampleTest.java | 393 +++++++++--------- .../pedrovgs/txtToList/TxtToListTest.java | 92 ++-- 6 files changed, 437 insertions(+), 461 deletions(-) diff --git a/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java b/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java index abe7444b..5e58211f 100644 --- a/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java +++ b/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java @@ -23,26 +23,24 @@ */ public class Grade2Equation { - - /** - * Method solves 2nd grade equations. - * @param a - * @param b - * @param c - * @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; - - } + + /** + * 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; + } } diff --git a/src/main/java/com/github/pedrovgs/statistics/Sample.java b/src/main/java/com/github/pedrovgs/statistics/Sample.java index f0a2f0b2..b2bd957c 100644 --- a/src/main/java/com/github/pedrovgs/statistics/Sample.java +++ b/src/main/java/com/github/pedrovgs/statistics/Sample.java @@ -16,143 +16,127 @@ package com.github.pedrovgs.statistics; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.ArrayList; -import java.util.HashMap; + + public class Sample { - - private List sample; - - public static void main(String[] args) { - - List file = new ArrayList(); - - for (int i = 1; i < 4; i++) { - file.add(Integer.toString(i)); - } - Sample grades = new Sample(file); - Map freq = grades.frequencies(); - System.out.println(freq); - System.out.println(grades.avg()); - System.out.println(grades.median()); - System.out.println(grades.variance()); - System.out.println(grades.standardDeviation()); - - } - - 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 the 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 the 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 the 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 the 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); - } + + 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/main/java/com/github/pedrovgs/txtToList/TxtToList.java b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java index abd929f1..6d2d6979 100644 --- a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java +++ b/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java @@ -31,39 +31,32 @@ 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 while reading the file. Probably wrong path or file doesn't exist."); - } - if (datas.size() == 0) { - throw new IllegalArgumentException("File was empty."); - } - - return datas; - } + /** + * 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 while reading the file. Probably 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/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java b/src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java index 64fa0f47..6b395634 100644 --- a/src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java +++ b/src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java @@ -16,11 +16,11 @@ package com.github.pedrovgs.grade2Equation; -import org.junit.Test; - import org.junit.Assert; -import org.junit.rules.ExpectedException; import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + /** * This class contains tests for Grade2Equation.solve method. @@ -29,29 +29,29 @@ */ public class Grade2EquationTest { - - Grade2Equation equation = new Grade2Equation(); - Double x[] = {-1.0, -1.0}; - - /** - * Tests a normal case of an equation. - */ - @Test - public void test_solve() { - Assert.assertArrayEquals(x, 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); - } + + Grade2Equation equation = new Grade2Equation(); + Double[] x = {-1.0, -1.0}; + + /** + * Tests a normal case of an equation. + */ + @Test + public void test_solve() { + Assert.assertArrayEquals(x, 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); + } } diff --git a/src/test/java/com/github/pedrovgs/statistics/SampleTest.java b/src/test/java/com/github/pedrovgs/statistics/SampleTest.java index d07eb589..5c6c60c6 100644 --- a/src/test/java/com/github/pedrovgs/statistics/SampleTest.java +++ b/src/test/java/com/github/pedrovgs/statistics/SampleTest.java @@ -16,16 +16,15 @@ package com.github.pedrovgs.statistics; -import org.junit.Test; - -import java.util.List; -import java.util.Map; import java.util.ArrayList; import java.util.HashMap; - +import java.util.List; +import java.util.Map; import org.junit.Assert; -import org.junit.rules.ExpectedException; import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + /** * This class contains tests for Sample class. @@ -34,195 +33,195 @@ */ 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); - } - Sample grades = new Sample(file); - Map freq = new HashMap(); - freq.put(1, 1.0); - freq.put(2, 1.0); - freq.put(3, 1.0); - 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(); - } - + + 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/java/com/github/pedrovgs/txtToList/TxtToListTest.java b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java index a721a88c..182286eb 100644 --- a/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java +++ b/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java @@ -16,13 +16,15 @@ package com.github.pedrovgs.txtToList; -import org.junit.Test; +import java.util.Arrays; +import java.util.List; import org.junit.Assert; -import org.junit.rules.ExpectedException; import org.junit.Rule; -import java.util.List; -import java.util.Arrays; +import org.junit.Test; + +import org.junit.rules.ExpectedException; + /** * This class contains tests for TxtToList.readFileToList method. @@ -30,46 +32,46 @@ * */ 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"; - - String[] a = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; //Expected outcome of the readFileToList test - List b = Arrays.asList(a); - /** - * This tests if the readFileToList method works correctly. - */ - @Test - public void test_readFileToList() { - Assert.assertEquals(b, 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 while reading the file. Probably 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); - } - - + //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"; + + String[] a = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; //Expected outcome of the readFileToList test + List b = Arrays.asList(a); + + /** + * This tests if the readFileToList method works correctly. + */ + @Test + public void test_readFileToList() { + Assert.assertEquals(b, 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 while reading the file. Probably 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 From 0593265bd05be35649393a9b1e038ce45aa7166b Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 15:00:34 +0300 Subject: [PATCH 09/12] Fixed more checkstyle errors --- .checkstyle | 7 +++++++ .../{grade2Equation => equations}/Grade2Equation.java | 4 ++-- .../github/pedrovgs/{txtToList => files}/TxtToList.java | 2 +- .../{grade2Equation => equations}/Grade2EquationTest.java | 4 ++-- .../pedrovgs/{txtToList => files}/TxtToListTest.java | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 .checkstyle rename src/main/java/com/github/pedrovgs/{grade2Equation => equations}/Grade2Equation.java (96%) rename src/main/java/com/github/pedrovgs/{txtToList => files}/TxtToList.java (97%) rename src/test/java/com/github/pedrovgs/{grade2Equation => equations}/Grade2EquationTest.java (96%) rename src/test/java/com/github/pedrovgs/{txtToList => files}/TxtToListTest.java (98%) 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/grade2Equation/Grade2Equation.java b/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java similarity index 96% rename from src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java rename to src/main/java/com/github/pedrovgs/equations/Grade2Equation.java index 5e58211f..2ce87c4c 100644 --- a/src/main/java/com/github/pedrovgs/grade2Equation/Grade2Equation.java +++ b/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.github.pedrovgs.grade2Equation; +package com.github.pedrovgs.equations; /** * This class contains an algorithm that solves 2nd grade equations. @@ -43,4 +43,4 @@ public Double[] solve(double a, double b, double c) { 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/txtToList/TxtToList.java b/src/main/java/com/github/pedrovgs/files/TxtToList.java similarity index 97% rename from src/main/java/com/github/pedrovgs/txtToList/TxtToList.java rename to src/main/java/com/github/pedrovgs/files/TxtToList.java index 6d2d6979..d543dca4 100644 --- a/src/main/java/com/github/pedrovgs/txtToList/TxtToList.java +++ b/src/main/java/com/github/pedrovgs/files/TxtToList.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.github.pedrovgs.txtToList; +package com.github.pedrovgs.files; import java.io.BufferedReader; import java.io.FileReader; diff --git a/src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java b/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java similarity index 96% rename from src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java rename to src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java index 6b395634..56695357 100644 --- a/src/test/java/com/github/pedrovgs/grade2Equation/Grade2EquationTest.java +++ b/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.github.pedrovgs.grade2Equation; +package com.github.pedrovgs.equations; import org.junit.Assert; import org.junit.Rule; @@ -54,4 +54,4 @@ public void test_solve_IllegalArgumentException() { 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/txtToList/TxtToListTest.java b/src/test/java/com/github/pedrovgs/files/TxtToListTest.java similarity index 98% rename from src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java rename to src/test/java/com/github/pedrovgs/files/TxtToListTest.java index 182286eb..dee2e4d1 100644 --- a/src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java +++ b/src/test/java/com/github/pedrovgs/files/TxtToListTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.github.pedrovgs.txtToList; +package com.github.pedrovgs.files; import java.util.Arrays; From 14a6e4d2efd8c295b7d8804d351359cd96ba64f2 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 15:07:42 +0300 Subject: [PATCH 10/12] Fixed even more checkstyle errors --- .../java/com/github/pedrovgs/files/TxtToList.java | 2 +- .../github/pedrovgs/equations/Grade2EquationTest.java | 4 ++-- .../java/com/github/pedrovgs/files/TxtToListTest.java | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/pedrovgs/files/TxtToList.java b/src/main/java/com/github/pedrovgs/files/TxtToList.java index d543dca4..17215079 100644 --- a/src/main/java/com/github/pedrovgs/files/TxtToList.java +++ b/src/main/java/com/github/pedrovgs/files/TxtToList.java @@ -52,7 +52,7 @@ public List readFileToList(String filepath) { } br.close(); } catch (IOException e) { - throw new IllegalArgumentException("Something went wrong while reading the file. Probably wrong path or file doesn't exist."); + throw new IllegalArgumentException("Something went wrong. Wrong path or file doesn't exist."); } if (datas.size() == 0) { throw new IllegalArgumentException("File was empty."); diff --git a/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java b/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java index 56695357..6db44174 100644 --- a/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java +++ b/src/test/java/com/github/pedrovgs/equations/Grade2EquationTest.java @@ -31,14 +31,14 @@ public class Grade2EquationTest { Grade2Equation equation = new Grade2Equation(); - Double[] x = {-1.0, -1.0}; + Double[] xvalue = {-1.0, -1.0}; /** * Tests a normal case of an equation. */ @Test public void test_solve() { - Assert.assertArrayEquals(x, equation.solve(1, 2, 1)); + Assert.assertArrayEquals(xvalue, equation.solve(1, 2, 1)); } @Rule diff --git a/src/test/java/com/github/pedrovgs/files/TxtToListTest.java b/src/test/java/com/github/pedrovgs/files/TxtToListTest.java index dee2e4d1..f9ebca90 100644 --- a/src/test/java/com/github/pedrovgs/files/TxtToListTest.java +++ b/src/test/java/com/github/pedrovgs/files/TxtToListTest.java @@ -37,16 +37,17 @@ public class TxtToListTest { TxtToList ttl = new TxtToList(); String path = "src/test/resources/grades.txt"; String empty = "src/test/resources/empty.txt"; - - String[] a = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; //Expected outcome of the readFileToList test - List b = Arrays.asList(a); + + //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(b, ttl.readFileToList(path)); + Assert.assertEquals(expected, ttl.readFileToList(path)); } @Rule @@ -58,7 +59,7 @@ public void test_readFileToList() { @Test public void test_readFile_IOexception() { thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Something went wrong while reading the file. Probably wrong path or file doesn't exist."); + thrown.expectMessage("Something went wrong. Wrong path or file doesn't exist."); ttl.readFileToList("asfasdf"); } From aafb61acf44c6ecda7f8d564b8761735253bad44 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 15:15:36 +0300 Subject: [PATCH 11/12] Fixed even even more checkstyle errors --- .../pedrovgs/binarytree/BinaryNode.java | 21 ++++++++++++++----- .../github/pedrovgs/linkedlist/ListNode.java | 13 +++++++++--- .../java/com/github/pedrovgs/pair/Pair.java | 17 +++++++++++---- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java b/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java index 0db25d25..27987486 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/linkedlist/ListNode.java b/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java index 377701e0..9e6a6ea6 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; } From a8e2574ef93fff682bb4bd78d8951055860d7320 Mon Sep 17 00:00:00 2001 From: ETsagkaris Date: Sun, 3 Jun 2018 15:21:33 +0300 Subject: [PATCH 12/12] Fixed some more checkstyle errors --- .../com/github/pedrovgs/binarytree/BinaryNode.java | 2 +- .../github/pedrovgs/equations/Grade2Equation.java | 2 +- .../com/github/pedrovgs/linkedlist/ListNode.java | 2 +- .../java/com/github/pedrovgs/statistics/Sample.java | 12 ++++++------ 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java b/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java index 27987486..a7693715 100644 --- a/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java +++ b/src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java @@ -76,7 +76,7 @@ public boolean hasRight() { if (!data.equals(that.data)) { return false; } - if (left != null ? !left.equals(that.left) : that.left != null) { + if (left != null ? !left.equals(that.left) : that.left != null) { return false; } if (right != null ? !right.equals(that.right) : that.right != null) { diff --git a/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java b/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java index 2ce87c4c..522b6ab6 100644 --- a/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java +++ b/src/main/java/com/github/pedrovgs/equations/Grade2Equation.java @@ -33,7 +33,7 @@ public class Grade2Equation { * @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."); diff --git a/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java b/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java index 9e6a6ea6..6b49855c 100644 --- a/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java +++ b/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java @@ -60,7 +60,7 @@ public void setNext(ListNode next) { ListNode listNode = (ListNode) o; - if (!data.equals(listNode.data)) { + if (!data.equals(listNode.data)) { return false; } diff --git a/src/main/java/com/github/pedrovgs/statistics/Sample.java b/src/main/java/com/github/pedrovgs/statistics/Sample.java index b2bd957c..92bba7f1 100644 --- a/src/main/java/com/github/pedrovgs/statistics/Sample.java +++ b/src/main/java/com/github/pedrovgs/statistics/Sample.java @@ -26,23 +26,23 @@ 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() { + public Map frequencies() { if (sample.size() == 0) { throw new IllegalArgumentException("Sample is empty !"); } - Map freq = new HashMap(); + Map freq = new HashMap(); double count; for (int i = 0; i < sample.size(); i++) { @@ -52,7 +52,7 @@ public Map frequencies() { } return freq; } - + /** * Method calculates the avg of the Sample. * @return The avg. @@ -74,7 +74,7 @@ public double avg() { } return avg / sample.size(); } - + /** * Method calculates the median of the Sample. * @return The median.