IQMATH
TECHNOLOGIES
Learn. Grow. Lead with IQmath.
JAVA PROGRAMMING –
MODULE 1
Control Flow & Fundamentals
◦Understand Java’s basic syntax and structure
◦Work with data types and variables
◦Implement conditional logic using if-else and switch
◦Use loops: for, while, and do-while
◦Understand break, continue, and nested loops
Java Program Structure
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Java Program Structure
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
◦Every Java program starts with a class
◦main method is the entry point
◦Statements end with ;
Java Syntax Basics
◦Case-sensitive
◦Use {} to define code blocks
◦Comments:
// Single line
/* Multi-line */
Taking Input in Java
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
String name = sc.nextLine();
◦Scanner class used for input
◦Common methods: nextInt(), nextLine(), nextDouble()
Data Types in Java
Data Types in Java
Type Size Example
int 4B int age = 25;
double 8B double pi = 3.14;
char 2B char ch = 'A';
boolean 1 bit boolean b = true;
Variables and Constants
int count = 10;
final double PI = 3.14159;
◦Use final for constants
◦Variable naming: meaningful, camelCase
Type Conversion
int x = 5;
double y = x; // Implicit
double a = 7.9;
int b = (int)a; // Explicit
◦Implicit: smaller to larger
◦Explicit: use casting (type)
Conditional Statements
Overview
◦Control the flow based on conditions
◦Types:
if, if-else, if-else-if
switch-case
if-else Example
if (marks > 90)
System.out.println("Excellent");
else
System.out.println("Keep Trying");
◦Conditions use relational operators: >, <, ==, !=, etc.
switch-case Example
int day = 2;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid");
}
◦break is required to prevent fall-through
Looping Structure Overview
◦Repeat code blocks efficiently
◦Types:
for
while
do-while
for Loop Example
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
◦Initialization → Condition → Increment
while Loop Example
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
◦Used when number of iterations is unknown
do-while Loop Example
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
◦Runs at least once
break and continue
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
System.out.println(i);
}
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.println(i);
}
Nested Loops Example
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
◦Loop inside another loop
◦Useful in matrix, pattern problems
Pattern Printing Example
for (int i = 1; i <= 5; i++) { Output:
for (int j = 1; j <= i; j++) { *
System.out.print("*"); **
} ***
System.out.println(); ****
} *****
Practice Problem: Employee
Performance Tracker
JAVA PROGRAMMING –
MODULE 2
Arrays and String Handling
◦Understand 1D and 2D arrays: declaration,
initialization, and traversal
◦Learn how to use the String and StringBuilder classes
◦Perform common string operations: reverse,
palindrome check, anagram check, substring
extraction
◦Count character frequency, change case, and handle
string conversions
What is an Array?
◦A data structure that stores multiple values of the
same type
◦Elements are stored in contiguous memory locations
◦Fixed size, zero-based indexing
int[] marks = new int[5];
1D Array – Declaration &
Initialization
// Declaration
int[] numbers = new int[3];
// Initialization
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
// Inline Declaration
int[] values = {10, 20, 30};
1D Array – Traversal
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
◦Use .length to get array size
◦Enhanced for-loop:
for (int num : numbers) {
System.out.println(num);
}
2D Arrays – Declaration &
Initialization
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
◦Each row is an array
◦Declaration: int[][] arr = new int[2][3];
2D Array – Traversal
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
◦Nested loops to iterate through rows and columns
String Class Basics
◦Strings in Java are immutable
◦String pool – reused objects for memory efficiency
String name = "Alice";
System.out.println(name.length()); // 5
System.out.println(name.charAt(0)); // A
Common String Operations
String s = "Hello Java";
s.length(); // 10
s.toUpperCase(); // "HELLO JAVA"
s.toLowerCase(); // "hello java"
s.substring(0, 5); // "Hello"
s.indexOf("Java"); // 6
s.equals("HELLO"); // false
String Reversal
String original = "hello";
String reversed = "";
for (int i = original.length() - 1; i >= 0; i--) {
reversed += original.charAt(i);
}
System.out.println(reversed); // "olleh“
◦Note: Inefficient due to immutability → Use
StringBuilder for better performance
Check Palindrome
String str = "madam";
boolean isPalindrome = true;
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
System.out.println(isPalindrome ? "Yes" : "No");
Anagram Checker
char[] s1 = "listen".toCharArray();
char[] s2 = "silent".toCharArray();
Arrays.sort(s1);
Arrays.sort(s2);
boolean isAnagram = Arrays.equals(s1, s2);
System.out.println(isAnagram ? "Anagram" : "Not
Anagram");
Character Frequency Count
String text = "hello";
int[] freq = new int[26];
for (char c : text.toCharArray()) {
freq[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (freq[i] > 0)
System.out.println((char)(i + 'a') + ": " + freq[i]);
}
Case Conversion
String s = "Java123";
StringBuilder result = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isUpperCase(c))
result.append(Character.toLowerCase(c));
else if (Character.isLowerCase(c))
result.append(Character.toUpperCase(c));
else
result.append(c);
}
System.out.println(result); // Output: jAVA123
StringBuilder Class
◦Used for mutable string manipulation
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // "Hello World"
sb.insert(5, ","); // "Hello, World"
sb.reverse(); // "dlroW ,olleH"
System.out.println(sb);
When to Use String vs
StringBuilder
Feature String StringBuilder
Mutability Immutable Mutable
Thread-safe Yes No
Performance Slower in loops Faster in loops
Use StringBuilder in loops or heavy manipulation
scenarios.
StringBuilder Class
◦Used for mutable string manipulation
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // "Hello World"
sb.insert(5, ","); // "Hello, World"
sb.reverse(); // "dlroW ,olleH"
System.out.println(sb);
Practice Problem: Student Name
Matrix Analyzer
JAVA PROGRAMMING –
MODULE 3
Module 3: Searching & Sorting
Algorithms
◦Searching: Finding an element in a collection
◦Sorting: Rearranging elements in a meaningful order
(ascending/descending)
◦Algorithms Covered:
Linear Search
Binary Search
Insertion Sort
What is Linear Search?
◦Sequentially checks each element in the array
◦Time Complexity: O(n)
◦Works on unsorted arrays
◦Best Case: Element at index 0 → O(1)
◦Worst Case: Element not present → O(n)
Linear Search – Code Example
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target)
return i;
}
return -1;
}
What is Binary Search?
◦Only works on sorted arrays
◦Divide and conquer: Repeatedly halves the search
space
◦Time Complexity: O(log n)
Binary Search – Code Example
public static int binarySearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
Linear vs Binary Search –
Comparison Table
Feature Linear Search Binary Search
Works on Sorted? No Yes
Time Complexity O(n) O(log n)
Method Sequential Divide & Conquer
Space Complexity O(1) O(1)
Insertion Sort – Concept
◦Builds sorted array one element at a time
◦Shifts elements to insert current value in the correct
position
◦Best for small datasets or nearly sorted data
Insertion Sort – Code Example
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // Shift right
j--;
}
arr[j + 1] = key; // Insert key at correct position
}
}
JAVA PROGRAMMING –
MODULE 4
Module 4: Collections in Java
◦Need for collections over arrays
◦Java Collections Framework (JCF)
◦Core Interfaces and Implementations
◦Focused Topics:
ArrayList
HashSet
HashMap
◦Iteration Techniques
Why Collections?
◦Arrays are fixed in size and type
◦Collections offer:
Dynamic sizing
Flexible data types
Built-in utility methods
◦Ideal for storing, manipulating, and accessing groups
of objects
Introduction to Collections
Framework
◦A unified architecture to represent and manipulate collections
◦Hierarchy:
Iterable
↳ Collection
↳ List
↳ ArrayList
↳ Set
↳ HashSet
↳ Map
↳ HashMap
ArrayList – Overview
◦Implements the List interface
◦Maintains insertion order
◦Allows duplicates
◦Dynamic resizing of array
ArrayList – Overview
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> cities = new ArrayList<>();
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Delhi");
for (String city : cities) {
System.out.println(city);
}
}
}
HashSet – Overview
◦Implements Set interface
◦No duplicates allowed
◦No guaranteed order of elements
◦Uses hashing for performance
HashSet – Code Example
import java.util.*;
public class HashSetDemo {
public static void main(String[] args) {
HashSet<String> emails = new HashSet<>();
emails.add("alice@gmail.com");
emails.add("bob@gmail.com");
emails.add("alice@gmail.com");
for (String email : emails) {
System.out.println(email);
}
}
}
HashMap – Overview
◦Implements Map interface
◦Stores key-value pairs
◦Keys must be unique; values can repeat
◦Access is very fast due to hashing
HashMap – Code Example
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
HashMap<String, Integer> studentMarks = new HashMap<>();
studentMarks.put("Alice", 90);
studentMarks.put("Bob", 80);
studentMarks.put("Alice", 95); // Overwrites value
for (String name : studentMarks.keySet()) {
System.out.println(name + ": " + studentMarks.get(name));
}
}
}
Iterating over an ArrayList
import java.util.*;
public class ArrayListIteration {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>(Arrays.asList("Apple",
"Banana", "Mango"));
for (String fruit : fruits) {
System.out.println("For-each: " + fruit);
}
for (int i = 0; i < fruits.size(); i++) {
System.out.println("Index: " + fruits.get(i));
}
}
}
Iterating over a HashSet
import java.util.*;
public class HashSetIteration {
public static void main(String[] args) {
HashSet<String> countries = new
HashSet<>(Arrays.asList("India", "USA", "UK"));
for (String country : countries) {
System.out.println("For-each: " + country);
}
}
}
Iterating over a HashMap
import java.util.*;
public class HashMapIteration {
public static void main(String[] args) {
HashMap<String, Integer> marks = new HashMap<>();
marks.put("Alice", 90);
marks.put("Bob", 85);
marks.put("Charlie", 78);
for (String key : marks.keySet()) {
System.out.println("KeySet: " + key + " -> " + marks.get(key));
}
}
}
JAVA PROGRAMMING –
MODULE 5
Module 5: Object-Oriented
Programming (OOP)
◦What is OOP?
◦Creating Classes and Objects
◦Constructors in Java
◦The this keyword
What is Object-Oriented
Programming?
◦OOP is a programming paradigm that models real-
world entities as "objects" containing data (fields) and
behavior (methods).
◦Encapsulation
◦Abstraction
◦Inheritance
◦Polymorphism
Class and Object – Overview
◦ Class – A blueprint or template to create class Car {
objects. String color;
◦ Object – An instance of a class. void drive() {
System.out.println("Driving a " + color + "
car.");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object creation
myCar.color = "red"; // Setting field
myCar.drive(); // Calling method
}
}
Real-World Analogy
Concept Real World Example
Class Blueprint of a House
Object Actual built house
Field Rooms, color, doors
Method Open door, switch lights
Constructors in Java
Definition:
◦A constructor is a special method used to initialize
objects.
Key Points:
◦Has the same name as the class
◦No return type (not even void)
◦Called automatically when object is created
Constructor – Example
class Student { public class Main {
String name; public static void main(String[] args)
// Constructor {
Student(String n) { Student s1 = new Student("Alice");
name = n; s1.greet(); // Output: Hello, my
name is Alice
}
}
void greet() {
}
System.out.println("Hello, my
name is " + name);
}
}
Types of Constructors
Type Description
Provided by Java if no constructor is
Default Constructor
defined
Accepts arguments to initialize object
Parameterized Constructor
fields
Used to create a copy of an object
Copy Constructor (manual)
(not built-in)
The this Keyword
◦Refers to the current object
◦Differentiates instance variables from parameters
◦Can be used to invoke other constructors
this Keyword – Example
class Book {
String title;
Book(String title) {
this.title = title; // Resolving variable shadowing
}
void print() {
System.out.println("Book: " + this.title);
}
}
Constructor Chaining Using
this()
class Employee {
String name;
int age;
Employee() {
this("Unknown", 0); // Calling another constructor
}
Employee(String name, int age) {
this.name = name;
this.age = age;
}
void show() {
System.out.println(name + " - " + age);
}
}