CH6 – Boolean Methods

  1. Create a new class named BooleanSingle and add a method named isSingleDigit.
public class BooleanSingle{ 
  public static boolean isSingleDigit(int x){
    if (x > -10 && x < 10){  
      return true;
    }
    else {
      return false;
    }
  }
}
  1. Add a main method that calls the isSingleDigit method.
public static void main(String[] args){
  boolean isSingleOne = BooleanSingle.isSingleDigit(4);
  boolean isSingleTwo = isSingleDigit(22);
  System.out.println("is 4 single digit: " + isSingleOne);
  System.out.println("is 22 single digit: " + isSingleTwo);
}
  1. Compile and run the program.
$ javac BooleanSingle.java
$ java BooleanSingle
is 4 single digit: true
is 22 single digit: false

The complete class appears as follows.

public class BooleanSingle{
  public static boolean isSingleDigit(int x){
    if (x > -10 && x < 10){
      return true;
    }
    else {
      return false;
    }
  }
  public static void main(String[] args){
    boolean isSingleOne = BooleanSingle.isSingleDigit(4);
    boolean isSingleTwo = isSingleDigit(22);
    System.out.println("is 4 single digit: " + isSingleOne);
    System.out.println("is 22 single digit: " + isSingleTwo);
  }
}