0

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;
}
Renuz
  • 1,587
  • 7
  • 21
  • 34
  • 1
    please restate your question. it is very unclear. – Stas Jaro Nov 01 '11 at 02:32
  • Formatting your code will make it easier for other people (and you) to read it, which means people will generally answer faster. – Brendan Long Nov 01 '11 at 02:32
  • Okay i Reformatted the question, and the code is only an example of what i attempted at, it has nothing to do with the question, except showing how i cannot do it. – Renuz Nov 01 '11 at 02:39
  • On an unrelated note, a system exit value of `0` generally indicates success. If you *are* going to just kill the program, you should exit with any value in the range 1-255 (like `System.exit(1);`). – Brendan Long Nov 01 '11 at 02:48
  • Do the numbers 1-255 matter? Do they have different attributes or are they all just the same. – Renuz Nov 01 '11 at 02:55
  • @Burninfate - The only generally agreed on exit code standard is that `0` is good and everything else is bad. – Brendan Long Nov 01 '11 at 03:00

3 Answers3

3

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 */;
}
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • @DannielFischer `private boolean isValid(int aRating) { if (aRating >= 1 && aRating <= 10) return true; else return false; }` Would this be the way to do it? – Renuz Nov 01 '11 at 02:49
  • That would be _a_ way to do it. With a few hundred lines of code more below your belt, you'll prefer `return aRating >= 1 && aRating <= 10;` though, since `if (condition) return true; else return false;` can always be reduced to `return condition;`. Code formatting in comments is restricted, afaik. – Daniel Fischer Nov 01 '11 at 02:59
2

Calling Private Methods

Calling a private method from a public one is exactly the same as calling any other method:

public void doStuff() {
    System.out.println("Stuff done.");
}

private void doOtherStuff() {
    System.out.println("Other stuff done.");
}

public void showHowItWorks() {
    doStuff();
    doOtherStuff();

    // or if you prefer this style:
    this.doStuff();
    this.doOtherStuff();
}

The important difference is that you can only call private methods from inside the class where they were defined:

class PublicExample {
    public static void doStuff() {
        System.out.println("Stuff done.");
    }
}

class PrivateExample {
    private static void doStuff() {
        System.out.println("Stuff done.");
    }
}

class Example {
    public static void Main(String[] args) {
        PublicExample.doStuff(); // Works
        PrivateExample.doStuff(); // Doesn't work (because it's private and defined in a different class)
    }
}

Return Values

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);
    }
}

The problem with this approach is that your program immediately dies if you input a bad hour or minute. What if I want to loop until I get a good input?

I think the main problem is that you don't know how to return a value from a method. Here's an example of a function that always returns true:

public static boolean trueExample() {
    return true;
}

public static void main(String[] args) {
    boolean returnValue = trueExample();
    System.out.println("trueExample() returned " + returnValue);
}

You can also make it more complicated:

/**
 * @return the input value minus one.
 */
public static int oneLess(int num) {
    return num - 1;
}

public static void main(String[] args) {
    int num = 10;

    // Print 10
    System.out.println(num);

    // Print 9
    num = oneLess(num);
    System.out.println(num);

    // Print 8
    num = oneLess(num);
    System.out.println(num);
}

Here's one that will return true if num is in the range 10-20 and false otherwise:

public boolean isValid(int num) {
    return num >= 10 && num <= 20;
}

This version does exactly the same thing, but you may find it easier to read since it's more explicit:

public boolean isValid(int num) {
    /* Numbers less than 10 are invalid */
    if(num < 10) {
        return false;
    }
    /* Numbers greater than 20 are invalid */
    else if (num > 20) {
        return false;
    }
    /* By process of elimination, everything else is valid */
    else {
        return true;
    }
}

Let me know if that's not enough to help you with your problem.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0

Just like you would call any normal method as long as the private method is visible to the public method.

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53