Merge branch 'master' of https://github.com/eugenp/tutorials into task/JAVA-12424
This commit is contained in:
commit
b61075c113
|
@ -4,6 +4,4 @@ This module contains articles about Apache CXF
|
||||||
|
|
||||||
## Relevant Articles:
|
## Relevant Articles:
|
||||||
|
|
||||||
- [Apache CXF Support for RESTful Web Services](https://www.baeldung.com/apache-cxf-rest-api)
|
- [Introduction to Apache CXF Aegis Data Binding](https://www.baeldung.com/aegis-data-binding-in-apache-cxf)
|
||||||
- [A Guide to Apache CXF with Spring](https://www.baeldung.com/apache-cxf-with-spring)
|
|
||||||
- [Introduction to Apache CXF](https://www.baeldung.com/introduction-to-apache-cxf)
|
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Introduction to Apache CXF](http://www.baeldung.com/introduction-to-apache-cxf)
|
- [Introduction to Apache CXF](https://www.baeldung.com/introduction-to-apache-cxf)
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.httpclient.parameters;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class HttpClientParametersLiveTest {
|
||||||
|
|
||||||
|
private static HttpClient client;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void setUp() {
|
||||||
|
client = HttpClient.newHttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenQueryParams_whenGetRequest_thenResponseOk() throws IOException, InterruptedException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertEquals(response.statusCode(), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenQueryParams_whenGetRequestWithDefaultConfiguration_thenResponseOk() throws IOException, InterruptedException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertEquals(response.statusCode(), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -13,5 +13,4 @@ This module contains articles about the Java List collection
|
||||||
- [Finding the Differences Between Two Lists in Java](https://www.baeldung.com/java-lists-difference)
|
- [Finding the Differences Between Two Lists in Java](https://www.baeldung.com/java-lists-difference)
|
||||||
- [List vs. ArrayList in Java](https://www.baeldung.com/java-list-vs-arraylist)
|
- [List vs. ArrayList in Java](https://www.baeldung.com/java-list-vs-arraylist)
|
||||||
- [How to Store HashMap<String, ArrayList> Inside a List](https://www.baeldung.com/java-hashmap-inside-list)
|
- [How to Store HashMap<String, ArrayList> Inside a List](https://www.baeldung.com/java-hashmap-inside-list)
|
||||||
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
|
|
||||||
- [[<-- Prev]](/core-java-modules/core-java-collections-list-2)
|
- [[<-- Prev]](/core-java-modules/core-java-collections-list-2)
|
||||||
|
|
|
@ -5,4 +5,5 @@ This module contains articles about the Java List collection
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
|
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
|
||||||
- [Reverse an ArrayList in Java](https://www.baeldung.com/java-reverse-arraylist)
|
- [Reverse an ArrayList in Java](https://www.baeldung.com/java-reverse-arraylist)
|
||||||
|
- [Sort a List Alphabetically in Java](https://www.baeldung.com/java-sort-list-alphabetically)
|
||||||
- [[<-- Prev]](/core-java-modules/core-java-collections-list-3)
|
- [[<-- Prev]](/core-java-modules/core-java-collections-list-3)
|
||||||
|
|
|
@ -13,4 +13,4 @@ This module contains articles about numbers in Java.
|
||||||
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
|
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
|
||||||
- [Finding the Least Common Multiple in Java](https://www.baeldung.com/java-least-common-multiple)
|
- [Finding the Least Common Multiple in Java](https://www.baeldung.com/java-least-common-multiple)
|
||||||
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
|
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
|
||||||
- More articles: [[<-- prev]](/java-numbers) [[next -->]](/java-numbers-3)
|
- More articles: [[<-- prev]](../java-numbers) [[next -->]](../java-numbers-3)
|
|
@ -9,10 +9,9 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -1,140 +1,140 @@
|
||||||
package com.baeldung.binarynumbers;
|
package com.baeldung.binarynumbers;
|
||||||
|
|
||||||
public class BinaryNumbers {
|
public class BinaryNumbers {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method takes a decimal number and convert it into a binary number.
|
* This method takes a decimal number and convert it into a binary number.
|
||||||
* example:- input:10, output:1010
|
* example:- input:10, output:1010
|
||||||
*
|
*
|
||||||
* @param decimalNumber
|
* @param decimalNumber
|
||||||
* @return binary number
|
* @return binary number
|
||||||
*/
|
*/
|
||||||
public Integer convertDecimalToBinary(Integer decimalNumber) {
|
public Integer convertDecimalToBinary(Integer decimalNumber) {
|
||||||
|
|
||||||
if (decimalNumber == 0) {
|
if (decimalNumber == 0) {
|
||||||
return decimalNumber;
|
return decimalNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder binaryNumber = new StringBuilder();
|
StringBuilder binaryNumber = new StringBuilder();
|
||||||
Integer quotient = decimalNumber;
|
Integer quotient = decimalNumber;
|
||||||
|
|
||||||
while (quotient > 0) {
|
while (quotient > 0) {
|
||||||
|
|
||||||
int remainder = quotient % 2;
|
int remainder = quotient % 2;
|
||||||
binaryNumber.append(remainder);
|
binaryNumber.append(remainder);
|
||||||
quotient /= 2;
|
quotient /= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
binaryNumber = binaryNumber.reverse();
|
binaryNumber = binaryNumber.reverse();
|
||||||
return Integer.valueOf(binaryNumber.toString());
|
return Integer.valueOf(binaryNumber.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method takes a binary number and convert it into a decimal number.
|
* This method takes a binary number and convert it into a decimal number.
|
||||||
* example:- input:101, output:5
|
* example:- input:101, output:5
|
||||||
*
|
*
|
||||||
* @param binary number
|
* @param binary number
|
||||||
* @return decimal Number
|
* @return decimal Number
|
||||||
*/
|
*/
|
||||||
public Integer convertBinaryToDecimal(Integer binaryNumber) {
|
public Integer convertBinaryToDecimal(Integer binaryNumber) {
|
||||||
|
|
||||||
Integer decimalNumber = 0;
|
Integer decimalNumber = 0;
|
||||||
Integer base = 1;
|
Integer base = 1;
|
||||||
|
|
||||||
while (binaryNumber > 0) {
|
while (binaryNumber > 0) {
|
||||||
|
|
||||||
int lastDigit = binaryNumber % 10;
|
int lastDigit = binaryNumber % 10;
|
||||||
binaryNumber = binaryNumber / 10;
|
binaryNumber = binaryNumber / 10;
|
||||||
|
|
||||||
decimalNumber += lastDigit * base;
|
decimalNumber += lastDigit * base;
|
||||||
base = base * 2;
|
base = base * 2;
|
||||||
}
|
}
|
||||||
return decimalNumber;
|
return decimalNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method accepts two binary numbers and returns sum of input numbers.
|
* This method accepts two binary numbers and returns sum of input numbers.
|
||||||
* Example:- firstNum: 101, secondNum: 100, output: 1001
|
* Example:- firstNum: 101, secondNum: 100, output: 1001
|
||||||
*
|
*
|
||||||
* @param firstNum
|
* @param firstNum
|
||||||
* @param secondNum
|
* @param secondNum
|
||||||
* @return addition of input numbers
|
* @return addition of input numbers
|
||||||
*/
|
*/
|
||||||
public Integer addBinaryNumber(Integer firstNum, Integer secondNum) {
|
public Integer addBinaryNumber(Integer firstNum, Integer secondNum) {
|
||||||
|
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
|
|
||||||
int carry = 0;
|
int carry = 0;
|
||||||
int temp;
|
int temp;
|
||||||
|
|
||||||
while (firstNum != 0 || secondNum != 0) {
|
while (firstNum != 0 || secondNum != 0) {
|
||||||
|
|
||||||
temp = (firstNum % 10 + secondNum % 10 + carry) % 2;
|
temp = (firstNum % 10 + secondNum % 10 + carry) % 2;
|
||||||
output.append(temp);
|
output.append(temp);
|
||||||
|
|
||||||
carry = (firstNum % 10 + secondNum % 10 + carry) / 2;
|
carry = (firstNum % 10 + secondNum % 10 + carry) / 2;
|
||||||
|
|
||||||
firstNum = firstNum / 10;
|
firstNum = firstNum / 10;
|
||||||
secondNum = secondNum / 10;
|
secondNum = secondNum / 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (carry != 0) {
|
if (carry != 0) {
|
||||||
output.append(carry);
|
output.append(carry);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Integer.valueOf(output.reverse()
|
return Integer.valueOf(output.reverse()
|
||||||
.toString());
|
.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method takes two binary number as input and subtract second number from the first number.
|
* This method takes two binary number as input and subtract second number from the first number.
|
||||||
* example:- firstNum: 1000, secondNum: 11, output: 101
|
* example:- firstNum: 1000, secondNum: 11, output: 101
|
||||||
* @param firstNum
|
* @param firstNum
|
||||||
* @param secondNum
|
* @param secondNum
|
||||||
* @return Result of subtraction of secondNum from first
|
* @return Result of subtraction of secondNum from first
|
||||||
*/
|
*/
|
||||||
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
|
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
|
||||||
|
|
||||||
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
|
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
int carry = 0;
|
int carry = 0;
|
||||||
int temp;
|
int temp;
|
||||||
|
|
||||||
while (firstNum != 0 || onesComplement != 0) {
|
while (firstNum != 0 || onesComplement != 0) {
|
||||||
|
|
||||||
temp = (firstNum % 10 + onesComplement % 10 + carry) % 2;
|
temp = (firstNum % 10 + onesComplement % 10 + carry) % 2;
|
||||||
output.append(temp);
|
output.append(temp);
|
||||||
|
|
||||||
carry = (firstNum % 10 + onesComplement % 10 + carry) / 2;
|
carry = (firstNum % 10 + onesComplement % 10 + carry) / 2;
|
||||||
|
|
||||||
firstNum = firstNum / 10;
|
firstNum = firstNum / 10;
|
||||||
onesComplement = onesComplement / 10;
|
onesComplement = onesComplement / 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
String additionOfFirstNumAndOnesComplement = output.reverse()
|
String additionOfFirstNumAndOnesComplement = output.reverse()
|
||||||
.toString();
|
.toString();
|
||||||
|
|
||||||
if (carry == 1) {
|
if (carry == 1) {
|
||||||
return addBinaryNumber(Integer.valueOf(additionOfFirstNumAndOnesComplement), carry);
|
return addBinaryNumber(Integer.valueOf(additionOfFirstNumAndOnesComplement), carry);
|
||||||
} else {
|
} else {
|
||||||
return getOnesComplement(Integer.valueOf(additionOfFirstNumAndOnesComplement));
|
return getOnesComplement(Integer.valueOf(additionOfFirstNumAndOnesComplement));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getOnesComplement(Integer num) {
|
public Integer getOnesComplement(Integer num) {
|
||||||
|
|
||||||
StringBuilder onesComplement = new StringBuilder();
|
StringBuilder onesComplement = new StringBuilder();
|
||||||
while (num > 0) {
|
while (num > 0) {
|
||||||
int lastDigit = num % 10;
|
int lastDigit = num % 10;
|
||||||
if (lastDigit == 0) {
|
if (lastDigit == 0) {
|
||||||
onesComplement.append(1);
|
onesComplement.append(1);
|
||||||
} else {
|
} else {
|
||||||
onesComplement.append(0);
|
onesComplement.append(0);
|
||||||
}
|
}
|
||||||
num = num / 10;
|
num = num / 10;
|
||||||
}
|
}
|
||||||
return Integer.valueOf(onesComplement.reverse()
|
return Integer.valueOf(onesComplement.reverse()
|
||||||
.toString());
|
.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,73 +1,73 @@
|
||||||
package com.baeldung.binarynumbers;
|
package com.baeldung.binarynumbers;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class BinaryNumbersUnitTest {
|
public class BinaryNumbersUnitTest {
|
||||||
|
|
||||||
private BinaryNumbers binaryNumbers = new BinaryNumbers();
|
private BinaryNumbers binaryNumbers = new BinaryNumbers();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_decimalNumber_then_returnBinaryNumber() {
|
public void given_decimalNumber_then_returnBinaryNumber() {
|
||||||
assertEquals(Integer.valueOf(1000), binaryNumbers.convertDecimalToBinary(8));
|
assertEquals(Integer.valueOf(1000), binaryNumbers.convertDecimalToBinary(8));
|
||||||
assertEquals(Integer.valueOf(10100), binaryNumbers.convertDecimalToBinary(20));
|
assertEquals(Integer.valueOf(10100), binaryNumbers.convertDecimalToBinary(20));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_decimalNumber_then_convertToBinaryNumber() {
|
public void given_decimalNumber_then_convertToBinaryNumber() {
|
||||||
assertEquals("1000", Integer.toBinaryString(8));
|
assertEquals("1000", Integer.toBinaryString(8));
|
||||||
assertEquals("10100", Integer.toBinaryString(20));
|
assertEquals("10100", Integer.toBinaryString(20));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_binaryNumber_then_ConvertToDecimalNumber() {
|
public void given_binaryNumber_then_ConvertToDecimalNumber() {
|
||||||
assertEquals(8, Integer.parseInt("1000", 2));
|
assertEquals(8, Integer.parseInt("1000", 2));
|
||||||
assertEquals(20, Integer.parseInt("10100", 2));
|
assertEquals(20, Integer.parseInt("10100", 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_binaryNumber_then_returnDecimalNumber() {
|
public void given_binaryNumber_then_returnDecimalNumber() {
|
||||||
assertEquals(Integer.valueOf(8), binaryNumbers.convertBinaryToDecimal(1000));
|
assertEquals(Integer.valueOf(8), binaryNumbers.convertBinaryToDecimal(1000));
|
||||||
assertEquals(Integer.valueOf(20), binaryNumbers.convertBinaryToDecimal(10100));
|
assertEquals(Integer.valueOf(20), binaryNumbers.convertBinaryToDecimal(10100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_twoBinaryNumber_then_returnAddition() {
|
public void given_twoBinaryNumber_then_returnAddition() {
|
||||||
// adding 4 and 10
|
// adding 4 and 10
|
||||||
assertEquals(Integer.valueOf(1110), binaryNumbers.addBinaryNumber(100, 1010));
|
assertEquals(Integer.valueOf(1110), binaryNumbers.addBinaryNumber(100, 1010));
|
||||||
|
|
||||||
// adding 26 and 14
|
// adding 26 and 14
|
||||||
assertEquals(Integer.valueOf(101000), binaryNumbers.addBinaryNumber(11010, 1110));
|
assertEquals(Integer.valueOf(101000), binaryNumbers.addBinaryNumber(11010, 1110));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_twoBinaryNumber_then_returnSubtraction() {
|
public void given_twoBinaryNumber_then_returnSubtraction() {
|
||||||
// subtracting 16 from 25
|
// subtracting 16 from 25
|
||||||
assertEquals(Integer.valueOf(1001), binaryNumbers.substractBinaryNumber(11001, 10000));
|
assertEquals(Integer.valueOf(1001), binaryNumbers.substractBinaryNumber(11001, 10000));
|
||||||
|
|
||||||
// subtracting 29 from 16, the output here is negative
|
// subtracting 29 from 16, the output here is negative
|
||||||
assertEquals(Integer.valueOf(1101), binaryNumbers.substractBinaryNumber(10000, 11101));
|
assertEquals(Integer.valueOf(1101), binaryNumbers.substractBinaryNumber(10000, 11101));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_binaryLiteral_thenReturnDecimalValue() {
|
public void given_binaryLiteral_thenReturnDecimalValue() {
|
||||||
|
|
||||||
byte five = 0b101;
|
byte five = 0b101;
|
||||||
assertEquals((byte) 5, five);
|
assertEquals((byte) 5, five);
|
||||||
|
|
||||||
short three = 0b11;
|
short three = 0b11;
|
||||||
assertEquals((short) 3, three);
|
assertEquals((short) 3, three);
|
||||||
|
|
||||||
int nine = 0B1001;
|
int nine = 0B1001;
|
||||||
assertEquals(9, nine);
|
assertEquals(9, nine);
|
||||||
|
|
||||||
long twentyNine = 0B11101;
|
long twentyNine = 0B11101;
|
||||||
assertEquals(29, twentyNine);
|
assertEquals(29, twentyNine);
|
||||||
|
|
||||||
int minusThirtySeven = -0B100101;
|
int minusThirtySeven = -0B100101;
|
||||||
assertEquals(-37, minusThirtySeven);
|
assertEquals(-37, minusThirtySeven);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -14,4 +14,4 @@ This module contains articles about numbers in Java.
|
||||||
- [Print an Integer in Binary Format in Java](https://www.baeldung.com/java-print-integer-binary)
|
- [Print an Integer in Binary Format in Java](https://www.baeldung.com/java-print-integer-binary)
|
||||||
- [Number Formatting in Java](https://www.baeldung.com/java-number-formatting)
|
- [Number Formatting in Java](https://www.baeldung.com/java-number-formatting)
|
||||||
- [Division by Zero in Java: Exception, Infinity, or Not a Number](https://www.baeldung.com/java-division-by-zero)
|
- [Division by Zero in Java: Exception, Infinity, or Not a Number](https://www.baeldung.com/java-division-by-zero)
|
||||||
- More articles: [[<-- prev]](/java-numbers-2)
|
- More articles: [[<-- prev]](../java-numbers-2) [[next -->]](../java-numbers-4)
|
|
@ -7,10 +7,9 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -9,3 +9,5 @@
|
||||||
- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int)
|
- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int)
|
||||||
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
|
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
|
||||||
- [Reverse a Number in Java](https://www.baeldung.com/java-reverse-number)
|
- [Reverse a Number in Java](https://www.baeldung.com/java-reverse-number)
|
||||||
|
- [Check if BigDecimal Value Is Zero](https://www.baeldung.com/java-bigdecimal-zero)
|
||||||
|
- More articles: [[<-- prev]](../java-numbers-3)
|
|
@ -7,10 +7,9 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -13,4 +13,4 @@ This module contains articles about numbers in Java.
|
||||||
- [Convert Double to String, Removing Decimal Places](https://www.baeldung.com/java-double-to-string)
|
- [Convert Double to String, Removing Decimal Places](https://www.baeldung.com/java-double-to-string)
|
||||||
- [Changing the Order in a Sum Operation Can Produce Different Results?](https://www.baeldung.com/java-floating-point-sum-order)
|
- [Changing the Order in a Sum Operation Can Produce Different Results?](https://www.baeldung.com/java-floating-point-sum-order)
|
||||||
- [Using Math.sin with Degrees](https://www.baeldung.com/java-math-sin-degrees)
|
- [Using Math.sin with Degrees](https://www.baeldung.com/java-math-sin-degrees)
|
||||||
- More articles: [[next -->]](/../java-numbers-2)
|
- More articles: [[next -->]](../java-numbers-2)
|
|
@ -9,10 +9,9 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -1,51 +1,51 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
public class FloatingPointArithmetic {
|
public class FloatingPointArithmetic {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
double a = 13.22;
|
double a = 13.22;
|
||||||
double b = 4.88;
|
double b = 4.88;
|
||||||
double c = 21.45;
|
double c = 21.45;
|
||||||
|
|
||||||
System.out.println("a = " + a);
|
System.out.println("a = " + a);
|
||||||
System.out.println("b = " + b);
|
System.out.println("b = " + b);
|
||||||
System.out.println("c = " + c);
|
System.out.println("c = " + c);
|
||||||
|
|
||||||
double sum_ab = a + b;
|
double sum_ab = a + b;
|
||||||
System.out.println("a + b = " + sum_ab);
|
System.out.println("a + b = " + sum_ab);
|
||||||
|
|
||||||
double abc = a + b + c;
|
double abc = a + b + c;
|
||||||
System.out.println("a + b + c = " + abc);
|
System.out.println("a + b + c = " + abc);
|
||||||
|
|
||||||
double ab_c = sum_ab + c;
|
double ab_c = sum_ab + c;
|
||||||
System.out.println("ab + c = " + ab_c);
|
System.out.println("ab + c = " + ab_c);
|
||||||
|
|
||||||
double sum_ac = a + c;
|
double sum_ac = a + c;
|
||||||
System.out.println("a + c = " + sum_ac);
|
System.out.println("a + c = " + sum_ac);
|
||||||
|
|
||||||
double acb = a + c + b;
|
double acb = a + c + b;
|
||||||
System.out.println("a + c + b = " + acb);
|
System.out.println("a + c + b = " + acb);
|
||||||
|
|
||||||
double ac_b = sum_ac + b;
|
double ac_b = sum_ac + b;
|
||||||
System.out.println("ac + b = " + ac_b);
|
System.out.println("ac + b = " + ac_b);
|
||||||
|
|
||||||
double ab = 18.1;
|
double ab = 18.1;
|
||||||
double ac = 34.67;
|
double ac = 34.67;
|
||||||
double sum_ab_c = ab + c;
|
double sum_ab_c = ab + c;
|
||||||
double sum_ac_b = ac + b;
|
double sum_ac_b = ac + b;
|
||||||
System.out.println("ab + c = " + sum_ab_c);
|
System.out.println("ab + c = " + sum_ab_c);
|
||||||
System.out.println("ac + b = " + sum_ac_b);
|
System.out.println("ac + b = " + sum_ac_b);
|
||||||
|
|
||||||
BigDecimal d = new BigDecimal(String.valueOf(a));
|
BigDecimal d = new BigDecimal(String.valueOf(a));
|
||||||
BigDecimal e = new BigDecimal(String.valueOf(b));
|
BigDecimal e = new BigDecimal(String.valueOf(b));
|
||||||
BigDecimal f = new BigDecimal(String.valueOf(c));
|
BigDecimal f = new BigDecimal(String.valueOf(c));
|
||||||
|
|
||||||
BigDecimal def = d.add(e).add(f);
|
BigDecimal def = d.add(e).add(f);
|
||||||
BigDecimal dfe = d.add(f).add(e);
|
BigDecimal dfe = d.add(f).add(e);
|
||||||
|
|
||||||
System.out.println("d + e + f = " + def);
|
System.out.println("d + e + f = " + def);
|
||||||
System.out.println("d + f + e = " + dfe);
|
System.out.println("d + f + e = " + dfe);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,67 +1,67 @@
|
||||||
package com.baeldung.numberofdigits;
|
package com.baeldung.numberofdigits;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.openjdk.jmh.annotations.Benchmark;
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
import org.openjdk.jmh.annotations.Mode;
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
import org.openjdk.jmh.annotations.Scope;
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
import org.openjdk.jmh.annotations.State;
|
import org.openjdk.jmh.annotations.State;
|
||||||
import org.openjdk.jmh.runner.RunnerException;
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
|
|
||||||
public class Benchmarking {
|
public class Benchmarking {
|
||||||
public static void main(String[] args) throws RunnerException, IOException {
|
public static void main(String[] args) throws RunnerException, IOException {
|
||||||
org.openjdk.jmh.Main.main(args);
|
org.openjdk.jmh.Main.main(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@State(Scope.Thread)
|
@State(Scope.Thread)
|
||||||
public static class ExecutionPlan {
|
public static class ExecutionPlan {
|
||||||
public int number = Integer.MAX_VALUE;
|
public int number = Integer.MAX_VALUE;
|
||||||
public int length = 0;
|
public int length = 0;
|
||||||
public NumberOfDigits numberOfDigits= new NumberOfDigits();
|
public NumberOfDigits numberOfDigits= new NumberOfDigits();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
public void stringBasedSolution(ExecutionPlan plan) {
|
public void stringBasedSolution(ExecutionPlan plan) {
|
||||||
plan.length = plan.numberOfDigits.stringBasedSolution(plan.number);
|
plan.length = plan.numberOfDigits.stringBasedSolution(plan.number);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
public void logarithmicApproach(ExecutionPlan plan) {
|
public void logarithmicApproach(ExecutionPlan plan) {
|
||||||
plan.length = plan.numberOfDigits.logarithmicApproach(plan.number);
|
plan.length = plan.numberOfDigits.logarithmicApproach(plan.number);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
public void repeatedMultiplication(ExecutionPlan plan) {
|
public void repeatedMultiplication(ExecutionPlan plan) {
|
||||||
plan.length = plan.numberOfDigits.repeatedMultiplication(plan.number);
|
plan.length = plan.numberOfDigits.repeatedMultiplication(plan.number);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
public void shiftOperators(ExecutionPlan plan) {
|
public void shiftOperators(ExecutionPlan plan) {
|
||||||
plan.length = plan.numberOfDigits.shiftOperators(plan.number);
|
plan.length = plan.numberOfDigits.shiftOperators(plan.number);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
public void dividingWithPowersOf2(ExecutionPlan plan) {
|
public void dividingWithPowersOf2(ExecutionPlan plan) {
|
||||||
plan.length = plan.numberOfDigits.dividingWithPowersOf2(plan.number);
|
plan.length = plan.numberOfDigits.dividingWithPowersOf2(plan.number);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
public void divideAndConquer(ExecutionPlan plan) {
|
public void divideAndConquer(ExecutionPlan plan) {
|
||||||
plan.length = plan.numberOfDigits.divideAndConquer(plan.number);
|
plan.length = plan.numberOfDigits.divideAndConquer(plan.number);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,97 +1,97 @@
|
||||||
package com.baeldung.numberofdigits;
|
package com.baeldung.numberofdigits;
|
||||||
|
|
||||||
public class NumberOfDigits {
|
public class NumberOfDigits {
|
||||||
public int stringBasedSolution(int number) {
|
public int stringBasedSolution(int number) {
|
||||||
int length = String.valueOf(number).length();
|
int length = String.valueOf(number).length();
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int logarithmicApproach(int number) {
|
public int logarithmicApproach(int number) {
|
||||||
int length = (int) Math.log10(number) + 1;
|
int length = (int) Math.log10(number) + 1;
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int repeatedMultiplication(int number) {
|
public int repeatedMultiplication(int number) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
long temp = 1;
|
long temp = 1;
|
||||||
while(temp <= number) {
|
while(temp <= number) {
|
||||||
length++;
|
length++;
|
||||||
temp *= 10;
|
temp *= 10;
|
||||||
}
|
}
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int shiftOperators(int number) {
|
public int shiftOperators(int number) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
long temp = 1;
|
long temp = 1;
|
||||||
while(temp <= number) {
|
while(temp <= number) {
|
||||||
length++;
|
length++;
|
||||||
temp = (temp << 3) + (temp << 1);
|
temp = (temp << 3) + (temp << 1);
|
||||||
}
|
}
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int dividingWithPowersOf2(int number) {
|
public int dividingWithPowersOf2(int number) {
|
||||||
int length = 1;
|
int length = 1;
|
||||||
if (number >= 100000000) {
|
if (number >= 100000000) {
|
||||||
length += 8;
|
length += 8;
|
||||||
number /= 100000000;
|
number /= 100000000;
|
||||||
}
|
}
|
||||||
if (number >= 10000) {
|
if (number >= 10000) {
|
||||||
length += 4;
|
length += 4;
|
||||||
number /= 10000;
|
number /= 10000;
|
||||||
}
|
}
|
||||||
if (number >= 100) {
|
if (number >= 100) {
|
||||||
length += 2;
|
length += 2;
|
||||||
number /= 100;
|
number /= 100;
|
||||||
}
|
}
|
||||||
if (number >= 10) {
|
if (number >= 10) {
|
||||||
length += 1;
|
length += 1;
|
||||||
}
|
}
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int divideAndConquer(int number) {
|
public int divideAndConquer(int number) {
|
||||||
if (number < 100000){
|
if (number < 100000){
|
||||||
// 5 digits or less
|
// 5 digits or less
|
||||||
if (number < 100){
|
if (number < 100){
|
||||||
// 1 or 2
|
// 1 or 2
|
||||||
if (number < 10)
|
if (number < 10)
|
||||||
return 1;
|
return 1;
|
||||||
else
|
else
|
||||||
return 2;
|
return 2;
|
||||||
}else{
|
}else{
|
||||||
// 3 to 5 digits
|
// 3 to 5 digits
|
||||||
if (number < 1000)
|
if (number < 1000)
|
||||||
return 3;
|
return 3;
|
||||||
else{
|
else{
|
||||||
// 4 or 5 digits
|
// 4 or 5 digits
|
||||||
if (number < 10000)
|
if (number < 10000)
|
||||||
return 4;
|
return 4;
|
||||||
else
|
else
|
||||||
return 5;
|
return 5;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 6 digits or more
|
// 6 digits or more
|
||||||
if (number < 10000000) {
|
if (number < 10000000) {
|
||||||
// 6 or 7 digits
|
// 6 or 7 digits
|
||||||
if (number < 1000000)
|
if (number < 1000000)
|
||||||
return 6;
|
return 6;
|
||||||
else
|
else
|
||||||
return 7;
|
return 7;
|
||||||
} else {
|
} else {
|
||||||
// 8 to 10 digits
|
// 8 to 10 digits
|
||||||
if (number < 100000000)
|
if (number < 100000000)
|
||||||
return 8;
|
return 8;
|
||||||
else {
|
else {
|
||||||
// 9 or 10 digits
|
// 9 or 10 digits
|
||||||
if (number < 1000000000)
|
if (number < 1000000000)
|
||||||
return 9;
|
return 9;
|
||||||
else
|
else
|
||||||
return 10;
|
return 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,35 +1,35 @@
|
||||||
package com.baeldung.numberofdigits;
|
package com.baeldung.numberofdigits;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
public class NumberOfDigitsDriver {
|
public class NumberOfDigitsDriver {
|
||||||
private static NumberOfDigits numberOfDigits;
|
private static NumberOfDigits numberOfDigits;
|
||||||
|
|
||||||
private static Logger LOG = Logger.getLogger(NumberOfDigitsDriver.class);
|
private static Logger LOG = Logger.getLogger(NumberOfDigitsDriver.class);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
numberOfDigits = new NumberOfDigits();
|
numberOfDigits = new NumberOfDigits();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
LOG.info("Testing all methods...");
|
LOG.info("Testing all methods...");
|
||||||
|
|
||||||
long length = numberOfDigits.stringBasedSolution(602);
|
long length = numberOfDigits.stringBasedSolution(602);
|
||||||
LOG.info("String Based Solution : " + length);
|
LOG.info("String Based Solution : " + length);
|
||||||
|
|
||||||
length = numberOfDigits.logarithmicApproach(602);
|
length = numberOfDigits.logarithmicApproach(602);
|
||||||
LOG.info("Logarithmic Approach : " + length);
|
LOG.info("Logarithmic Approach : " + length);
|
||||||
|
|
||||||
length = numberOfDigits.repeatedMultiplication(602);
|
length = numberOfDigits.repeatedMultiplication(602);
|
||||||
LOG.info("Repeated Multiplication : " + length);
|
LOG.info("Repeated Multiplication : " + length);
|
||||||
|
|
||||||
length = numberOfDigits.shiftOperators(602);
|
length = numberOfDigits.shiftOperators(602);
|
||||||
LOG.info("Shift Operators : " + length);
|
LOG.info("Shift Operators : " + length);
|
||||||
|
|
||||||
length = numberOfDigits.dividingWithPowersOf2(602);
|
length = numberOfDigits.dividingWithPowersOf2(602);
|
||||||
LOG.info("Dividing with Powers of 2 : " + length);
|
LOG.info("Dividing with Powers of 2 : " + length);
|
||||||
|
|
||||||
length = numberOfDigits.divideAndConquer(602);
|
length = numberOfDigits.divideAndConquer(602);
|
||||||
LOG.info("Divide And Conquer : " + length);
|
LOG.info("Divide And Conquer : " + length);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,25 +1,25 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
public class BigDecimalImplUnitTest {
|
public class BigDecimalImplUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBigDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
|
public void givenBigDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
|
||||||
BigDecimal serviceTax = new BigDecimal("56.0084578639");
|
BigDecimal serviceTax = new BigDecimal("56.0084578639");
|
||||||
serviceTax = serviceTax.setScale(2, RoundingMode.CEILING);
|
serviceTax = serviceTax.setScale(2, RoundingMode.CEILING);
|
||||||
|
|
||||||
BigDecimal entertainmentTax = new BigDecimal("23.00689");
|
BigDecimal entertainmentTax = new BigDecimal("23.00689");
|
||||||
entertainmentTax = entertainmentTax.setScale(2, RoundingMode.FLOOR);
|
entertainmentTax = entertainmentTax.setScale(2, RoundingMode.FLOOR);
|
||||||
|
|
||||||
BigDecimal totalTax = serviceTax.add(entertainmentTax);
|
BigDecimal totalTax = serviceTax.add(entertainmentTax);
|
||||||
BigDecimal result = BigDecimal.valueOf(79.01);
|
BigDecimal result = BigDecimal.valueOf(79.01);
|
||||||
|
|
||||||
Assert.assertEquals(result, totalTax);
|
Assert.assertEquals(result, totalTax);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,21 +1,21 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
public class BigIntegerImplUnitTest {
|
public class BigIntegerImplUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBigIntegerNumbers_whenAddedTogether_thenGetExpectedResult() {
|
public void givenBigIntegerNumbers_whenAddedTogether_thenGetExpectedResult() {
|
||||||
BigInteger numStarsMilkyWay = new BigInteger("8731409320171337804361260816606476");
|
BigInteger numStarsMilkyWay = new BigInteger("8731409320171337804361260816606476");
|
||||||
BigInteger numStarsAndromeda = new BigInteger("5379309320171337804361260816606476");
|
BigInteger numStarsAndromeda = new BigInteger("5379309320171337804361260816606476");
|
||||||
|
|
||||||
BigInteger totalStars = numStarsMilkyWay.add(numStarsAndromeda);
|
BigInteger totalStars = numStarsMilkyWay.add(numStarsAndromeda);
|
||||||
BigInteger result = new BigInteger("14110718640342675608722521633212952");
|
BigInteger result = new BigInteger("14110718640342675608722521633212952");
|
||||||
|
|
||||||
Assert.assertEquals(result, totalStars);
|
Assert.assertEquals(result, totalStars);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,45 +1,45 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class FloatingPointArithmeticUnitTest {
|
public class FloatingPointArithmeticUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
|
public void givenDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
|
||||||
double a = 13.22;
|
double a = 13.22;
|
||||||
double b = 4.88;
|
double b = 4.88;
|
||||||
double c = 21.45;
|
double c = 21.45;
|
||||||
double result = 39.55;
|
double result = 39.55;
|
||||||
|
|
||||||
double abc = a + b + c;
|
double abc = a + b + c;
|
||||||
double acb = a + c + b;
|
double acb = a + c + b;
|
||||||
|
|
||||||
Assert.assertEquals(result, abc, 0);
|
Assert.assertEquals(result, abc, 0);
|
||||||
Assert.assertNotEquals(result, acb, 0);
|
Assert.assertNotEquals(result, acb, 0);
|
||||||
|
|
||||||
double ab = 18.1;
|
double ab = 18.1;
|
||||||
double ac = 34.67;
|
double ac = 34.67;
|
||||||
|
|
||||||
double ab_c = ab + c;
|
double ab_c = ab + c;
|
||||||
double ac_b = ac + b;
|
double ac_b = ac + b;
|
||||||
|
|
||||||
Assert.assertEquals(result, ab_c, 0);
|
Assert.assertEquals(result, ab_c, 0);
|
||||||
Assert.assertNotEquals(result, ac_b, 0);
|
Assert.assertNotEquals(result, ac_b, 0);
|
||||||
|
|
||||||
BigDecimal d = new BigDecimal(String.valueOf(a));
|
BigDecimal d = new BigDecimal(String.valueOf(a));
|
||||||
BigDecimal e = new BigDecimal(String.valueOf(b));
|
BigDecimal e = new BigDecimal(String.valueOf(b));
|
||||||
BigDecimal f = new BigDecimal(String.valueOf(c));
|
BigDecimal f = new BigDecimal(String.valueOf(c));
|
||||||
BigDecimal sum = new BigDecimal("39.55");
|
BigDecimal sum = new BigDecimal("39.55");
|
||||||
|
|
||||||
BigDecimal def = d.add(e).add(f);
|
BigDecimal def = d.add(e).add(f);
|
||||||
BigDecimal dfe = d.add(f).add(e);
|
BigDecimal dfe = d.add(f).add(e);
|
||||||
|
|
||||||
Assert.assertEquals(0, def.compareTo(sum));
|
Assert.assertEquals(0, def.compareTo(sum));
|
||||||
Assert.assertEquals(0, dfe.compareTo(sum));
|
Assert.assertEquals(0, dfe.compareTo(sum));
|
||||||
|
|
||||||
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
|
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,71 +1,71 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
import org.apache.commons.math3.util.Precision;
|
import org.apache.commons.math3.util.Precision;
|
||||||
import org.decimal4j.util.DoubleRounder;
|
import org.decimal4j.util.DoubleRounder;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class RoundUnitTest {
|
public class RoundUnitTest {
|
||||||
private double value = 2.03456d;
|
private double value = 2.03456d;
|
||||||
private int places = 2;
|
private int places = 2;
|
||||||
private double delta = 0.0d;
|
private double delta = 0.0d;
|
||||||
private double expected = 2.03d;
|
private double expected = 2.03d;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() {
|
public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() {
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
Assert.assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
|
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
|
|
||||||
places = 3;
|
places = 3;
|
||||||
expected = 2.035d;
|
expected = 2.035d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
Assert.assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
|
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
|
|
||||||
value = 1000.0d;
|
value = 1000.0d;
|
||||||
places = 17;
|
places = 17;
|
||||||
expected = 1000.0d;
|
expected = 1000.0d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
Assert.assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 92.23372036854776 !
|
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 92.23372036854776 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
|
|
||||||
value = 256.025d;
|
value = 256.025d;
|
||||||
places = 2;
|
places = 2;
|
||||||
expected = 256.03d;
|
expected = 256.03d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
Assert.assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 256.02 !
|
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 256.02 !
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 256.02 !
|
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 256.02 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 256.02 !
|
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 256.02 !
|
||||||
|
|
||||||
value = 260.775d;
|
value = 260.775d;
|
||||||
places = 2;
|
places = 2;
|
||||||
expected = 260.78d;
|
expected = 260.78d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
Assert.assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 260.77 !
|
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 260.77 !
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 260.77 !
|
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 260.77 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 260.77 !
|
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 260.77 !
|
||||||
|
|
||||||
value = 90080070060.1d;
|
value = 90080070060.1d;
|
||||||
places = 9;
|
places = 9;
|
||||||
expected = 90080070060.1d;
|
expected = 90080070060.1d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
Assert.assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 9.223372036854776E9 !
|
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 9.223372036854776E9 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,106 +1,106 @@
|
||||||
package com.baeldung.numberofdigits;
|
package com.baeldung.numberofdigits;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Assume;
|
import org.junit.Assume;
|
||||||
import org.junit.experimental.theories.DataPoints;
|
import org.junit.experimental.theories.DataPoints;
|
||||||
import org.junit.experimental.theories.Theories;
|
import org.junit.experimental.theories.Theories;
|
||||||
import org.junit.experimental.theories.Theory;
|
import org.junit.experimental.theories.Theory;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
@RunWith(Theories.class)
|
@RunWith(Theories.class)
|
||||||
public class NumberOfDigitsIntegrationTest {
|
public class NumberOfDigitsIntegrationTest {
|
||||||
|
|
||||||
private static NumberOfDigits numberOfDigits;
|
private static NumberOfDigits numberOfDigits;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
numberOfDigits = new NumberOfDigits();
|
numberOfDigits = new NumberOfDigits();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DataPoints
|
@DataPoints
|
||||||
public static int[][] lowestIntegers()
|
public static int[][] lowestIntegers()
|
||||||
{
|
{
|
||||||
return new int[][]{
|
return new int[][]{
|
||||||
{1, 1},
|
{1, 1},
|
||||||
{2, 10},
|
{2, 10},
|
||||||
{3, 100},
|
{3, 100},
|
||||||
{4, 1000},
|
{4, 1000},
|
||||||
{5, 10000},
|
{5, 10000},
|
||||||
{6, 100000},
|
{6, 100000},
|
||||||
{7, 1000000},
|
{7, 1000000},
|
||||||
{8, 10000000},
|
{8, 10000000},
|
||||||
{9, 100000000},
|
{9, 100000000},
|
||||||
{10, 1000000000}
|
{10, 1000000000}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@DataPoints
|
@DataPoints
|
||||||
public static int[][] highestIntegers()
|
public static int[][] highestIntegers()
|
||||||
{
|
{
|
||||||
return new int[][]{
|
return new int[][]{
|
||||||
{1, 9},
|
{1, 9},
|
||||||
{2, 99},
|
{2, 99},
|
||||||
{3, 999},
|
{3, 999},
|
||||||
{4, 9999},
|
{4, 9999},
|
||||||
{5, 99999},
|
{5, 99999},
|
||||||
{6, 999999},
|
{6, 999999},
|
||||||
{7, 9999999},
|
{7, 9999999},
|
||||||
{8, 99999999},
|
{8, 99999999},
|
||||||
{9, 999999999},
|
{9, 999999999},
|
||||||
{10, Integer.MAX_VALUE}
|
{10, Integer.MAX_VALUE}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@DataPoints
|
@DataPoints
|
||||||
public static int[][] randomIntegers()
|
public static int[][] randomIntegers()
|
||||||
{
|
{
|
||||||
return new int[][]{
|
return new int[][]{
|
||||||
{1, 1},
|
{1, 1},
|
||||||
{2, 14},
|
{2, 14},
|
||||||
{3, 549},
|
{3, 549},
|
||||||
{4, 1136},
|
{4, 1136},
|
||||||
{5, 25340},
|
{5, 25340},
|
||||||
{6, 134321},
|
{6, 134321},
|
||||||
{7, 1435432},
|
{7, 1435432},
|
||||||
{8, 54234129},
|
{8, 54234129},
|
||||||
{9, 113683912},
|
{9, 113683912},
|
||||||
{10, 1534031982}
|
{10, 1534031982}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenStringBasedSolutionInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenStringBasedSolutionInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], numberOfDigits.stringBasedSolution(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.stringBasedSolution(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenLogarithmicApproachInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenLogarithmicApproachInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], numberOfDigits.logarithmicApproach(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.logarithmicApproach(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenRepeatedMultiplicationInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenRepeatedMultiplicationInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], numberOfDigits.repeatedMultiplication(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.repeatedMultiplication(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenShiftOperatorsInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenShiftOperatorsInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], numberOfDigits.shiftOperators(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.shiftOperators(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenDividingWithPowersOf2Invoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenDividingWithPowersOf2Invoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], numberOfDigits.dividingWithPowersOf2(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.dividingWithPowersOf2(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenDivideAndConquerInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenDivideAndConquerInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], numberOfDigits.divideAndConquer(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.divideAndConquer(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue