Java Programming Module

Mentor:

Atharv Pathak

Day 1: Java Introduction & OOP Basics

1

Java and Object-Oriented Programming

On the first day, we learned the basics of Java programming and the fundamentals of Object-Oriented Programming (OOP).

Introduction to Java:

  • Java is a high-level, class-based, object-oriented programming language
  • Designed to have as few implementation dependencies as possible
  • Write once, run anywhere (WORA) capability
  • Java applications are typically compiled to bytecode that can run on any JVM
Java Programming Language
Java is one of the most popular programming languages worldwide

Need for OOP:

  • Modularity: The source code for an object can be written and maintained independently
  • Code reusability: Objects can be reused across programs
  • Pluggability and debugging ease: If a particular object turns out to be problematic, it can simply be replaced
  • Improved software maintainability: Objects can be maintained separately

Procedural vs Object-Oriented Programming:

Procedural Programming Object-Oriented Programming
Program is divided into small parts called functions Program is divided into parts called objects
Follows top-down approach Follows bottom-up approach
No access specifier Has access specifiers like public, private, protected
Adding new data and functions is not easy Adding new data and functions is easy
Less code reusability More code reusability

Day 2: Java Principles & Platform

2

JVM, JRE, JDK and Basic Programming

On the second day, we learned about the Java platform components and wrote our first Java program.

Java Platform Components:

JVM (Java Virtual Machine)

An abstract machine that provides the runtime environment for Java bytecode execution.

JRE (Java Runtime Environment)

Implementation of JVM which provides the libraries and other components to run applications.

JDK (Java Development Kit)

Includes JRE plus development tools like compiler, debugger, and documentation tools.

Applications of Java:

  • Desktop GUI applications
  • Web applications and servers
  • Mobile applications (Android)
  • Enterprise applications
  • Scientific applications
  • Embedded systems
  • Big data technologies

First Java Program:

// Simple Hello World program in Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Day 3: Java Features & Program Structure

3

Java Characteristics and Basic Syntax

On the third day, we explored Java's key features and learned about program structure.

Java Features:

  • Simple: Easy to learn and understand
  • Object-Oriented: Follows OOP paradigm
  • Platform Independent: Write once, run anywhere
  • Secure: Provides security features
  • Robust: Strong memory management and exception handling
  • Multithreaded: Supports concurrent programming
  • High Performance: Just-In-Time compiler enables high performance
  • Distributed: Designed for distributed environment

Java Program Structure:

// Package declaration (optional)
package com.example.mypackage;

// Import statements (optional)
import java.util.Scanner;

// Class declaration (required)
public class MyClass {
    // Main method - entry point of application
    public static void main(String[] args) {
        // Your code here
        System.out.println("Program structure example");
    }
    
    // Other methods (optional)
    public void myMethod() {
        // Method implementation
    }
}

Day 4: Variables, Data Types & Operators

4

Java Fundamentals

On the fourth day, we learned about variables, primitive data types, identifiers, literals, and operators.

Primitive Data Types:

Data Type Size Description Example
byte 1 byte 8-bit signed integer byte b = 100;
short 2 bytes 16-bit signed integer short s = 10000;
int 4 bytes 32-bit signed integer int i = 100000;
long 8 bytes 64-bit signed integer long l = 100000L;
float 4 bytes Single-precision floating point float f = 234.5f;
double 8 bytes Double-precision floating point double d = 123.4;
boolean 1 bit True or false values boolean flag = true;
char 2 bytes Single Unicode character char letter = 'A';

Java Operators:

Arithmetic

+, -, *, /, %, ++, --

Assignment

=, +=, -=, *=, /=, %=

Comparison

==, !=, >, <, >=, <=

Logical

&&, ||, !

Day 5: Expressions, Precedence & Associativity

5

Operator Precedence and Expression Evaluation

On the fifth day, we learned about expressions, precedence rules, and associativity in Java.

Expressions in Java:

An expression is a combination of variables, operators, and method invocations that evaluates to a single value.

// Examples of expressions
int result = 10 + 5 * 2; // Expression with operators
boolean isEqual = (a == b); // Comparison expression
String message = "Hello, " + name; // String concatenation expression

Operator Precedence Rules:

Java operators are evaluated in a specific order when they appear in complex expressions:

  1. Postfix: expr++, expr--
  2. Unary: ++expr, --expr, +expr, -expr, ~, !
  3. Multiplicative: *, /, %
  4. Additive: +, -
  5. Shift: <<, >>, >>>
  6. Relational: <, >, <=, >=, instanceof
  7. Equality: ==, !=
  8. Bitwise AND: &
  9. Bitwise XOR: ^
  10. Bitwise OR: |
  11. Logical AND: &&
  12. Logical OR: ||
  13. Ternary: ? :
  14. Assignment: =, +=, -=, *=, /=, %=, etc.

Associativity:

When operators have the same precedence, associativity determines the order of evaluation:

  • Most operators are left-associative: a + b + c is evaluated as (a + b) + c
  • Assignment operators are right-associative: a = b = c is evaluated as a = (b = c)
  • Ternary operator is right-associative

Day 6: Primitive Conversion, Casting & Flow Control

6

Type Conversion and Control Structures

On the sixth day, we learned about primitive conversion, casting, and flowchart concepts for flow control.

Primitive Conversion and Casting:

Java supports two types of casting:

Widening Casting

Automatically done when converting a smaller type to a larger type (byte → short → int → long → float → double)

int myInt = 9;
double myDouble = myInt; // Automatic casting

Narrowing Casting

Must be done manually when converting a larger type to a smaller size (double → float → long → int → short → byte)

double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting

Flow Control Structures:

Java provides several flow control structures:

Conditional Statements: if, if-else, switch
Looping Statements: for, while, do-while
Branching Statements: break, continue, return

Example of Flow Control:

// Example demonstrating various flow control structures
public class FlowControlExample {
    public static void main(String[] args) {
        // If-else statement
        int number = 10;
        if (number > 0) {
            System.out.println("Positive number");
        } else {
            System.out.println("Non-positive number");
        }

        // For loop
        for (int i = 0; i < 5; i++) {
            System.out.println("Iteration: " + i);
        }
    }
}

Vehicle Management System Project

Applying OOP Concepts in a Real Project

This project demonstrates the application of Object-Oriented Programming concepts learned throughout the module.

Problem Statement: Vehicle Management System

Design and implement a Java application that demonstrates Object-Oriented Programming concepts up to OOPs (without advanced features like databases or multithreading).

Requirements:

  • The system should manage different types of vehicles such as Car, Bike, and Truck
  • All vehicles share common attributes: Brand, Model, Price
  • Each type of vehicle has additional attributes:
    • Car → Number of doors
    • Bike → Type of bike (e.g., Sports, Cruiser)
    • Truck → Load capacity (in tons)
  • Use an abstract class or interface for the base Vehicle definition
  • Implement inheritance where Car, Bike, and Truck extend Vehicle
  • Use method overriding to display vehicle details differently for each type
  • Use encapsulation to ensure proper data hiding (private fields with getters/setters)
  • Demonstrate polymorphism by storing different types of vehicles in a single collection and displaying their details through a loop
  • The program should be menu-driven (optional for extra challenge):
    • Add a vehicle
    • View all vehicles
    • Exit

Project Implementation:

Vehicle (Abstract Class)
- brand: String
- model: String
- price: double
+ displayDetails(): void
+ getBrand(): String
+ setBrand(String): void
↓ (extends)
Car
- numberOfDoors: int
+ displayDetails(): void
Bike
- bikeType: String
+ displayDetails(): void
Truck
- loadCapacity: double
+ displayDetails(): void

Code Implementation:

// Abstract Vehicle class
public abstract class Vehicle {
    private String brand;
    private String model;
    private double price;

    // Constructor
    public Vehicle(String brand, String model, double price) {
        this.brand = brand;
        this.model = model;
        this.price = price;
    }

    // Abstract method to be implemented by subclasses
    public abstract void displayDetails();

    // Getters and setters (encapsulation)
    public String getBrand() { return brand; }
    public void setBrand(String brand) { this.brand = brand; }
    // ... other getters and setters
}
// Car class extending Vehicle
public class Car extends Vehicle {
    private int numberOfDoors;

    public Car(String brand, String model, double price, int numberOfDoors) {
        super(brand, model, price);
        this.numberOfDoors = numberOfDoors;
    }

    // Method overriding
    @Override
    public void displayDetails() {
        System.out.println("Car: " + getBrand() + " " + getModel() +
            ", Price: $" + getPrice() + ", Doors: " + numberOfDoors);
    }
}
// Main class demonstrating polymorphism
import java.util.ArrayList;
import java.util.List;

public class VehicleManagementSystem {
    public static void main(String[] args) {
        List vehicles = new ArrayList<>();

        // Adding different types of vehicles
        vehicles.add(new Car("Toyota", "Camry", 25000, 4));
        vehicles.add(new Bike("Harley-Davidson", "Sportster", 12000, "Cruiser"));
        vehicles.add(new Truck("Ford", "F-150", 35000, 2.5));

        // Demonstrating polymorphism
        for (Vehicle vehicle : vehicles) {
            vehicle.displayDetails(); // Calls appropriate overridden method
        }
    }
}

Key OOP Concepts Demonstrated:

Inheritance

Car, Bike, and Truck classes inherit from the Vehicle base class

Polymorphism

Different vehicle types stored in a single collection and processed uniformly

Encapsulation

Private fields with public getters and setters for data protection

Abstraction

Vehicle class is abstract with an abstract displayDetails method

Additional Resources

Further Learning

Expand your Java knowledge with these recommended resources.

Recommended Books

• "Head First Java" by Kathy Sierra & Bert Bates
• "Effective Java" by Joshua Bloch
• "Java: The Complete Reference" by Herbert Schildt

Online Resources

• Oracle Java Tutorials
• W3Schools Java Tutorial
• GeeksforGeeks Java Programming
• Java Code Geeks

Practice Platforms

• HackerRank Java Challenges
• LeetCode Java Problems
• Codecademy Java Course
• CodingBat Java Exercises

Back to FABLAB Modules