Wanting an explanation on the part of code I have commented out, not understandi
ID: 3696975 • Letter: W
Question
Wanting an explanation on the part of code I have commented out, not understanding the use of super or the point of using this.real and this.imaginary nor the get/set since there are no values until the methods are called; I am no java expert but this confuses me as I feel it would be okay to just use imaginary and real rather than having to do all of that. -------------------------------------------------------------------------------------
public class Complex
{
private double real;
private double imaginary;
public Complex(double real, double imaginary) {
/*super();
this.real = real;
this.imaginary = imaginary;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}*/
public void displayInCartesianFormat() {
char operand;
operand = this.imaginary > 0 ? '+' : '-';
System.out.println(this.real + " " + operand + " j" + this.imaginary);
}
public void displayInPolarFormat() {
double mag = Math.sqrt(this.real*this.real + this.imaginary*this.imaginary);
double slope = Math.toDegrees(Math.atan2(this.imaginary, this.real));
System.out.println(mag + ", " + slope);
}
public void plus(Complex obj) {
this.real+= obj.getReal();
this.imaginary+= obj.getImaginary();
}
public static void main(String[] args) {
Complex object1 = new Complex(3, 4);
Complex object2 = new Complex(4, -5);
object2.plus(object1);
object1.displayInCartesianFormat();
object2.displayInPolarFormat();
}
}
Explanation / Answer
EXPLAINATION:
Why super () is used:
A default constructor is provided by compiler automatically and it also adds super () for the first statement. If you are creating your own constructor and you don't have either this () or super () as the first statement, compiler will provide super () as the first statement of the constructor.
Eventhough you don't use super(), the compiler will automatically add it during the execution of the program.
Why the below statements are used?
this.real = real;
this.imaginary = imaginary;
The above statements call the accessors and mutators of the respective variable and assign their value.
The methods getReal(), setReal(), getImaginary, setImaginary() are called Accessors and Mutators.
One of the ways we can enforce data encapsulation is through the use of accessors and mutators. The role of accessors and mutators are to return and set the values of an object's state.
An accessor method is used to return the value of a private field. It follows a naming scheme prefixing the word "get" to the start of the method name.
A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word "set" to the start of the method name.
These methods do not have a return type and accept a parameter that is the same data type as their corresponding private field, we can also add our own methodology of accepting data such as validation etc, and whatever is the inner implementation the outside world is not aware of it.
The parameter is then used to set the value of that private field.
One the key feature of object oriented programming is Data encapsulation, which means we have to hide the data of the objects as much as possible.
It’s not good practice to expose as public fields or directly access them, because:
- If you use get and set method, you are able to control the access of the property.
- You can put any kind of validation in the set method. If there is a need you can throw an exception.
- The situation may need to set other dependent properties, too, when accessing/setting the current one.
- You can convert the value which you return from the getter to more information less data (for example you can have a value for an imaginary and you want to return it with 2 digits after the decimal point, but in your calculations it should be as much digits as possible for more correct calculations).
- It is easier to bind against properties.
In our everyday life almost every time the specification is changed and almost every time there is a situation which we didn’t take in mind at the time of writing the code. In general in my opinion a good developer is the one that can write as much extensible code as possible, as much reusable as possible and as easier to change as possible no matter which platform is used. The practice of using getters and setters gives you the flexibility to be the winner in the battle between you and the changing specification. It gives you a very simple rule of a thumb which makes your code keeping tight encapsulation.
As you said you can just use real and imaginary to achieve your requirement but that is not a good programming practice as the key feature Data Encapsulation will not be achieved by doing so.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.