What is the data type?
A data type is an attribute in a programming language which define the domain of values and operations on that domain.- Define a certain domain of value.
- Define operations allowed on those values.
Example
int type
- Take only integer values
- Operations supported: addition, subtraction, multiplication, bitwise operation etc.
float type
- Take only floating values
- Operations supported: addition, subtraction, multiplication, etc. (it does not support operations like bitwise and modulus).
Now, what is the user-defined data type?
A user data type is NOT an attribute in a programming language but defines by the user which define the domain of values.
In below example, we define the user-defined data type using class. Here Person class is the type which is consist of bunch of String, LocalDate and Address fields. Person class have a user define a data type of Address.
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
private Address address;
.....
}
public class Address {
private String firstLine;
private String lastLine,
private State state;
....
}
In below example, we define the user-defined data type using class. Here Person class is the type which is consist of bunch of String, LocalDate and Address fields. Person class have a user define a data type of Address.
Example
public class Person {private String firstName;
private String lastName;
private LocalDate dateOfBirth;
private Address address;
.....
}
public class Address {
private String firstLine;
private String lastLine,
private State state;
....
}
Abstract Data Types (ADT)
Abstract Data Types (ADT) are like user define types which define values and operations on that user define type without specifying how the operation will happen.Example
In this example, we will define an ADT where will create a data type named MyString class and define operations like addition, subtraction, multiplication and division.
User Define Data Type
- MyString is a wrapper object of string.
User Define Operations
- The addition will concatenation two MyString. Example: "Hello" + " world" will result in "Hello world".
- The subtraction will substring the difference of length. If the difference of length is zero or negative then it will have an empty string. Example:
- "Hello" - "world" will result in an empty string
- "Hello" - "hi" will result in "Hel"
- "hi" - "Hello" will also result in an empty string.
- The division will remove common character(s) from MyString. Example: "Hello" / "world" will result in "He".
- The multiplication will concatenate characters alternatively. Example: "Hello" * "world" will result in "Hweolrllod".
Actual Implementation
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
package com.andy.adt; | |
import java.util.Objects; | |
public class MyString { | |
private final String myString; | |
public MyString(String myString) { | |
this.myString = myString; | |
} | |
public MyString add(MyString str2) { | |
return new MyString(myString + str2.toString()); | |
} | |
public MyString subtract(MyString str2) { | |
int differenceInLength = myString.length() - str2.toString().length(); | |
if (differenceInLength > 0) { | |
return new MyString(myString.substring(0, differenceInLength)); | |
} | |
return new MyString(""); | |
} | |
public MyString multiple(MyString str2) { | |
StringBuilder stringBuilder = new StringBuilder(""); | |
int maxLength = Math.max(myString.length(), str2.toString().length()); | |
for (int index = 0; index < maxLength; index++) { | |
if(index < myString.length()) { | |
stringBuilder.append(myString.charAt(index)); | |
} | |
if(index < str2.toString().length()) { | |
stringBuilder.append(str2.toString().charAt(index)); | |
} | |
} | |
return new MyString(stringBuilder.toString()); | |
} | |
public MyString divide(MyString str2) { | |
char[] denominator = str2.toString().toCharArray(); | |
String str = myString.toString(); | |
for (char ch: | |
denominator) { | |
str = str.replaceAll(String.valueOf(ch), ""); | |
} | |
return new MyString(str); | |
} | |
@Override | |
public String toString() { | |
return myString; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof MyString)) return false; | |
MyString myString1 = (MyString) o; | |
return Objects.equals(myString, myString1.myString); | |
} | |
@Override | |
public int hashCode() { | |
return myString != null ? myString.hashCode() : 0; | |
} | |
} |
Sample Unit Test of the Implementation
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
package com.andy.adt; | |
import org.junit.jupiter.api.Test; | |
import static org.junit.jupiter.api.Assertions.*; | |
class MyStringTest { | |
private MyString myString; | |
@Test | |
public void testAdd(){ | |
myString = new MyString("Hello"); | |
MyString result = myString.add(new MyString(" World!")); | |
assertEquals("Hello World!", result.toString()); | |
} | |
@Test | |
public void testSubtract_WhenSecondParameterIsLong() { | |
myString = new MyString("Hello"); | |
MyString result = myString.subtract(new MyString(" World!")); | |
assertEquals("", result.toString()); | |
} | |
@Test | |
public void testSubtract_WhenFirstParameterIsLong() { | |
myString = new MyString("Robot"); | |
MyString result = myString.subtract(new MyString("Ok")); | |
assertEquals("Rob", result.toString()); | |
} | |
@Test | |
public void testDivide() { | |
myString = new MyString("Hello"); | |
MyString result = myString.divide(new MyString("world")); | |
assertEquals("He", result.toString()); | |
} | |
@Test | |
public void testMultiple() { | |
myString = new MyString("Hello"); | |
MyString result = myString.multiple(new MyString("world")); | |
assertEquals("Hweolrllod", result.toString()); | |
} | |
} |