Topics:

  • (static) functions: arguments, return types

  • conditions and logical operators
    • relational operators ==, <, etc.
    • logical operators: &&, ||, !
  • truth tables: and, or, not

  • logic and set theory

  • De Morgan’s Laws

  • unit testing

http://codingbat.com/java/Warmup-1

Assignment

HelloFunctions.java

public class HelloFunctions
{
    public static int add(int a, int b)
    {
        return a+b;
    }

    public static float average(float a, float b, float c)
    {
        return (a+b+c)/3;
    }

    public static boolean isGoodGrade(float grade)
    {
        return grade > 50;
        /*
        if (grade > 50.0)
            return true;
        else
            return false;
        */
    }

    public static void hello() 
    {
        System.out.println("Hello, world!");
    }

    public static void main(String[] args)
    {
        hello();     

        int result = add(5, 7);
        System.out.println("result: " + result);

        System.out.println("result: " + add(15, 27));

        System.out.println("average(10, 20, 30): " + average(10, 20, 30));

        System.out.println("isGoodGrade(60): " + isGoodGrade(60));
        System.out.println("isGoodGrade(46): " + isGoodGrade(46));
    }
}

SleepIn.java

public class SleepIn
{
    public static boolean sleepIn(boolean weekday, boolean vacation)
    {
        return !weekday || vacation;
    }

    // test function

    public static void testSleepIn(boolean weekday, boolean vacation, 
                                   boolean expected)
    {
        boolean result = sleepIn(weekday, vacation);

        System.out.print("weekday: " + weekday +
                         " vacation: " + vacation + 
                         " expected: " + expected +
                         " result: " + result + " ");

        if (result == expected)
        {
            System.out.println("Woohoo!");
        }
        else
        {
            System.out.println("Bummer.");
        }
    }

    public static void main(String[] args)
    {
        // unit tests

        testSleepIn(false, false, true);
        testSleepIn(true, false, false);
        testSleepIn(false, true, true);
    }
}

notes notes

pdf