{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unit 3 Boolean Expressions and if Statements\n", "Unit 3 Boolean Expressions and if Statements\n", "\n", "- toc: true\n", "- layout: post\n", "- description: Unit 3 Boolean Expressions and if Statements\n", "- categories: [jupyter]\n", "- image: /images/collegeboardlogo.png\n", "- title: Unit 3 Boolean Expressions and if Statements\n", "- author: Rithwikh Varma\n", "- show_tags: true\n", "- comments: true" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "public int getChargeStartTime (int chargeTime) {\n", " int startHour = 0;\n", " int lowPrice = 500_000;\n", " for (int x = 0; x < 23; x++) {\n", " if (lowPrice > getChargingCost(x, chargeTime)) {\n", " lowPrice = getChargingCost(x, chargeTime);\n", " startHour = x;\n", " }\n", " }\n", " return startHour;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "public boolean isStrictlyIncreasing () {\n", " boolean increasing = true;\n", " for (int i = 1; i < digitListSize(); i++) {\n", " if (digitList.get(i-1) >= digitList.get(i)) {\n", " increasing = false;\n", " }\n", " }\n", " return increasing;\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "public boolean isBalanced(ArrayList delimiters) {\n", " int numOpen = 0, numClosed = 0;\n", " for(String d : delimiters) {\n", " if(d.equals(openDel))\n", " numOpen++;\n", " if(d.equals(closeDel))\n", " numClosed++;\n", " if(numClosed > numOpen)\n", " return false;\n", " }\n", " return numOpen == numClosed;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compund Boolean Expression: Combinations of Boolean operators result in the creation of compund boolean operators, which include the &&, ||, or ! operators. (And, Or, Not, respectively)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true\n" ] } ], "source": [ "boolean IsDecember = true;\n", "boolean IsNovember= false;\n", "\n", "\n", "boolean compound = !(IsDecember && IsNovember) && (IsNovember || IsDecember);\n", "\n", "\n", "System.out.println(compound);\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "DeMorgan's Law: Helps with the simplification and abstraction of boolean expressions." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "false false \n" ] } ], "source": [ "boolean first = true;\n", "boolean second = false;\n", "\n", "\n", "// complicated boolean expression\n", "boolean res1 = !((!(first && second)) || (!(first || second)));\n", "\n", "// simplified using De Morgan's Law once\n", "boolean res2 = !((!first || !second) || (!first && !second));\n", "\n", "System.out.println(res1 + \" \" + res2 + \" \");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Truth Tables: Reveal the actual values ot boolean expressions, 0 is false, 1 is true\n", "\n", "0 | 0 | 1 |\n", "1 | 0 | 1 |" ] } ], "metadata": { "kernelspec": { "display_name": "Java", "language": "java", "name": "java" }, "language_info": { "codemirror_mode": "java", "file_extension": ".jshell", "mimetype": "text/x-java-source", "name": "java", "pygments_lexer": "java", "version": "19.0.1+10-21" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }