How to call a method of one class in another class

Asked

Viewed 2,385 times

2

I had posted earlier but could not edit, I will try to be clearer this time. I am doing (trying)a code for Fonica account. I have class time, connection and contagion. in the account link class I call a time type (class) field to convert time into minutes and display it by calling the time class a constructor.I also have in the link class another field that returns the minute value. What I would like to do is call both the method that returns me the value, when what returns the minutes, into the condadaligacao class. The point is, I’m having trouble calling these roles, and since I’m a beginner, I don’t even know what I’m doing wrong.

Follow my classes below:

public class Ligacao
{
    private String nrOrigem, localOrig, nrDest, localDest;
    private float valor;
    private Tempo inicio, termino;


    public Ligacao(String nrOrigem, String localOrig, String nrDest, String localDest, float valor, Tempo inicio, Tempo termino){
    this.nrOrigem=nrOrigem;
    this.localOrig=localOrig;
    this.nrDest=nrDest;
    this.localDest=localDest;
    this.valor=valor;
    this.inicio=inicio;
    this.termino=termino;
   }

   public String getnrOrigem(){
    return nrOrigem;
    }
   public void setnrOrigem(String numOrigem){
       this.nrOrigem=numOrigem;
    }

   public String getlocalOrig(){
   return localOrig;
    }
   public void setlocalOrig(String localOrigem){
   this.localOrig=localOrigem;
   }
   public String getnrDest(){
    return nrDest;
    }
   public void setnrDest(String numDest){
    this.nrDest=numDest;
    }
   public String getlocalDest(){
    return localDest;
    }
    public void setlocalDest(String localDest){
    this.localDest=localDest;
    }

    public float getvalor(){
    return valor;
    }
    public void setvalor(float valor){
        this.valor=valor;
    }
    public Tempo getinicio(){
        return inicio;
    }
    public Tempo gettermino(){
        return termino;
    }
    public int calcularDuracao(){
        int inicioSeg,terminoSeg,durSeg,durMin,resto;

        inicioSeg=inicio.tempoEmSegundos();
        terminoSeg=termino.tempoEmSegundos();

        durSeg=terminoSeg-inicioSeg;
        durMin=durSeg/60;
        resto=durSeg%2;
        if(resto>0){
            durMin++;
        }
        return durMin;
    }

    public boolean verificaNumero(String numero){
        return ((nrOrigem.equals(numero))||(nrDest.equals(numero)));

    }



}



public class Tempo
{
   private int hora, minuto, segundo;
   private float tempoTotal;


  public Tempo (int hora, int minuto, int segundo){
      this.hora=hora;
      this.minuto=minuto;
      this.segundo=segundo;
    }
    public int tempoEmSegundos(){
        return hora*3600 + minuto *60 + segundo;
    }
   public int gethora(){
    return this.hora;
    }
   public void sethora(int hora){
    this.hora = hora;
    }
    public int getminuto(){
    return this.minuto;
    }
    public void setminuto(int minuto){
    this.minuto=minuto;
    }
    public int getsegundo(){
    return this.segundo;
    }
    public void setsegundo(int segundo){
        this.segundo=segundo;
    }

}



public class ContaTel
{
    private int mes, ano, contrato;
    private Ligacao[] listaligacoes;
    private Tempo inicio,termino;

    public ContaTel(int mes,int ano, int contrato, int tamanho){
    this.mes=mes;
    this.ano=ano;
    this.contrato=contrato;
    this.listaligacoes = new Ligacao[tamanho];

    }
    public void setmes(int mes){
    this.mes= mes;
    }
    public int getmes(){
    return mes;
    }
    public void setano(int ano){
    this.ano = ano;

    } 
    public int getano(){
    return ano;
    }
    public void setcontrato(int contrato){
    this.contrato = contrato;
    }
    public int getcontrato(){
    return contrato;
    }
    public Ligacao[] getlistaligacoes(){
    return this.listaligacoes;
    }
    public void addligacao(Ligacao lig){
        int i=0;
         for(i=0;i<listaligacoes.length;i++){
            if (listaligacoes[i]==null){
                listaligacoes[i]=lig;
                break;
            }
            else {
                break;
            }
        }
    }

I don’t want the answer, I want to learn, I’ve been at it all day.

2 answers

1


You have the list (vector) of everything inside the ContaTel. To access these functions you first need to access each element of the Ligacao[].

public class ContaTel
{
    private int mes, ano, contrato;
    private Ligacao[] listaligacoes;
    private Tempo inicio,termino; // Desnecessário esses atributos

    ...

    public void doItAnywway(){

        for(int i = 0; i < listaligacoes.lenght; i++){

            Ligacao ligacao = listaligacoes[i]; // peguei um elemento
            Tempo t1 = ligacao.getinicio(); // peguei o Tempo deste elemento
            Tempo t2 = ligacao.gettermino(); // peguei o outro tempo deste elemento

            /**
            * Agora eu posso acessar todas as funções que eu precisar!!!!
            * VIVAAA!!!!
            */

            t1.tempoEmSegundos(); // usei essa!
            t1.gethora(); // peguei a hora!

            ligacao.calcularDuracao(); // essa parece boa!
            ligacao.getlocalDest(); // preciso ver para onde esse povo ligou!!!

        }

    }

    ...

I know you’re still learning but you need to get better at this modeling of yours.

I hope I’ve helped.

  • 1

    With modeling you say code identation? if yes, where can I see more about standardization? And thank you so much for the answer!

  • @Marcelabraga modeling, is what you did. You created a class ContaTel that there could be more than one connection, and that the connection depended on a Time to exist. You created the "rules". However, there are some holes... Like... the Contactel class has no reason to have the Time attribute. This Time is part of the link and not the account. Got it?

  • @Marcelabraga research about UML and class diagram.

  • Okay, anyway, I’m trying and it’s returning me an exception here: java.lang.Nullpointerexception At contactel.valorConta(Contactel.java:52)

  • @Marcelabraga ai is already another problem! But you can already access the functions. Search about this error in google.

0

I couldn’t understand very well what you want to do, but as you said you want to learn I’ll try to explain in a generic way. It has two means of you calling methods of a class:

1 - Instantiated it

Classe classe = new Classe();
classe.metodo(parametrosSeHouver);

2 - Making the class method static

2.1 - Declaring a Statistical Method

public static String retornaString(String str){
return str;
}

2.2 - Calling a static method:

Classe.retornaString("Uma string qualquer");

--- Edit

public int calculaValorLigacao() {
    return calcularDuracao() * 1;       
}
  • So I want to calculate the value of a connection, from the minutes. for example, 200 minutesx1 real=200 real. It must not have been very clear, but I created a variable with the type of the time class, which receives hours and seconds, converts into minutes etc, and a method that stores the value of the connection. only.

  • I think I get it, see my updated answer, you already had in the link class a method that calculated the duration, so I just created a new one and multiplied it by the minute value

Browser other questions tagged

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