Chapter 2 — Java Fundamentals
📚 Chapter Overview
Data is the core of any program. In this chapter, you will learn how to store, categorize, and manipulate data using Java's strict typing system and operators.
Learning Objectives:
- Master Variables and Primitive Data Types.
- Perform Arithmetic and Logical operations.
- Understand Type Casting and the String class.
2.1 Variables and Primitive Types
Part 1 — Definition
A variable is a container that holds data while the Java program is executed. Java is Statically Typed, meaning every variable must be declared with a specific type (e.g., int, float, char) before it can be used. Primitive types are the basic building blocks of data in Java.
Part 2 — Syntax
int age = 25;
double price = 99.99;
char grade = 'A';
boolean isJavaFun = true;
Part 3 — Example
Problem: Calculate the total price of items in a cart.
int quantity = 5;
double unitPrice = 20.5;
double total = quantity * unitPrice;
System.out.println("Total Price: " + total);
Output: Total Price: 102.5
Part 4 — Video
Video: Coming Soon
2.2 Java Operators
Part 1 — Definition
Operators are symbols used to perform operations on variables and values. Java provides Arithmetic (+, -, *, /), Relational (==, !=, <, >), and Logical (&&, ||, !) operators.
Part 2 — Syntax
int sum = a + b;
boolean check = (age >= 18) && (hasID == true);
Part 3 — Example
Problem: Check if a student passed based on their marks.
int marks = 75;
boolean isPass = marks >= 40;
System.out.println("Passed: " + isPass);
Output: Passed: true
Part 4 — Video
Video: Coming Soon
🏆 Chapter Challenge
Challenge Objective
Build a "Student Result Calculator" that uses various data types and operators.
Requirements
- Store Student Name (String) and Marks for 3 subjects (int).
- Calculate the Average (double).
- Display a summary and check if Average is > 50.
Step-by-Step Process
- Declare variables for name and subject marks.
- Sum the marks and divide by 3 to get the average.
- Use
System.out.printlnto print the final result card.
Expected Deliverables
ResultCalculator.javasource code.