Print Friendly, PDF & Email

An abstract data type or ADT is an entity that stores the information we require to store it as a information unit about a subject as if it was a type incorporated into the system , here we demonstrate how to do this in Kotlin and Java.

Suppose we want to list the information of some points that we are going to paint on the screen in some way, we know that our points have a coordinate in the axis of the abscissa and a coordinate in the axis of the ordinates, so we will use techniques of the object-oriented programming to develop this kind of abstract data type “Point” so that we can create information for several points in a more systematic way.

& nbsp;

It is also important to note that in a system different abstract data types </ strong> can coexist.

& nbsp;

Implementation

To avoid the filler code or boilerplate we will use the lombok </ strong> library in Java, you can learn more about it in the official website or in the following links on our site:
(once we finish migrating the content of the in gringo will appear here).

In the (link) configuration section you can find how to configure lombok for this project.

& nbsp;

First we add the necessary fields

Java

package jaba;

public class Point {
    private double y;
    private double x;
}

Kotlin:

package katlin

class Point(var y: Double, var x: Double) {

}

we later added the access methods to apply the encapsulation principle as suggested by OOP and we will create a method for displaying a textual representation of the object.

Java:

package jaba;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class Point {
    private double y;
    private double x;

    public Point() {
        x = Math.random();
        y = Math.random();
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ')';
    }
}

Kotlin:

package katlin

class Point {
    var y: Double
    var x: Double

    constructor(y: Double, x: Double) {
        this.y = y
        this.x = x
    }

    constructor() {
        x = Math.random()
        y = Math.random()
    }

    override fun toString(): String {
        return "($x, $y)"
    }
}

Finally we will add some convenient methods to this ADT to show how it should be used from inside and outside, being the most important distance that allows us to calculate the distance to another point.

Java:

package jaba;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
 * Created by Administrador on 21/05/2017.
 */
@Getter
@Setter
@AllArgsConstructor
public class Point {
    private double y;
    private double x;

    public Point() {
        x = Math.random();
        y = Math.random();
    }

    double r() {
        return Math.sqrt(x * x + y * y);
    }

    double theta() {
        return Math.atan2(x, y);
    }

    double distance(Point point) {
        double dx = x - point.getX();
        double dy = y - point.getY();
        return Math.sqrt(dx * dx + dy * dy);
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ')';
    }
}

 

Kotlin:

package katlin

class Point {
    var y: Double
    var x: Double

    constructor() {
        x = Math.random()
        y = Math.random()
    }

    constructor(y: Double, x: Double) {
        this.y = y
        this.x = x
    }

    internal fun r(): Double {
        return Math.sqrt(x * x + y * y)
    }

    internal fun theta(): Double {
        return Math.atan2(x, y)
    }

    internal fun distance(point: Point): Double {
        val dx = x - point.x
        val dy = y - point.y
        return Math.sqrt(dx * dx + dy * dy)
    }

    override fun toString(): String {
        return "($x, $y)"
    }
}

Use

In order to take our ADT to another level and to make a more real example, we will add the label property that will allow us to have another subtype of points, which in addition to having their coordinates in both axes, will have an extra property with a text to show, and its textual representation will be the same as the one of a point, but adding the property of the label (therefore it will be more convenient to reuse the functionality of the superclass).

Java:

package jaba;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LabeledPoint extends Point {
    String label;

    @Override
    public String toString() {
        return label + super.toString();
    }
}

Kotlin:

package katlin

class LabeledPoint(override var y: Double, override var x: Double, var label: String) : Point() {

    override fun toString(): String {
        return "$label${super.toString()}"
    }
}

para el caso de kotlin, se debe de modificar la clase padre para evitar errores. Primeramente la deberemos de hacer abierta, para que pueda ser heredada

package katlin

open class Point {
    var y: Double
    var x: Double

    constructor() {
        x = Math.random()
        y = Math.random()
    }

    constructor(y: Double, x: Double) {
        this.y = y
        this.x = x
    }

    internal fun r(): Double {
        return Math.sqrt(x * x + y * y)
    }

    internal fun theta(): Double {
        return Math.atan2(x, y)
    }

    internal fun distance(point: Point): Double {
        val dx = x - point.x
        val dy = y - point.y
        return Math.sqrt(dx * dx + dy * dy)
    }

    override fun toString(): String {
        return "($x, $y)"
    }
}

later, we must modify their fields so that they are not final

package katlin

open class Point {
    open var y: Double
    open var x: Double

    constructor() {
        x = Math.random()
        y = Math.random()
    }

    constructor(y: Double, x: Double) {
        this.y = y
        this.x = x
    }

    internal fun r(): Double {
        return Math.sqrt(x * x + y * y)
    }

    internal fun theta(): Double {
        return Math.atan2(x, y)
    }

    internal fun distance(point: Point): Double {
        val dx = x - point.x
        val dy = y - point.y
        return Math.sqrt(dx * dx + dy * dy)
    }

    override fun toString(): String {
        return "($x, $y)"
    }
}

 

I hope this is enough for you to have gained experience by creating your own abstract data types.

Do not miss our next entry for this course: Simply linked lists (link coming soon)

 

 

Radio

Do NOT follow this link or you will be banned from the site!