Tina Comston
Comp-111 Week 7
Franklin University
Week 7
Preparation
    • Read Chapter 5 (sections 5.1 - 5.8) in the COMP 111 Reading Guide.
      Guided Learning Activity
      Assignment
      • Problem 1
      Operators, conditions, and decision structures

      Java supports the following operators:

      <    less than
      >    greater than
      ==  equality (notice the double equal sign)
      A single equal sign assigns a value, a double equal sign tests for equality.
      <=   less than or equal
      >=   greater than or equal
      !       not, reverses the result
      !=     not equal

      These operators are used when comparing two values.  The result of which is a boolean value, either true or false.   The result of the comparison is typically referred to as a "condition".

      Conditions are used in decision structures.

      A decision structure is also referred to as if statement

      The format of a decision structure/if statement is:
      if (condition)
      {
              statements to execute
      }

      for example:
      if (x < y)
      {
              System.out.println("y is greater than x");
      }

      When reviewing the format of the if statement take note of the following:
      • The condition is enclosed in parenthesis
      • The body of the if statement is enclosed in curly brackets.
      • If the condition equates to true, the statements within the brackets are executed.
      • The only semi-colons are those at the end of the statements in the body of the if statement - NO semi-colons after the condition.
      The example given above is a single decision structure.  The statements are only executed if the condition is true.  Another form is the dual decision structure.  This type of structure has statements to execute when the condition is true, but also has statements to execute when the condition is false.

      if (condition)
      {
              statements to execute when true
      }
      else
      {
              statements to execute when false
      }


      for example:

      if (x < y )
      {
              System.out.println("Y is greater than X");
      }
      else
      {
              System.out.println("X is greater than Y");
      }

      Note that:
      • Else condition statements are enclosed in curly brackets.
      • Else condition statements are executed if the condition equates to false.
      • Again, the only semi-colons are at the end of the statements within the curly brackets.
      YouTube video on comparisons https://www.youtube.com/watch?v=6O_lwNnWFR0

      Object Comparison
      Because objects are user-defined, you cannot use the == comparison operator to verify if two objects contain the same value.  The == operator will check to see if two object variables contain the exact same object, meaning they point to the same location in memory, but they will not validate that the two objects contain the same value.

      For example:
      String tmpName = "Joe";
      String newName = "Joe";

      The above String objects both contain the same value, but they are two completely different objects.  If you were to have the following comparison
      if (tmpName == newName)
      The condition would equate to false.  These are not the same object.

      If you want to check to see if they have the same value, you have to use a method written to compare the values.  The String class has 3 such methods:
      • .equals() - this method checks for exact equality, including the case
        • "Joe" would not be equal to "joe" because the case is different
        • format:
          • string1.equals(string2)
      • .equalsIgnoreCase() - this method checks for equality of characters, regardless of the case
        • "Joe" would be considered equal to "joe"
        • format:
          • string1.equalsIgnoreCase(string2)
      • .compareTo() - this method checks to see if a string is equal, greater than alphabetically, or less than alphabetically
        • If the strings are equal, 0 is returned
        • If string1 comes before string2, -1 is returned
        • If string2 comes before string1, 1 is returned
        • format:
          • string1.compareTo(string2)
      YouTube video on string comparison https://www.youtube.com/watch?v=QkjeFtNAgmk
        • Copy the main method into a BlueJ class.
        • Compile
        • Execute the code and view the output.
        • Answer the questions.
      • Problem 2
      And, Or, Not

      And  (&&)
      The && is used to join two conditions together.  When using the && operator, both conditions must equate to true for the entire expression to equate to true.

      For example:
      if (x < y && y < z)

      In the above expression, both x must be less than y and y must be less than z for the condition to equate to true

      Or (||)
      The || operator is also used to join two conditions together.  When using the || operator, if either condition equates to true the entire expression equates to true.

      For example:

      if (x < y || y < z)

      In the above expression, if x is less than y OR y is less than z, the condition equates to true.

      Not (!)
      The ! is used to reverse a condition.  A condition that would equate to true is reversed and becomes false and likewise a condition that would equate to false is reversed and becomes true.

      For example

      if (! (x < y))

      In this expression you would first determine if x is less than y and the reverse the result.


      YouTube video on logical/boolean operators https://www.youtube.com/watch?v=2o0jEUhOqaw
        • Review each of the if statements.  Each contains a syntax error.  Identify the error.
          • You can also copy the statements and paste them into BlueJ to aid in the identification of the errors.
      • Problem 3
        • Review each of the if statements.  Each contains a logic error. Identify the error.
          • You can also copy the statements and paste them into BlueJ to aid in the identification of the errors.
      • Problem 4
        • Complete the truth table, determining if the result of the expression will be true or false.
          • Remember to work on those expressions within the parenthesis first.
      • Problem 5
        • As a child, did you ever create a secret code?   Perhaps you assigned a number to each of the letters in the alphabet or gave each letter a special symbol?  You will be writing a class to generate such a code.  This class will consist of 1 constructor and 3 methods.
      Reflection Paper
      • Continue journaling for your reflection paper.
      Lab 2 - due this week
      •   Read through the instructions. Make sure you have a clear understanding of what is needed.