Search code examples
javamethodsbooleanprivate-methods

How to write a private method that returns True for certain peramaters?


So the exact wording is, "Write a private method isValid(aRating) that returns true if the given rating is valid, which is between 1-10."

private void isValid(aRating)
{

}

What Goes in the Method above in order to return a true value if given rating is valid, Validity meaning a number 1-10.

This is what i attempted, the instructor wants it the "proper way" This is just an example of what i attempted at, It is a different program completely.(Ignore if confusing).

private void isValid(int hour,  int minute) 
{
    if (hour >= 0 && hour <=23) 
    {
        System.out.println("Hour is valid");
        hourIsValid = true;
    } 

    else 
    {
        System.out.println("Hour is not valid");
        hourIsValid = false;
        System.exit(0);
    }
}

Would this be correct

    private boolean isValid(int aRating)
{                     
     if (aRating >= 1 && aRating <= 10)
         return true;
     else
         return false;
}

Solution

  • The problem demands a function that returns true under some conditions, so the signature cannot be

    private void isValid(int aRating) {}
    

    it needs a return type. Now, true's type is boolean, so make it

    private boolean isValid(int aRating) {
        return /* validity test here */;
    }