How do I make the methods receive the x and y parameters?

Asked

Viewed 53 times

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 = ... and this.y = ... does? Understand what this.x and this.y sane?

  • I believe they receive the entered values, since it is being used Scanner to read the reported values.

  • Did you write the code? You know what instance attributes are?

  • 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...

  • 1

    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.

  • 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.

Show 1 more comment

1 answer

2


I imagine this is what you wish:

import java.util.Scanner;

class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Informe dois valores: ");
        Calculadora calc = new Calculadora(sc.nextDouble(), sc.nextDouble());
        System.out.println(calc.soma());
        System.out.println(calc.subtracao());
        System.out.println(calc.multiplicacao());
        System.out.println(calc.divisao());
    }
}

class Calculadora {
    protected double x;
    protected double y;

    Calculadora(double x, double y) {
        this.x = x;
        this.y = y;
    }
    double soma() {
        return x + y;
    }
    double subtracao() {
        return x - y;
    }
    double multiplicacao() {
        return x * y;
    }
    double divisao() {
        return x / y;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I did not do abstract to facilitate the test, but this does not change the essence. I have my doubts if you need it. In fact I see little use for this class.

I removed the request for typing from the class because they are different responsibilities. Who takes care of the interaction with the user should not be the business class. I used a builder which is the right thing to do.

If you already have the data you do not need to keep passing to the methods as argument. Every method that is not static already has a parameter that is the this, then the object values are already accessible in all its methods. And the this nor need to be typed when the name is not ambiguous.

I simplified a lot doing everything described in the question. And I gave better names and cleaner formatting.

  • That’s exactly what it was. I was wondering how to use Scanner to assign values to x and y.

Browser other questions tagged

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