Class Conditionals

java.lang.Object
Conditionals

public class Conditionals extends Object
This class doesn't have any state (fields), it's just a convenient place to put some sample code that uses if statements. Don't forget that some of our first examples of IF statements were in the Circle class.
Author:
Brad Richards
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Here we asked a different question so we didn't have a blank.
    boolean
    Don't even need an IF! The boolean expression evaluates to true when we want to return true, and evaluates to false when we want to return false.
    boolean
    Here we're only testing for the *bad* grades.
    boolean
    Takes a letter grade as input and returns true if it's a "good grade".
    void
    isUnlucky(int n)
    This method prints a message if n is unlucky (isn't 7).
    void
    mystery(int a, int b)
    The mystery method takes two integer inputs, and prints "yep" if they're the same.
    scoreToGrade(double score)
    This method takes a numeric score and converts it to a letter grade.
    boolean
    Here we're using || (or) to cram all three .equals checks into ONE test.
    int
    shorterSmallest(int a, int b, int c)
    A shorter version that takes advantage of && (and) to reduce the number of cases.
    void
    simplerMystery(int a, int b)
     
    int
    smallest(int a, int b, int c)
    Takes three integers as inputs and returns the smallest of the three.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Conditionals

      public Conditionals()
  • Method Details

    • mystery

      public void mystery(int a, int b)
      The mystery method takes two integer inputs, and prints "yep" if they're the same. It prints "nope" otherwise.
    • simplerMystery

      public void simplerMystery(int a, int b)
    • smallest

      public int smallest(int a, int b, int c)
      Takes three integers as inputs and returns the smallest of the three.
      Parameters:
      a - An integer input
      b - An integer input
      c - An integer input
      Returns:
      The smallest of the three inputs.
    • shorterSmallest

      public int shorterSmallest(int a, int b, int c)
      A shorter version that takes advantage of && (and) to reduce the number of cases.
    • scoreToGrade

      public String scoreToGrade(double score)
      This method takes a numeric score and converts it to a letter grade. (A double is a numeric value that can have a decimal point in it.)
      Parameters:
      score - A numeric score in the range 0..100
      Returns:
      The letter grade corresponding to the score
    • goodGrade

      public boolean goodGrade(String grade)
      Takes a letter grade as input and returns true if it's a "good grade". We'll assume the input doesn't have a + or a - following it. Note that we're using .equals() to compare two strings, since == doesn't check the actual text of two strings for equality. (== is only for primitive values.)
      Parameters:
      grade - The letter grade to inspect
      Returns:
      True if the grade is a C or higher
    • shorterGoodGrade

      public boolean shorterGoodGrade(String grade)
      Here we're using || (or) to cram all three .equals checks into ONE test.
    • evenShorterGoodGrade

      public boolean evenShorterGoodGrade(String grade)
      Here we're only testing for the *bad* grades. There are fewer of them, so the code ends up being a little shorter.
    • evenEvenShorterGoodGrade

      public boolean evenEvenShorterGoodGrade(String grade)
      Don't even need an IF! The boolean expression evaluates to true when we want to return true, and evaluates to false when we want to return false. We can just return the expression's value!
    • isUnlucky

      public void isUnlucky(int n)
      This method prints a message if n is unlucky (isn't 7). It's bad style to leave the true or false branches empty though...
    • betterIsUnlucky

      public void betterIsUnlucky(int n)
      Here we asked a different question so we didn't have a blank.