1

in a theoretical view how would the function "absSensor" in the class "sensorFinal" be able to call the function "proximitySensorAdjusted" in the class "proximitySensor" IF the class "sensorFinal" does not have access to the integer "value"?

Is there a standard way to call functions in which access to data is limited or is there a workaround? Thanks in advance!

public class proximitySensor
{
    double adjustment = 5;
    public double proximitySensorAdjusted(int value)
    {
        double finalValue = value + adjustment;
        return finalValue; 
    }
}

public class sensorFinal
{
    public double absSensor()
    {
        proximitySensor sensorValue = new proximitySensor();
        sensorValue.proximitySensorAdjusted( ??? ) 
    }
}
2
  • The only value in sight is an argument of the method proximitySensorAdjusted(int value). It's local to that method; nothing else has access to it and thus nothing else can need access to it either. Commented Nov 5, 2018 at 3:13
  • What exactly is it that you would like accomplish at the point where you have written the line sensorValue.proximitySensorAdjusted(???)? Commented Nov 5, 2018 at 3:55

3 Answers 3

1

You can use method overloading.

public double proximitySensorAdjusted(int value) {
    double finalValue;
    if(value == -1) {
        finalValue = adjustment;
    } else {
        finalValue = value + adjustment;
    }
    return finalValue;
}

public double proximitySensorAdjusted() {
    return proximitySensorAdjusted(-1); //call with some default value
}

Now you can call sensorValue.proximitySensorAdjusted();.

Sign up to request clarification or add additional context in comments.

7 Comments

A better default value would be 0 which will leave the original method unchanged
@smac89 sure, for this use-case. The intent was to explain the concept of method overloading though. :)
Why do you need overloading if you can use 0 as value to have same result? The is a principle KISS
"Magic" argument values are a slippery slope. Can you be certain that -1 or 0 isn't actually a perfectly valid argument that should be handled like any other?
@KevinAnderson I'd argue there is no magic here. The user is not the one supplying the zero argument. 0 is an implementation detail and the behaviour of this argument-less function will presumably be documented. This answer is no different than if we had instead replaced the argument with an optional and the empty optional is treated as 0.
|
0

Instead of ??? you need to specify any integer value for example 0 or 5. In your example int value is an argument name. Only code inside that function has access to it.

4 Comments

that would change the numerical value of "int value" however I want to keep it as is and just call it up with whatever numerical value it has in class "proximitySensor"
What value will be changed when you will call sensorValue.proximitySensorAdjusted(5) ?
value will change to 5 correct if I follow that method
First call sensorValue.proximitySensorAdjusted(5) - value is 5 and result is 10. On second call sensorValue.proximitySensorAdjusted(0) - value is 0 and result is 10. Local values are valid only till the end of the block of code and will not be saved till next execution / call.
0

You can use optionals which is really no different from simply overloading your function, as another answer here suggested.

public class proximitySensor {

    double adjustment = 5;
    public double proximitySensorAdjusted(Optional<Integer> value) {

        double finalValue = value.map(v -> v + adjustment)
                                 .orElseGet(() -> {

            ... // Do something intelligent here
        });
        return finalValue; 
    }
}

Now you call the function with:

sensorValue.proximitySensorAdjusted(Optional.empty());

As a sidenote, the naming convention for class names in Java is Capitalized, so your proximitySensor class should be called ProximitySensor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.