refactor: format code and fix typos

This commit is contained in:
yanglbme 2019-05-10 14:49:24 +08:00
parent 60bb026f8a
commit 8e8e14d1c2
21 changed files with 524 additions and 516 deletions

View File

@ -3,6 +3,7 @@ package src.main.java.com.conversions;
public class AnyBaseToDecimal {
/**
* This method produces a decimal value of any given input number of any base
*
* @param inpNum String of which we need the decimal value and base in integer format
* @return string format of the decimal value
*/
@ -12,11 +13,11 @@ public class AnyBaseToDecimal {
int num = 0;
int pow = 1;
for (int i=len-1; i>=0; i--) {
for (int i = len - 1; i >= 0; i--) {
if (valOfChar(inpNum.charAt(i)) >= base) {
return "Invalid Number";
}
num += valOfChar(inpNum.charAt(i))*pow;
num += valOfChar(inpNum.charAt(i)) * pow;
pow *= base;
}
return String.valueOf(num);
@ -24,16 +25,16 @@ public class AnyBaseToDecimal {
/**
* This method produces integer value of the input character and returns it
*
* @param c Char of which we need the integer value of
* @return integer value of input char
*/
private static int valOfChar(char c) {
if (c >= '0' && c <= '9') {
return (int)c - '0';
}
else {
return (int)c - 'A' + 10;
return (int) c - '0';
} else {
return (int) c - 'A' + 10;
}
}
}

View File

@ -3,23 +3,24 @@ package src.main.java.com.conversions;
import java.util.ArrayList;
public class DecimalToAnyBase {
/**
* This method produces a String value of any given input decimal in any base
*
* @param inp Decimal of which we need the value in base in String format
* @param base base in which we want the decimal value to be converted into
* @return string format of the converted value in the given base
*/
public String convertToAnyBase(int inp, int base) {
ArrayList<Character> charArr = new ArrayList<>();
while (inp > 0) {
charArr.add(reVal(inp%base));
charArr.add(reVal(inp % base));
inp /= base;
}
StringBuilder str = new StringBuilder(charArr.size());
for(Character ch: charArr) {
for (Character ch : charArr) {
str.append(ch);
}
@ -28,14 +29,15 @@ public class DecimalToAnyBase {
/**
* This method produces character value of the input integer and returns it
*
* @param num integer of which we need the character value of
* @return character value of input integer
*/
private char reVal(int num) {
if (num >= 0 && num <= 9)
return (char)(num + '0');
return (char) (num + '0');
else
return (char)(num - 10 + 'A');
return (char) (num - 10 + 'A');
}
}

View File

@ -3,15 +3,16 @@ package src.main.java.com.conversions;
import java.math.BigInteger;
public class DecimalToHexadecimal {
private static final char hexChars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
private static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static final BigInteger valueHex = new BigInteger("16");
/**
* This method converts and decimal number to a Hexadecimal number
*
* @param decimalStr
* @return hexadecimal number
*/
public String decimalToHex(String decimalStr){
public String decimalToHex(String decimalStr) {
BigInteger decimal = new BigInteger(decimalStr);
int rem;

View File

@ -3,20 +3,21 @@ package src.main.java.com.conversions;
import java.math.BigInteger;
public class DecimalToOctal {
private static final char octalChars[] = {'0','1','2','3','4','5','6','7'};
private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'};
private static final BigInteger valueOctal = new BigInteger("8");
/**
* This method converts and decimal number to a octal number
*
* @param decimalStr
* @return octal number
*/
public String decimalToOctal(String decimalStr){
public String decimalToOctal(String decimalStr) {
BigInteger decimal = new BigInteger(decimalStr);
int rem;
String octal = "";
while(decimal.compareTo(BigInteger.ZERO) > 0) {
while (decimal.compareTo(BigInteger.ZERO) > 0) {
rem = decimal.mod(valueOctal).intValueExact();
octal = octalChars[rem] + octal;
decimal = decimal.divide(valueOctal);

View File

@ -2,9 +2,10 @@ package src.main.java.com.dataStructures;
/**
* Binary tree for general value type, without redundancy
* @author RICARDO
*
* @param <T> root data
*/
public class BinaryTree<T extends Comparable> {
private final T data;
private BinaryTree right, // the upper binary tree
@ -15,23 +16,25 @@ public class BinaryTree<T extends Comparable> {
}
@Override
public String toString(){
public String toString() {
return this.data.toString();
}
/**
* inserts a new value in it's correspondant place
* @param newDataValue value of the new banary tree to add on this tree
*
* @param newDataValue value of the new binary tree to add on this tree
*/
public void insert(T newDataValue){
public void insert(T newDataValue) {
this.insert(new BinaryTree(newDataValue));
}
/**
* inserts a new binary tree in it's correspondant place
*
* @param newData new value to add on this tree
*/
public void insert(BinaryTree newData){
public void insert(BinaryTree newData) {
int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value
@ -51,10 +54,11 @@ public class BinaryTree<T extends Comparable> {
/**
* search and specific value on the tree
*
* @param data Searched value
* @return Binary tree wich contains the value, null if it doesn't exist
* @return Binary tree which contains the value, null if it doesn't exist
*/
public BinaryTree search(T data){
public BinaryTree search(T data) {
int cpr = data.compareTo(this.data); //new value comparission respect to actual value
if (cpr < 0) {
@ -72,18 +76,20 @@ public class BinaryTree<T extends Comparable> {
/**
* Checks if the data value exist in the tree
*
* @param data data to be searched
* @return true if this tree contains the data value, false if not.
*/
public boolean contains(T data){
public boolean contains(T data) {
return this.search(data) != null;
}
/**
* uses recursive black magic to print this tree in console
*
* @param tabCounter prev tabs
*/
private void print(int tabCounter){
private void print(int tabCounter) {
for (int i = 0; i < tabCounter; i++)
System.out.print("\t");
@ -98,7 +104,7 @@ public class BinaryTree<T extends Comparable> {
/**
* uses black magic to print this tree in console
*/
public void print(){
public void print() {
this.print(0);
}

View File

@ -15,7 +15,6 @@ import java.util.*;
* <p>
* 2. quickly query two elements whether contained in the same set, requiring about O(1) time.
*
* @author yangxf
*/
public class DisjointSet<T> implements Serializable {
private static final long serialVersionUID = 3134700471905625636L;

View File

@ -25,14 +25,14 @@ public class SimplexNoise {
this.persistance = persistence;
this.seed = seed;
int octaveCount = (int)Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D));
int octaveCount = (int) Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D));
this.octaves = new SimplexNoiseOctave[octaveCount];
this.frequencys = new double[octaveCount];
this.amplitudes = new double[octaveCount];
Random random = new Random(seed);
for(int index = 0; index < octaveCount; index++) {
for (int index = 0; index < octaveCount; index++) {
this.octaves[index] = new SimplexNoiseOctave(random.nextInt());
this.frequencys[index] = Math.pow(2, index);
@ -42,6 +42,7 @@ public class SimplexNoise {
/**
* Generates a height map.
*
* @param x X coordinate
* @param y Y coordinate
* @param width width
@ -55,13 +56,13 @@ public class SimplexNoise {
float[][] result = new float[width][height];
for(int i = 0; i < width; i++) {
for (int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
for (int j = 0; j < height; j++) {
int posX = x + i * ((xEnd - x) / width);
int posY = y + j * ((yEnd - y) / height);
result[i][j] = Math.min(1.0F, Math.max(0.0F, (float)(0.5D * (1 + this.getNoise(posX, posY)))));
result[i][j] = Math.min(1.0F, Math.max(0.0F, (float) (0.5D * (1 + this.getNoise(posX, posY)))));
}
}
@ -70,6 +71,7 @@ public class SimplexNoise {
/**
* Generates a two dimensional noise.
*
* @param x X coordinate
* @param y Y coordinate
* @return the generated noise
@ -78,7 +80,7 @@ public class SimplexNoise {
double result = 0;
for(int index = 0; index < this.octaves.length; index++) {
for (int index = 0; index < this.octaves.length; index++) {
result += this.octaves[index].noise(x / this.frequencys[index], y / this.frequencys[index]) * this.amplitudes[index];
}
@ -88,6 +90,7 @@ public class SimplexNoise {
/**
* Generates a three dimensional noise.
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
@ -97,7 +100,7 @@ public class SimplexNoise {
double result = 0;
for(int index = 0; index < this.octaves.length; index++) {
for (int index = 0; index < this.octaves.length; index++) {
double frequency = Math.pow(2, index);
double amplitude = Math.pow(this.persistance, this.octaves.length - index);

View File

@ -14,7 +14,7 @@ public class SimplexNoiseOctave {
new Gradient(1, 0, -1), new Gradient(-1, 0, -1), new Gradient(0, 1, 1),
new Gradient(0, -1, 1), new Gradient(0, 1, -1), new Gradient(0, -1, -1)
};
private static final short P_SUPPLY[] = {
private static final short[] P_SUPPLY = {
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,

View File

@ -16,7 +16,6 @@ import java.util.function.Function;
* <p>
* The accuracy rate depends on capacity and hash functions.
*
* @author yangxf
*/
public class BloomFilter implements Serializable {
private static final long serialVersionUID = -4466610350741278658L;

View File

@ -15,7 +15,7 @@ public final class LinearSearch {
* @param <T> is any comparable type
* @return index of the element
*/
public static <T extends Comparable<T>> int findIndex(T array[], T key) {
public static <T extends Comparable<T>> int findIndex(T[] array, T key) {
return search(array, key);
}
@ -24,7 +24,7 @@ public final class LinearSearch {
* @param key The element you are looking for
* @return the location of the key or -1 if the element is not found
**/
private static <T extends Comparable<T>> int search(T array[], T key){
private static <T extends Comparable<T>> int search(T[] array, T key){
for(int i = 0; i < array.length;i++) {
if (array[i].compareTo(key) == 0){
return i;

View File

@ -9,7 +9,6 @@ import static src.main.java.com.sorts.SortUtils.swap;
public class HeapSort {
private static class Heap<T extends Comparable<T>> {
/**
* Array to store heap

View File

@ -5,7 +5,6 @@ import static src.main.java.com.sorts.SortUtils.swap;
public class QuickSort {
/**
* This method implements the Generic Quick Sort
*

View File

@ -8,7 +8,7 @@ public class StoogeSort {
/**
* This method implements recursion StoogeSort
*
* @param int[] array to store number elements
* @param arr array to store number elements
* @param f first element in the array
* @param l last element in the array
*/

View File

@ -3,5 +3,5 @@ package src.main.java.com.types;
@FunctionalInterface
public interface Sort<T> {
public <T extends Comparable<T>> T[] sort(T[] array);
<T extends Comparable<T>> T[] sort(T[] array);
}

View File

@ -1,12 +1,10 @@
package src.main.java.com.dataStructures;
package src.test.java.com.dataStructures;
import org.junit.Test;
import src.main.java.com.dataStructures.BinaryTree;
import static org.junit.Assert.*;
/**
*
* @author RICARDO
*/
public class BinaryTreeTest {
public BinaryTreeTest() {
@ -16,15 +14,16 @@ public class BinaryTreeTest {
* Test of insert method, of class BinaryTree.
*/
@Test
public void testInsert_BinaryTree() {
public void testInsertBinaryTree() {
System.out.println("insert");
BinaryTree<String> lowerdata = new BinaryTree<>("1");
BinaryTree<String> upperdata = new BinaryTree<>("3");
BinaryTree<String> lowerData = new BinaryTree<>("1");
BinaryTree<String> upperData = new BinaryTree<>("3");
BinaryTree<String> instance = new BinaryTree<>("2");
instance.insert(lowerdata);
instance.insert(upperdata);
String proof = instance.getLeft().toString()+instance.toString()+instance.getRight().toString();
System.out.println(proof);
instance.insert(lowerData);
instance.insert(upperData);
String proof = instance.getLeft().toString()
+ instance.toString()
+ instance.getRight().toString();
assertEquals("123", proof);
}
@ -36,10 +35,10 @@ public class BinaryTreeTest {
System.out.println("search");
BinaryTree<Integer> instance = new BinaryTree<>(5);
for (int i = 1; i < 10; i++) {
instance.insert(new Integer(i));
instance.insert(i);
}
BinaryTree result = instance.search(new Integer(1));
assertEquals(new Integer(1), result.getData());
BinaryTree result = instance.search(1);
assertEquals(1, result.getData());
}
/**
@ -53,8 +52,7 @@ public class BinaryTreeTest {
instance.insert(i);
}
boolean result = instance.contains(2)&&instance.contains(11);
assertEquals(false, result);
boolean result = instance.contains(2) && instance.contains(11);
assertFalse(result);
}
}