-
Notifications
You must be signed in to change notification settings - Fork 54.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Creating a deep vs shallow copy of an object in Java * Creating a deep vs shallow copy of an object in Java * Baeldung article converting number bases * Baeldung article converting number bases * edits made to Converting a Number from One Base to Another in Java * added braces to oneliners * added precondition to check input * String[] vs String... * helper vs Utility classes * helper vs utility classes code * helper vs utility classes refactor * utility vs helper class * Helper vs utility * helper vs utility classes * refactor package name * Revert "refactor package name" This reverts commit d9464aa. * renamed package
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...odules/core-java-8-2/src/main/java/com/baeldung/helpervsutilityclasses/MyHelperClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.baeldung.helpervsutilityclasses; | ||
|
||
class MyHelperClass { | ||
public double discount; | ||
public MyHelperClass(double discount) { | ||
if (discount > 0 && discount < 1) { | ||
this.discount = discount; | ||
} | ||
} | ||
public double discountedPrice(double price) { | ||
return price - (price * discount); | ||
} | ||
|
||
public static int getMaxNumber(int[] numbers) { | ||
if (numbers.length == 0) { | ||
throw new IllegalArgumentException("Ensure array is not empty"); | ||
} | ||
int max = numbers[0]; | ||
for (int i = 1; i < numbers.length; i++) { | ||
if (numbers[i] > max) { | ||
max = numbers[i]; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
public static int getMinNumber(int[] numbers) { | ||
if (numbers.length == 0) { | ||
throw new IllegalArgumentException("Ensure array is not empty"); | ||
} | ||
int min = numbers[0]; | ||
for (int i = 1; i < numbers.length; i++) { | ||
if (numbers[i] < min) { | ||
min = numbers[i]; | ||
} | ||
} | ||
return min; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...dules/core-java-8-2/src/main/java/com/baeldung/helpervsutilityclasses/MyUtilityClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.helpervsutilityclasses; | ||
|
||
public final class MyUtilityClass { | ||
|
||
private MyUtilityClass(){} | ||
|
||
public static String returnUpperCase(String stringInput) { | ||
return stringInput.toUpperCase(); | ||
} | ||
|
||
public static String returnLowerCase(String stringInput) { | ||
return stringInput.toLowerCase(); | ||
} | ||
|
||
public static String[] splitStringInput(String stringInput, String delimiter) { | ||
return stringInput.split(delimiter); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...ore-java-8-2/src/test/java/com/baeldung/helpervsutilityclasses/MyHelperClassUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baeldung.helpervsutilityclasses; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class MyHelperClassUnitTest { | ||
|
||
@Test | ||
void whenCreatingHelperObject_thenHelperObjectShouldBeCreated() { | ||
MyHelperClass myHelperClassObject = new MyHelperClass(0.10); | ||
int[] numberArray = {15, 23, 66, 3, 51, 79}; | ||
|
||
assertNotNull(myHelperClassObject); | ||
|
||
assertEquals(90, myHelperClassObject.discountedPrice(100.00)); | ||
assertEquals( 79, MyHelperClass.getMaxNumber(numberArray)); | ||
assertEquals(3, MyHelperClass.getMinNumber(numberArray)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...re-java-8-2/src/test/java/com/baeldung/helpervsutilityclasses/MyUtilityClassUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.helpervsutilityclasses; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class MyUtilityClassUnitTest { | ||
|
||
@Test | ||
void whenUsingUtilityMethods_thenAccessMethodsViaClassName(){ | ||
assertEquals( "INIUBONG", MyUtilityClass.returnUpperCase("iniubong")); | ||
assertEquals("accra", MyUtilityClass.returnLowerCase("AcCrA")); | ||
} | ||
|
||
} |