* 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
This commit is contained in:
parent
42fb696539
commit
d2f52b059f
@ -12,3 +12,4 @@ This module contains articles about core features in the Java language
|
|||||||
- [Infinity in Java](https://www.baeldung.com/java-infinity)
|
- [Infinity in Java](https://www.baeldung.com/java-infinity)
|
||||||
- [Type Parameter vs Wildcard in Java Generics](https://www.baeldung.com/java-generics-type-parameter-vs-wildcard)
|
- [Type Parameter vs Wildcard in Java Generics](https://www.baeldung.com/java-generics-type-parameter-vs-wildcard)
|
||||||
- [Convert Between int and char in Java](https://www.baeldung.com/java-convert-int-char)
|
- [Convert Between int and char in Java](https://www.baeldung.com/java-convert-int-char)
|
||||||
|
-
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.convertnumberbases;
|
||||||
|
|
||||||
|
public class ConvertNumberBases {
|
||||||
|
|
||||||
|
public static String convertNumberToNewBase(String number, int base, int newBase){
|
||||||
|
return Integer.toString(Integer.parseInt(number, base), newBase);
|
||||||
|
}
|
||||||
|
public static String convertNumberToNewBaseCustom(String num, int base, int newBase) {
|
||||||
|
int decimalNumber = convertFromAnyBaseToDecimal(num, base);
|
||||||
|
return convertFromDecimalToBaseX(decimalNumber, newBase);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String convertFromDecimalToBaseX(int num, int newBase) {
|
||||||
|
|
||||||
|
String result = "";
|
||||||
|
int remainder;
|
||||||
|
while (num > 0) {
|
||||||
|
remainder = num % newBase;
|
||||||
|
if (newBase == 16) {
|
||||||
|
if (remainder == 10)
|
||||||
|
result += 'A';
|
||||||
|
else if (remainder == 11)
|
||||||
|
result += 'B';
|
||||||
|
else if (remainder == 12)
|
||||||
|
result += 'C';
|
||||||
|
else if (remainder == 13)
|
||||||
|
result += 'D';
|
||||||
|
else if (remainder == 14)
|
||||||
|
result += 'E';
|
||||||
|
else if (remainder == 15)
|
||||||
|
result += 'F';
|
||||||
|
else
|
||||||
|
result += remainder;
|
||||||
|
} else
|
||||||
|
result += remainder;
|
||||||
|
|
||||||
|
num /= newBase;
|
||||||
|
}
|
||||||
|
return new StringBuffer(result).reverse().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int convertFromAnyBaseToDecimal(String num, int base) {
|
||||||
|
|
||||||
|
if (base < 2 || (base > 10 && base != 16))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int val = 0;
|
||||||
|
int power = 1;
|
||||||
|
|
||||||
|
for (int i = num.length() - 1; i >= 0; i--) {
|
||||||
|
int digit = charToDecimal(num.charAt(i));
|
||||||
|
|
||||||
|
if (digit < 0 || digit >= base)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
val += digit * power;
|
||||||
|
power = power * base;
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int charToDecimal(char c) {
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
return (int) c - '0';
|
||||||
|
else
|
||||||
|
return (int) c - 'A' + 10;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.deepshallowcopy;
|
||||||
|
|
||||||
|
public class SchoolDeepCopy implements Cloneable{
|
||||||
|
public String name;
|
||||||
|
public SchoolDeepCopy(String name){
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Object clone() throws CloneNotSupportedException{
|
||||||
|
return super.clone();
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.deepshallowcopy;
|
||||||
|
|
||||||
|
public class SchoolShallowCopy {
|
||||||
|
public String name;
|
||||||
|
public SchoolShallowCopy(String name){
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.baeldung.deepshallowcopy;
|
||||||
|
|
||||||
|
public class StudentDeepCopy implements Cloneable{
|
||||||
|
public String firstName;
|
||||||
|
public String lastName;
|
||||||
|
public String level;
|
||||||
|
public SchoolDeepCopy school;
|
||||||
|
|
||||||
|
|
||||||
|
public StudentDeepCopy(String firstName, String lastName, String level, SchoolDeepCopy school){
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.level = level;
|
||||||
|
this.school = school;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLevel() {
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLevel(String level) {
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SchoolDeepCopy getSchool() {
|
||||||
|
return school;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSchool(SchoolDeepCopy school) {
|
||||||
|
this.school = school;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected Object clone() throws CloneNotSupportedException {
|
||||||
|
StudentDeepCopy student = (StudentDeepCopy) super.clone();
|
||||||
|
student.school = (SchoolDeepCopy) school.clone();
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.deepshallowcopy;
|
||||||
|
|
||||||
|
public class StudentShallowCopy implements Cloneable{
|
||||||
|
public String firstName;
|
||||||
|
public String lastName;
|
||||||
|
public String level;
|
||||||
|
public SchoolShallowCopy school;
|
||||||
|
|
||||||
|
|
||||||
|
public StudentShallowCopy(String firstName, String lastName, String level, SchoolShallowCopy school){
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.level = level;
|
||||||
|
this.school = school;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLevel() {
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLevel(String level) {
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SchoolShallowCopy getSchool() {
|
||||||
|
return school;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSchool(SchoolShallowCopy school) {
|
||||||
|
this.school = school;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected Object clone() throws CloneNotSupportedException {
|
||||||
|
return super.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.convertnumberbases;
|
||||||
|
|
||||||
|
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBase;
|
||||||
|
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBaseCustom;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ConvertNumberBasesUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenConvertingBase10NumberToBase5_ThenResultShouldBeDigitsInBase5() {
|
||||||
|
assertEquals(convertNumberToNewBase("89", 10, 5), "324");
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void whenConvertingBase2NumberToBase8_ThenResultShouldBeDigitsInBase8() {
|
||||||
|
assertEquals(convertNumberToNewBaseCustom("11001000", 2, 8), "310");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.deepshallowcopy;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class StudentDeepCopyUnitTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenDeepCopying_thenCopyObjectMakesCopyOfReferencedObjectInSourceObject(){
|
||||||
|
SchoolDeepCopy ug = new SchoolDeepCopy("University of Ghana");
|
||||||
|
StudentDeepCopy studentOne = new StudentDeepCopy("Abena", "Kojo","200L", ug );
|
||||||
|
StudentDeepCopy studentTwo = null;
|
||||||
|
|
||||||
|
try{
|
||||||
|
studentTwo = (StudentDeepCopy) studentOne.clone();
|
||||||
|
} catch (CloneNotSupportedException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNotSame(studentOne.getSchool(),studentTwo.getSchool());
|
||||||
|
studentTwo.getSchool().setName("University of Nigeria");
|
||||||
|
assertNotEquals(studentOne.getSchool().getName(), studentTwo.getSchool().getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.deepshallowcopy;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class StudentShallowCopyUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenShallowCopying_thenCopyObjectAndSourceObjectShareSameReferencedObject(){
|
||||||
|
SchoolShallowCopy ug = new SchoolShallowCopy("University of Ghana");
|
||||||
|
StudentShallowCopy studentOne = new StudentShallowCopy("Abena", "Kojo","200L", ug );
|
||||||
|
StudentShallowCopy studentTwo = null;
|
||||||
|
|
||||||
|
try{
|
||||||
|
studentTwo = (StudentShallowCopy) studentOne.clone();
|
||||||
|
} catch (CloneNotSupportedException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSame(studentOne.getSchool(),studentTwo.getSchool());
|
||||||
|
studentTwo.getSchool().setName("University of Nigeria");
|
||||||
|
assertEquals(studentOne.getSchool().getName(), studentTwo.getSchool().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user