Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 199

Deprecated: Creation of dynamic property Automattic\Jetpack\Connection\Manager::$error_handler is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-connection/src/class-manager.php on line 91

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Instagram_Gallery::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-instagram-gallery.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Mailchimp::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php on line 17

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 21

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 22

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php on line 20

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 24

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Subscribers::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php on line 15

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property WPCOM_Widget_Goodreads::$shelves is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/modules/widgets/goodreads.php on line 33

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /var/www/twodee/blog/wp-includes/script-loader.php on line 348

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198
» CS1: Lecture 14 – Relational Operators

teaching machines

CS1: Lecture 14 – Relational Operators

October 7, 2019 by . Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs1, fall 2019, lectures.

Dear students,

We have seen the Computer as Calculator, a lover of number crunching. We have seen the Computer as Chef, organizing its tasks into self-contained recipes. Now we see the Computer as Philosopher, seeking truth from our data.

Truth is communicated in our programs through the boolean data type. Values of type boolean can only be true or false. We’ve already seen some ways to generate such values: String.startsWith, String.endsWith, String.contains, String.isEmpty, Random.nextBoolean, and so on. There are also a number of operators that yield boolean truths, and these are our focus for today.

Relational Operators

To begin, let’s meet the relational operators: <, <=, >, >=, ==, and !=. == and != are sometimes distinguished as equality operators. We’ll introduce these by writing some methods that serve as predicates, answering yes/no questions like these:

TODO

Here’s your TODO list to complete before our next lecture:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

i <3 u
ur at least a third of me
better half, likely

P.P.S. Here’s the code we wrote together in class…

Certainly.java

package lecture1004.cs145;

public class Certainly {
  public static void samesies(int expected, int actual) {
    System.out.printf("%d == %d%n", expected, actual);
  }

  public static void samesies(boolean expected, boolean actual) {
    System.out.printf("%b == %b%n", expected, actual);
  }
}

Booleandrews.java

package lecture1004.cs145;

public class Booleandrews {
  public static void main(String[] args) {
    Certainly.samesies(false, isSocialSecurityEligible(14));
    Certainly.samesies(true, isSocialSecurityEligible(74));
    Certainly.samesies(true, isSocialSecurityEligible(62));
    Certainly.samesies(false, isSocialSecurityEligible(61));

    Certainly.samesies(false, isWalk(0, false));
    Certainly.samesies(true, isWalk(4, false));
    Certainly.samesies(false, isWalk(3, false));

    Certainly.samesies(false, isPolygamous(0));
    Certainly.samesies(false, isPolygamous(1));
    Certainly.samesies(true, isPolygamous(2));
    Certainly.samesies(true, isPolygamous(17));

    Certainly.samesies(true, isLegalToDrive(0));
    Certainly.samesies(true, isLegalToDrive(0.0799999));
    Certainly.samesies(false, isLegalToDrive(0.08));
    Certainly.samesies(false, isLegalToDrive(0.09));
    Certainly.samesies(false, isLegalToDrive(2));

    Certainly.samesies(true, isWrongNumber(11, 7));
    Certainly.samesies(false, isWrongNumber(11, 11));
    Certainly.samesies(false, isWrongNumber(0.001, 0.001));
    Certainly.samesies(false, isWrongNumber(0.001, 0.0005 + 0.0005));
    Certainly.samesies(true, isWrongNumber(0.001, 0.0004 + 0.0005));
    Certainly.samesies(false, isWrongNumber(0.001, 0.0010));

    Certainly.samesies(true, isNameTattooable(10, "Chris"));
    Certainly.samesies(false, isNameTattooable(10, "Christopher"));
    Certainly.samesies(false, isNameTattooable(10, "Demosthenes"));
    Certainly.samesies(false, isNameTattooable(0, "Mom"));
    Certainly.samesies(true, isNameTattooable(0, ""));
  }

  public static boolean isSocialSecurityEligible(int age) {
    return age >= 62;
  }

  public static boolean isWalk(int numberOfBalls, boolean isHitByPitch) {
    return numberOfBalls == 4;
  }

  public static boolean isPolygamous(int nSpouses) {
    return nSpouses > 1;
  }

  public static boolean isLegalToDrive(double bac) {
    return bac < 0.08;
  }

  public static boolean isWrongNumber(double rightNumber, double guess) {
//    return rightNumber != guess;
    return Math.abs(rightNumber - guess) > 0.001;
  }

  public static boolean isNameTattooable(int nKnuckles, String name) {
    return nKnuckles >= name.length();
  }
}

Quarter.java

package lecture1004.cs148;

public class Quarter {
  public static boolean hasJustOne(String haystack, char needle) {
    int count = haystack.length() - haystack.replaceAll("" + needle, "").length();
    return count == 1;
  }

  public static boolean hasJustOneJustin(String haystack, char needle) {
    return haystack.indexOf(needle) == haystack.lastIndexOf(needle);
  }

  public static boolean hasJustOneVictor(String haystack, char needle) {
    int count = 0;
    for (int i = 0; i < haystack.length(); ++i) {
      if (haystack.charAt(i) == needle) {
        return true;
      }
    }

    return count == 1;
  }
}

Certainly.java

package lecture1004.cs148;

public class Certainly {
  public static void samesies(int expected, int actual, String message) {
    if (expected != actual) {
      throw new RuntimeException(String.format("%s%nExpected: %d%n  Actual: %d%n", message, expected, actual));
    }
  }

  public static void samesies(boolean expected, boolean actual, String message) {
    if (expected != actual) {
      throw new RuntimeException(String.format("%s%nExpected: %b%n  Actual: %b%n", message, expected, actual));
    }
  }
}

Booleanning.java

package lecture1004.cs148;

public class Booleanning {
  public static void main(String[] args) {
    Certainly.samesies(false, isSocialSecurityEligible(25), "");
    Certainly.samesies(true, isSocialSecurityEligible(65), "");
    Certainly.samesies(false, isSocialSecurityEligible(18), "");
    Certainly.samesies(true, isSocialSecurityEligible(300), "");
    Certainly.samesies(true, isSocialSecurityEligible(62), "");
    Certainly.samesies(false, isSocialSecurityEligible(61), "");

    Certainly.samesies(false, isWalk(0), "walk 0");
    Certainly.samesies(false, isWalk(1), "walk 1");
    Certainly.samesies(false, isWalk(2), "walk 2");
    Certainly.samesies(false, isWalk(3), "walk 3");
    Certainly.samesies(true, isWalk(4), "walk 4");

    Certainly.samesies(false, isPolygamous(0), "poly 0");
    Certainly.samesies(false, isPolygamous(1), "poly 1");
    Certainly.samesies(true, isPolygamous(2), "poly 2");
    Certainly.samesies(true, isPolygamous(17), "poly 17");

    Certainly.samesies(true, isLegalToDrive(0), "bac 0");
    Certainly.samesies(true, isLegalToDrive(0.079999), "bac 0.079999");
    Certainly.samesies(false, isLegalToDrive(0.08), "bac 0.08");
    Certainly.samesies(false, isLegalToDrive(0.09), "bac 0.09");

    Certainly.samesies(true, isTattooable(10, "Sam"), "sam");
    Certainly.samesies(false, isTattooable(10, "Bartholomew"), "Barthomolew");
    Certainly.samesies(false, isTattooable(10, "Demosthenes"), "Demosthenes");
  }

  public static boolean isSocialSecurityEligible(int age) {
    return age >= 62;
  }

  public static boolean isWalk(int nBalls) {
    return nBalls == 4;
  }

  public static boolean isPolygamous(int nPartners) {
    return nPartners >= 2;
  }

  public static boolean isLegalToDrive(double bac) {
    return bac < 0.08;
  }

  public static boolean isTattooable(int nKnuckles, String name) {
    int nCharacters = name.length();
    return nKnuckles >= nCharacters;
  }
}