I'm new to learning Java and was trying to understand OOP, but I can't seem to find anyone who has the same exact question. My question is, is it okay to use methods inside a constructor like the example here:
package ezrab.nl;
public class Calculations {
private int number;
private int multiplier;
private String operator = "";
public Calculations(int number, String operator, int multiplier) {
this.number = number;
this.operator = operator;
this.multiplier = multiplier;
switch (getOperator()) {
case "+":
System.out.println(getNumber() + getMultiplier());
break;
case "-":
System.out.println(getNumber() - getMultiplier());
break;
case "*":
System.out.println(getNumber() * getMultiplier());
break;
case "/":
System.out.println(getNumber() / getMultiplier());
break;
case "%":
System.out.println(getNumber() % getMultiplier());
break;
default:
System.out.println("Something went wrong.");
}
}
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
public int getMultiplier() {
return this.multiplier;
}
public void setMultiplier(int multiplier) {
this.multiplier = multiplier;
}
public String getOperator() {
return this.operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
}
So I'd like to know, is it allowed to use the methods I've created inside the constructor.
EDIT: I'd like to point out that the program is working. I just want to know if I followed the rules to OOP correctly.
finalSystem.out.printlnis final/static.print. Because constructor must only create and initialize the object. And not to print or do something. For each action create a method.