COM 123: PROGRAMMING LANGUAGE USING JAVA 1
(ND 1 – Second Semester)
Course Note (Student-Friendly Version)
Credit Hours: 6 (2 theory + 4 practical)
Why We Learn Java
Java is one of the most important programming languages in the world.
Java is used to build:
• Android apps
• ATM and banking systems
• school management systems
• websites (backend)
• enterprise systems (big companies)
• desktop applications
Java is also a good language for beginners because:
• it teaches programming discipline
• it forces you to write correct code
• it introduces Object-Oriented Programming (OOP)
What This Course Will Teach You
By the end of this course, you should be able to:
Write Java programs correctly
• Understand classes and objects
• Use operators and expressions
• Use if/else and switch for decision making
• Use loops for repetition
• Work with strings and characters
• Write Java programs that solve real problems
PART 1: JAVA PROGRAMMING BASICS
(Weeks 1–3)
1.1 What is a Program?
A program is a set of instructions that tells the computer what to do.
Example:
• “Add two numbers”
• “Calculate student GPA”
• “Check if a password is correct”
• “Print a receipt”
1.2 What is Java?
Java is a high-level programming language created to write programs that can run on
many computers.
Java is special because of this statement:
“Write Once, Run Anywhere”
Meaning:
If you write a Java program on Windows, you can run it on:
• Linux
• Mac
• other systems
(as long as Java is installed)
1.3 Java is Not Like C
Students who already know a little C should note:
In C:
• you can write code outside functions
• you compile to machine code directly
In Java:
• everything must be inside a class
• Java compiles into bytecode (.class)
• Java runs using JVM (Java Virtual Machine)
1.4 The Basic Components of a Java Program
A Java program normally has:
1. A class
2. A main method
3. Statements inside the main method
4. Output statements
5. Variables and data types
1.5 Your First Java Program
Let’s start with the simplest program.
public class FirstProgram {
public static void main(String[] args) {
[Link]("Welcome to Java Programming!");
}
}
Let’s Understand It Slowly
1. public class FirstProgram
• Java programs are written inside classes.
• FirstProgram is the name of the class.
📌 Important Rule:
The file name must match the class name.
So the file must be saved as:
[Link]
2. public static void main(String[] args)
This line looks scary, but don’t panic.
For now, just understand this:
This is where Java starts running your program.
If there is no main(), the program will not run.
3. [Link](...)
This prints output to the screen.
Example:
[Link]("Hello");
[Link]("World");
Output:
Hello
World
1.6 Common Beginner Mistakes in Java
Java is strict. Beginners often make these mistakes:
❌ Mistake 1: Wrong filename
If your class is FirstProgram, file must be [Link].
❌ Mistake 2: Missing semicolon
[Link]("Hello")
Correct:
[Link]("Hello");
❌ Mistake 3: Wrong bracket
You must open and close braces correctly:
{
// code
}
1.7 How Java Programs Are Created and Run
There are 2 main steps:
Step 1: Compile
Compilation checks your code for errors and converts it into bytecode.
javac [Link]
If successful, it produces:
[Link]
Step 2: Run
java FirstProgram
Why No .java When Running?
Because Java runs the .class file.
PART 2: DATA TYPES, VARIABLES AND CONSTANTS
(Still in Weeks 1–3)
2.1 What is a Variable?
A variable is like a container that stores data.
Example:
• name
• age
• score
• CGPA
Example:
int age = 20;
Meaning:
• create a variable named age
• store 20 inside it
2.2 Data Types in Java
Data type tells Java what kind of value you want to store.
Common Data Types for ND Students
Type Stores Example
whole 10, 500, -
int
numbers 3
decimal
double 2.5, 4.75
numbers
single
char 'A', 'b'
character
boolea
true/false true, false
n
String text "Bello"
Example Program
public class DataTypesDemo {
public static void main(String[] args) {
int age = 19;
double cgpa = 4.75;
char grade = 'A';
boolean isStudent = true;
String name = "Salim";
[Link](name);
[Link](age);
[Link](cgpa);
[Link](grade);
[Link](isStudent);
}
}
2.3 Difference Between char and String
This is very important.
char
• stores only ONE character
• uses single quotes ' '
Example:
char letter = 'A';
String
• stores many characters (word/sentence)
• uses double quotes " "
Example:
String word = "Apple";
2.4 Constants in Java
A constant is a value that should not change.
In Java we use final.
Example:
final double PI = 3.14159;
If you try:
PI = 5.0;
Java will reject it.
PART 3: OBJECT-ORIENTED PROGRAMMING
(Weeks 4–5)
3.1 What is Object-Oriented Programming (OOP)?
OOP is a way of programming where we represent real-world things using:
• Classes
• Objects
3.2 Understanding a Class
A class is like a blueprint.
Example:
A class Student can describe:
• student name
• matric number
• department
• CGPA
Student Class Example
public class Student {
String name;
int matricNo;
double cgpa;
}
This class is not a real student yet.
It is only a plan.
3.3 Understanding an Object
An object is a real thing created from a class.
Example:
• Bello is an object of Student
• Amina is an object of Student
Creating an Object
Student s1 = new Student();
3.4 Object Declaration vs Object Creation
This is exactly what your curriculum mentions.
Declaration only
Student s1;
Here:
• s1 exists
• but no Student object exists in memory
Creation
s1 = new Student();
Now the object is created.
Declaration + Creation (common way)
Student s1 = new Student();
3.5 Fields (Attributes)
Fields are variables inside a class.
Example:
String name;
int matricNo;
double cgpa;
3.6 Methods
Methods are actions inside a class.
Example:
A student can:
• display info
• calculate grade
• update CGPA
Example
public class Student {
String name;
double cgpa;
void display() {
[Link]("Name: " + name);
[Link]("CGPA: " + cgpa);
}
}
3.7 Constructors
A constructor is used to give initial values to an object.
Example: Constructor without parameter
public class Student {
String name;
double cgpa;
Student() {
name = "Unknown";
cgpa = 0.0;
}
}
Example: Constructor with parameter
public class Student {
String name;
double cgpa;
Student(String n, double c) {
name = n;
cgpa = c;
}
}
3.8 Method Overloading
Overloading means:
Same method name, but different parameters.
Example:
public class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
3.9 Local vs Instance Variables
Instance Variable
Declared inside class, outside methods.
Example:
String name;
Local Variable
Declared inside a method.
Example:
void test() {
int x = 10;
}
3.10 Public vs Private
public
Anyone can access.
private
Only inside the class.
Example:
public class Student {
private double cgpa;
}
3.11 Garbage Collection
In Java, memory is managed automatically.
If an object is no longer used, Java removes it.
Example:
Student s = new Student();
s = null; // object can be cleared
3.12 Nested Classes
A nested class is a class inside another class.
Example:
class Outer {
class Inner {
void show() {
[Link]("Hello from inner class");
}
}
}
PART 4: EXPRESSIONS AND INPUT/OUTPUT
(Weeks 6–7)
4.1 What is an Expression?
An expression is something that produces a value.
Example:
int x = 5 + 2;
Here:
• 5 + 2 is an expression
• it produces 7
4.2 Operator Precedence
This means: which operation happens first.
Example:
int x = 10 + 5 * 2;
Multiplication happens first:
• 5 * 2 = 10
• 10 + 10 = 20
So x = 20
4.3 Input in Java
The easiest way for ND students is using Scanner.
Example: Reading from keyboard
import [Link];
public class InputDemo {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
4.4 Integer and Real Numbers in Memory
int
Stored in binary, 32 bits.
double
Stored as floating point, 64 bits.
Key Idea for Students
Floating numbers may not always be exact.
PART 5: CONDITIONAL STATEMENTS
(Weeks 7–8)
5.1 What is a Condition?
A condition is something that is either true or false.
Example:
age >= 18
5.2 Relational and Logical Operators
Relational:
• >
• <
• >=
• <=
• ==
• !=
Logical:
• &&
• ||
• !
5.3 IF Statement
Example:
int score = 65;
if(score >= 50) {
[Link]("Pass");
}
5.4 IF...ELSE
Example:
int score = 45;
if(score >= 50) {
[Link]("Pass");
} else {
[Link]("Fail");
}
5.5 ELSE IF Ladder (Next-IF)
Example:
int score = 72;
if(score >= 70) {
[Link]("A");
} else if(score >= 60) {
[Link]("B");
} else if(score >= 50) {
[Link]("C");
} else {
[Link]("F");
}
5.6 Nested IF
Example:
int age = 20;
int score = 60;
if(age >= 18) {
if(score >= 50) {
[Link]("Eligible");
} else {
[Link]("Not eligible due to low score");
}
} else {
[Link]("Not eligible due to age");
}
PART 6: ITERATION STATEMENTS
(Weeks 9–10)
6.1 Why Loops Are Important
Loops help us avoid writing repeated code.
Example:
Instead of writing:
[Link](1);
[Link](2);
[Link](3);
We use loop.
6.2 while loop
Example:
int i = 1;
while(i <= 5) {
[Link](i);
i++;
}
6.3 do-while loop
Example:
int i = 1;
do {
[Link](i);
i++;
} while(i <= 5);
6.4 for loop
Example:
for(int i = 1; i <= 5; i++) {
[Link](i);
}
6.5 Nested Loops
Example: Print multiplication table
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
[Link]((i*j) + "\t");
}
[Link]();
}
6.6 Recursion
Recursion means a method calling itself.
Example: Factorial
public class RecursionDemo {
static int factorial(int n) {
if(n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
[Link](factorial(5));
}
}
PART 7: STRINGS AND CHARACTER MANIPULATION
(Weeks 11–15)
7.1 Working with Characters
Example:
char c = 'A';
Java characters can be:
• compared
• converted
• checked if digit/letter
Useful Character Methods
Java has a Character class.
Example:
[Link]([Link]('5')); // true
[Link]([Link]('A')); // true
[Link]([Link]('B')); // b
7.2 String vs StringBuffer
String
• cannot change
• every modification creates a new object
Example:
String s = "Hello";
s = s + " World";
StringBuffer
• can change
• good for repeated modifications
Example:
StringBuffer sb = new StringBuffer("Hello");
[Link](" World");
[Link](sb);
7.3 Primitive vs Reference Types
Primitive
Stores value directly.
Example:
int x = 10;
Reference
Stores address of object.
Example:
String name = "Bello";
7.4 Testing Strings: == vs equals
Using ==
Checks if they are the same object.
Using equals()
Checks if the text is the same.
Example:
String a = new String("Hello");
String b = new String("Hello");
[Link](a == b); // false
[Link]([Link](b)); // true
7.5 Passing Objects
Java passes copies, but for objects it passes the reference value.
Example:
class Student {
String name;
}
public class PassObject {
static void change(Student s) {
[Link] = "Changed";
}
public static void main(String[] args) {
Student st = new Student();
[Link] = "Original";
change(st);
[Link]([Link]); // Changed
}
}
FINAL ADVICE TO STUDENTS
To become good in Java:
1. Practice writing small programs daily
2. Don’t fear errors — errors teach you
3. Understand variables and loops well
4. Master classes and objects early
5. Learn strings properly because they appear everywhere
CLASS PRACTICAL WORK (Suggested)
By the end of COM 123, students should be able to build:
• A grading system
• A simple ATM program
• A student record program using classes
• A string manipulation program
• A menu-driven calculator
• Multiplication table generator