Programming Fundamentals Lecture Notes

Jun 22, 2024

Programming Fundamentals Lecture Notes

Introduction to Programming Concepts

  • Purpose: Understanding basic programming concepts and how they are implemented through code.
  • Main Focus: Conditional statements, object references, and string manipulations.

Conditional Statements

  • Conditional Statements Role: Used to perform different actions based on different conditions.

    • Examples: if, else, switch statements.
    • Scenario Handling: Example provided to handle different fruit types using conditional checks.

    if Statement

    • Structure: Basic conditional check to execute a code block if the condition is true.
    • Example: if(fruit.equals('mango')) { print('King of fruits'); }

    else Statement

    • Function: Extends if statement to execute a different code block if the if condition is false.
    • Example: Handling multiple fruit types.

    switch Statement

    • Purpose: More readable and organized way to handle multiple conditional checks.
    • Example: Handling fruits like mango, orange, etc.
    • Case Statements: Each case corresponds to a possible value of the switch expression.
    switch(fruit) {
        case 'mango':
            print('King of fruits');
            break;
        case 'orange':
            print('Citrus fruit');
            break;
        // default case
        default:
            print('Unknown fruit');
    }
    

Object References and Memory Management

  • Understanding Objects: How objects are stored and referenced in memory.
  • Memory Allocation: Multiple references can point to the same object in memory.
  • String Comparison: Importance of comparing strings using equals method rather than ==.
    • Example: if (str1.equals(str2)) { /* do something */ }

Practical Examples and Code Implementation

  • Scanner Input: Using scanner to take user inputs and process data accordingly.
    • Example: Scanning fruit names and printing details based on conditional checks.
  • String Manipulations: Creating, comparing, and modifying strings in Java.
    • Common Methods: equals, substring, charAt, etc.
    String fruit =