Encapsulation and Get and Set methods

Asked

Viewed 1,048 times

0

I am continuing my studies in object-oriented Java programming.

I am currently studying encapsulation and methods get and set and I came across the following exercise:

Encapsulate the value attribute of the Boleto class.

This attribute must not be set directly. The class user informs the value and the student. If the student is approved a 10% discount is granted on the amount of the ticket and the get method must display the amount with the discount granted.

And I created this code:

Pupil Class:

package Academico;

public class Aluno {

    public String nome;
    private String matricula;
    private String curso;
    protected Disciplina disciplina[];

}

Classe Disciplina:

package Academico;

public class Disciplina {

    String nome;
    double nota1, nota2, nota3, nota4;
   
}

Boleto class:

package Financeiro;

import Academico.*;

public class Boleto {

    private Aluno aluno;
    private String referencia;
    private double valor;
    
    public Boleto(Aluno aluno, String referencia, double valor){
        this.aluno = aluno;
        this.referencia = referencia;
        this.valor = valor;
    }
    
    public double desconto() {
        disciplina = new Disciplina[3];
        if(disciplina == true){
            this.valor = this.valor-(this.valor*0.1);
            return this.valor;
        }
    }
    
    public double getValorDesconto() {
        return this.valor;
    }

}

There is some error in code construction, encapsulation and methods Get and Set?

  • There’s some specific question. I don’t know what to do with this question, because the answer is "it’s all right":) Of course the requirements indicate problem. It seems that the code does not do what is being determined. There are things that do not make any sense, but not in what is being asked.

  • I have created another package to study encapsulation. This other package is called academic and has a Student class and a Discipline class. Ai wanted to import the Student and the Discipline from the other package. Ai created some methods before to check if it was ok. So the code is totally correct in relation to what the exercise asked for?

  • Without all the information, it is difficult to say if everything is right, it seems to me that the use of packages is wrong, but the methods get nay.

  • It is that the exercise said that the attributes could not be set directly, so I removed the set methods and only let the get.

  • I edited the code and put the academic package and how I created the classes within it to import into the financial package. So you can take a closer look at the code.

  • Separate your classes into the question, the code is piled up, it’s hard to understand that way.

  • It’s just that I haven’t gotten the hang of editing here at the forum yet, like separating everything straight.

  • I added the Student and Discipline class and edited the boleto class to make it easier to understand the code, whether it is correct according to the exercise or not and also take advantage to check if it is correct the construction of the encapsulations.

  • @Gon The answer solved your problem? Do you think you can accept it? If you don’t know how, check out the [tour] how to do it. This would help a lot to indicate that the solution was useful for you and help everyone understand that. You can also vote for anything on the entire site.

Show 4 more comments

1 answer

1

The question was edited, I made based on the original code posted

The exercise already seems to be well defined. So it is even difficult to say what is right. Without more accurate information you can’t say or help do right, you can’t say what’s right.

Not to be unanswered I will give a slight improvement and ask questions:

package Financeiro;

import Academico.Disciplina;

public class Boleto {
    private Aluno aluno;
    private String referencia; //o exercício não fala disto
    private double valor; //seria melhor usar um BigDecimal, mas para exercício ok

    public Boleto(Aluno aluno, String referencia, double valor){
        this.aluno = aluno;
        this.referencia = referencia;
        this.valor = valor;
    }

    public Aluno getAluno() { return aluno; }

    public String getReferencia() { return referencia; }

    public double getValor() { return valor; }

    public double desconto() { //isto faz e retorna o desconto, o nome não é bom
        disciplina = new Disciplina[3]; //isto não parece fazer o menor sentido,nem compila
        if (disciplina) { //onde está a verificação de aprovação?
            valor -=  valor * 0.1;
            return valor;
        }
    }

    public double getValorDesconto() { //qual o intuito disto? O requisito não pede
        return valor; //isto parece estar errado se o método faz sentido existir
    }
}

I put in the Github for future reference.

Possibly lacking methods set, but I don’t know if I should do this. The fact that I say the attribute should not be setate directly is that it should not be public and, in theory, can be setate indirectly through a method setXXX().

What’s to stop calling the discount several times and even reset the value? I know for an exercise this shouldn’t be very important, but it’s something to think about.

I think the discount calculation should be done in the builder and there is no method desconto, then I think you’ll solve it better.

A code possibly a little better:

public class Boleto {
    private Aluno aluno;
    private String referencia;
    private double valor;

    public Boleto(Aluno aluno, String referencia, double valor) {
        this.aluno = aluno;
        this.referencia = referencia;
        this.valor = valor * (aluno.ÉAprovado() ? 0.9 : 1;
    }
    public double getValor() { return valor; }
}

Obviously the method ÉAprovado() is part of the class Aluno, after all this is a student property and should be defined there. Boleto is only a consumer of student information. Each class has its own responsibility. It will have an algorithm looking through the student’s grades in the disciplines and will return a boolean indicated the student’s condition. The approval could even be defined by a policy class, but that is not necessary for this algorithm.

  • The new Discipline [3], I had thought of vectorizing, to call the number of discipline he is enrolling, because if you think about college, some students are enrolled in 3, 4, 5 subjects and so on. In the discount method it would be better to put protected then, so you can not call until reset the value? These are my doubts in encapsulation, when to use each one.

  • 1

    There’s nothing wrong with protected, do not use things that you do not know how to use and cannot justify its use. This does not solve any problem. The only decent solution is the method does not exist. But as I said, it is difficult to do something right when the definition is not good. The code that takes care of the notes is completely wrong, nor compiles.

  • I think you did not need to pass into ignorance. I just wanted to ask a question, I know the theory of public, private and protected. The way you said, I had understood that I should use a protected or private. What I could not use would put the discount on the builder, because I would not know how to do and nor justify.

  • Where are you ignorant? I’m telling you what I think you should do. Don’t accept my recommendation, do it the way you want.

  • I said yes! I’m even redoing the code and you’re helping me a lot, I just found it kind of offensive this part: "There’s nothing protected, don’t use things you don’t know how to use and can’t justify your use." I studied public, protected and private, I just didn’t quite understand the time to use each of them. What you said makes total sense, calling to zero. I also apologize on my part if I was offensive at any time, it is not my intention, I just want to learn correctly.

  • You don’t want to help anymore?

Show 1 more comment

Browser other questions tagged

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