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.
– Maniero
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?
– Gon
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.– Maniero
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.
– Gon
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.
– Gon
Separate your classes into the question, the code is piled up, it’s hard to understand that way.
– user28595
It’s just that I haven’t gotten the hang of editing here at the forum yet, like separating everything straight.
– Gon
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
@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.
– Maniero