Java "per unit" logic

Asked

Viewed 62 times

-3

How can it be a method to return the following:

Knowing that 220 is equivalent to 0.3275 and that with each addition of 0.0049 in the value of 0.3275, it will increase in one unit the 220, or with each decrease of 0.0049 in the value of 0.3275 will reduce in one unit the 220. How to assemble a method that receives the value of 0.3275, for example and returns me 220, having this factor of 0.0049?

  • 11

    First, write the mathematical formula for this. Then think of the language.

1 answer

2

Although the problem is a programming logic problem and not in itself programming, I made an example code for you to adapt.

    public int calcula(double valor) {
            double unidade = 0.0049; // valor da unidade
            double valorFixo = 0.3275; // valor correspondente a 220 

            // captura a diferença e soma com o 220
            return (int) ((valor - valorFixo ) / unidade) + 220;
    }

Explaining the code:

First one must obtain the difference between the informed value and the value we have

    (valor - 0.3275)

After this it is necessary to know how many units this value represents

    (valor - 0.3275) / 0.0049

So just add it up to 220 and it will have the amount you want

    ((valor - 0.3275) / 0.0049) + 220
  • Thank you very much for the help! That’s what I needed, I’m trying to read a voltage sensor with Andy, and the language is similar to java, with this logic and the method you gave me, it worked here. Thanks!!

Browser other questions tagged

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