Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new package with an Algorithm that takes a txt file and conve… #39

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions .checkstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="Google Checks" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>
21 changes: 16 additions & 5 deletions src/main/java/com/github/pedrovgs/binarytree/BinaryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.pedrovgs.binarytree;

/**
Expand Down Expand Up @@ -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;
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/github/pedrovgs/equations/Grade2Equation.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/github/pedrovgs/files/TxtToList.java
Original file line number Diff line number Diff line change
@@ -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<String> readFileToList(String filepath) {

List<String> datas = new ArrayList<String>();
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;
}
}
13 changes: 10 additions & 3 deletions src/main/java/com/github/pedrovgs/linkedlist/ListNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.pedrovgs.linkedlist;

/**
Expand Down Expand Up @@ -50,12 +51,18 @@ public void setNext(ListNode<T> 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;
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/github/pedrovgs/pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.pedrovgs.pair;

/**
Expand All @@ -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;
}
Expand Down
142 changes: 142 additions & 0 deletions src/main/java/com/github/pedrovgs/statistics/Sample.java
Original file line number Diff line number Diff line change
@@ -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<Object> sample;

public Sample(List<Object> 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<Object, Double> frequencies() {

if (sample.size() == 0) {
throw new IllegalArgumentException("Sample is empty !");
}

Map<Object, Double> freq = new HashMap<Object, Double>();
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);
}
}
Loading