1
When I ask for the values of x
and y
, i want to make the operations of the methods receive the values entered by the user. What to do?
public class Calculadora {
Scanner sc = new Scanner(System.in);
protected double x;
protected double y;
protected double soma;
protected double subt;
protected double mult;
protected double divs;
void setValores(){
System.out.println("Informe dois valores: ");
this.x = sc.nextDouble();
this.y = sc.nextDouble();
}
void setSoma(double x, double y){
this.soma = x + y;
}
double getSoma(){
return soma;
}
void setSubt(double x, double y){
this.subt = x-y;
}
double getSubt(){
return subt;
}
void setMult(double x, double y){
this.mult = x*y;
}
double getMult(){
return mult;
}
void setDivs(double x, double y){
this.divs = x/y;
}
double getDivs(){
return divs;
}
void showValor(){
System.out.println("soma: " + this.soma);
}
}
You understand what the line
this.x = ...
andthis.y = ...
does? Understand whatthis.x
andthis.y
sane?– Woss
I believe they receive the entered values, since it is being used Scanner to read the reported values.
– Elder Son
Did you write the code? You know what instance attributes are?
– Woss
It was me, this is an exercise. Attributes are the characteristics that are given to the object that is instantiated in a class. In this case, the attributes are x, y, sum, subt, mult and Divs...
– Elder Son
You need to tell us what the general purpose of the problem is and why this code has no head or foot. According to the purpose it would be written one way or another.
– Maniero
I get it. This class should be abstract and implement the basic operations (sum, subtraction, division and multiplication)... so far I know it has to be 'public Abstract class'. then the goal is to make use of this class in main, requesting the values to the user through the Scanner class and passing to the methods of the generated objects.
– Elder Son