How to implement Abstract Data Type?

Asked

Viewed 227 times

1

Good evening, I need to do a point TAD implementation, I’m managing to do in Python, but the problem is that it should be implemented in Java and I have no idea how to start.

import math
class Ponto(object):

  def __init__(self, x=0, y=0):
    self.x=x
    self.y=y

  def igual(self, ponto):
    return self.x == ponto.x and self.y == ponto.y

  def texto(self):
    return '('+str(self.x)+', '+str(self.y)+')'

  def distancia(self, ponto):
    d1=self.x-ponto.x
    d2=self.y-ponto.y
    return math.sqrt(d1*d1+d2*d2)

  def translada(self, dx, dy):
    self.x=self.x+dx
    self.y=self.y+dy
  • What do you know about Java? If it’s little, start by studying the language documentation.

  • You can start by that question

1 answer

0

import java.lang.Math;
class Ponto(){

private int x;
private int y;


public Ponto(int x, int y){
    this.x = x;
    this.y = y;
}

public boolean igual(Ponto ponto){
    return this.x == ponto.x && this.y == ponto.y;
}

public String texto(){
    return (this.x + " " this.y);
}

public int distancia(Ponto ponto){
    int d1 = this.x - ponto.x;
    int d2 = this.y - ponto.y;
    return Math.sqrt((d1*d1)+(d2*d2));
}

public void translada(int dx, int dy){
    this.x = this.x+dx;
    thix.y = this.y+dy;
}

}

class chamaPonto{
       public static void main(String[] args) {
            Ponto ponto = new Ponto(5,10)
            Ponto ponto2 = new Ponto(10,5)
            ponto.igual(ponto2) //vai retornar false
            ponto.texto() //vai retornar "5 10"
            ponto.distancia(ponto2) //vai retornar a distância entre os pontos
            ponto.translada(5,10) //vai somar 5 ao x e 10 ao y
    }
}
  • Cool, but giving error : Could not find or load main class Main Exit status 1.

  • I edited the comment. Now you have an example of how to call this main function using another class with the main method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.