vinists
vinists

Reputation: 19

How to implement this Class Diagram in JAVA?

I'm following a Class Diagram of a project. and I got confused with this part: enter image description here

How am I supposed to create a variable containing a Class? (I don't know if it's clear, but none of these classes are the main one)

Please, bare in mind that I'm a student, and I've never had to follow any Class Diagram before.

Upvotes: 0

Views: 190

Answers (3)

Nagaraj Kandoor
Nagaraj Kandoor

Reputation: 305

Class diagram shows a composition between Historico and Usuario. It's a has relationship between (Historico has a Usuario).

You can define zero parameterized constructor like this:

 class Usuario {
    public Usuario() {
   }
  }

more ever compiler will add zero parameter constructor if you don't define while byte code generation if no constructor defined.

You can define compostion like this :

public class Historico {
  private Usuario responsavel;
}

Complete code:

public class Usuario {
    String login;
    String senha;

    /**
     * @return the login
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * @return the senha
     */
    public String getSenha() {
        return senha;
    }

    /**
     * @param senha the senha to set
     */
    public void setSenha(String senha) {
        this.senha = senha;
    }

}

class Historico:

 public class Historico {
    private Usuario responsavel;
    private String dataatualizacao;
    private String descricao;

    /**
     * @return the dataatualizacao
     */
    public String getDataatualizacao() {
        return dataatualizacao;
    }

    /**
     * @param dataatualizacao the dataatualizacao to set
     */
    public void setDataatualizacao(String dataatualizacao) {
        this.dataatualizacao = dataatualizacao;
    }

    /**
     * @return the descricao
     */
    public String getDescricao() {
        return descricao;
    }

    /**
     * @param descricao the descricao to set
     */
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public void setResponsavel(Usuario responsavel) {
        this.responsavel = responsavel;
    }
}

Upvotes: 3

geffchang
geffchang

Reputation: 3340

It would be something like this:

class Usuario {

  public Usuario() {
    // TODO Auto-generated constructor stub
  }
}

class Historico {
  private Usuario responsavel;

  public Historico() {
    // TODO Auto-generated constructor stub
  }

  public Usuario getResponsavel() {
    return responsavel;
  }

  public void setResponsavel(Usuario responsavel) {
    this.responsavel = responsavel;
  }
}

public class Demo {
  public static void main(String[] args) {
    Usuario u = new Usuario();
    Historico h = new Historico();
    h.setResponsavel(u);
  }
}

Upvotes: 1

afghanimah
afghanimah

Reputation: 775

You can create a variable for a class just like you create any other variable:

// what you're probably used to:
int myInt;

Say you have a class Usuario as in the diagram, you can make a variable for it like this:

Usuario usuario;

Not sure exactly what you're asking, but you can even have instances of a class, point to other instances of the same class, for example with a node:

public class Node {
    public Node prev;
    public Node next;

    public Node(Node prev, Node next) {
        this.prev = prev;
        this.next = next;
    }
}

Upvotes: 0

Related Questions