Lecture 3(1)

Recap From Last Two Lectures

  • Designing an APP for ordinary users requested by a bank.

  • Important considerations include:

    • Data Types

    • Java Syntax

Motivation

  • Example scenario: Assigning a negative value for the radius in ComputeArea.java.

    • Consequence: Program prints an invalid result.

    • Solution: Avoid computation of area if the radius is negative.

boolean Data Type

  • Definition: A variable that holds a boolean value is known as a boolean variable.

  • Usage: The boolean data type is used for declaring boolean variables.

  • Evaluation: A boolean expression evaluates to either true or false.

    • Example: boolean b = (1 > 2); // b is assigned the value false

Comparison in Programs

  • In programming, it’s common to compare two values.

  • Java provides six comparison (relational) operators:

    • Example Comparisons:

      • i > j

      • radius > 0

Relational Operators in Java

Mathematical Name

Symbol

Example (radius = 5)

Result

less than

<

radius < 0

false

less than or equal

<=

radius <= 0

false

greater than

>

radius > 0

true

greater than or equal

>=

radius >= 0

true

equal to

==

radius == 0

false

not equal to

!=

radius != 0

true

Selection Statements

  • Definition: Use conditions that are boolean expressions.

  • Types of selection statements in Java:

    • One-way if statements.

    • Two-way if-else statements.

    • Nested if statements.

    • Multi-way if-else statements.

    • Switch statements.

    • Conditional operators.

One-way if Statements

  • Structure:

    if (boolean-expression) {
    statement(s);
    }

Example of One-way if Statement

  • Code Example:

    if (radius >= 0) {
    area = 3.14 * radius * radius;
    System.out.println("The area for the circle of radius " + radius + " is " + area);
    }
  • Note: The boolean expression is enclosed in parentheses, block braces can be omitted for a single statement.

Two-way if-else Statements

  • Structure:

    if (boolean-expression) {
    statement(s) for true-case;
    } else {
    statement(s) for false-case;
    }

Example of Two-way if-else Statement

  • Code Example:

    if (radius >= 0) {
    area = 3.14159 * radius * radius;
    System.out.println("The area of the circle of radius " + radius + " is " + area);
    } else {
    System.out.println("Negative input");
    }

Classroom Exercise

  • Task: Write a program to check if the integer number entered by the user is odd or even.

Random Number Generation

  • Syntax: Math.random() produces a random double

  • Example: int x = (int) (5 * Math.random()) + 4 generates a random integer in the range [4, 9).

Example: ArithmeticTest

  • Write a program to:

    1. Randomly generate two single-digit integers.

    2. Display a question like "What is 3 + 4?".

    3. After the user answers, indicate whether it is correct.

Nested-if Statement

  • Definition: An if statement within another if statement.

  • Example:

    if (i > k) {
    if (j > k) {
    System.out.println("i and j are greater than k");
    }
    } else {
    System.out.println("i is less than or equal to k");
    }

Sample Program - Grades

  • Task: Compute the letter grade based on numeric score (0-100).

    • A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: <60.

Multi-way if-else Statements

  • Structure: Check multiple conditions.

    • Example: Trace if-else statements to determine the grade based on a score.

Switch Statements

  • Syntax:

    switch (switch-expression) {
    case value1:
    statement(s)1;
    break;
    case value2:
    statement(s)2;
    break;
    default:
    System.out.println("Errors: invalid status");
    }

When to Use Switch vs. If-Else

  • Use if-then-else for ranges of values or conditions.

  • Use switch for a single integer or enumerated value.

Conditional Operators

  • Usage of ternary operator for concise conditions.

    • Example: Instead of using if-else, use: y = (x > 0) ? 1 : -1;.

Logical Operators

  • Operators and their meanings:

    • !: NOT

    • &&: AND

    • ||: OR

    • ^: Exclusive OR

Example: Leap Year Calculation

  • Write a program to determine if a year is a leap year based on:

    1. Divisible by 400

    2. Or (divisible by 4 AND not divisible by 100).

Practice Exercise 1

  • Task: Prompt for IMDB rating and Metascore to determine if a movie is recommended.

    • Criteria: IMDB rating > 7.0 and Metascore > 60.

Practice Exercise 2

  • Task: Prompt for year and month to display the number of days in that month.

    • Leap year handling for February.

robot