Csc 321 Java Programming
Csc 321 Java Programming
USING
JAVA PROGRAM
AYEPEKU FELIX
1
CHAPTER ONE
1. Encapsulation
o Data and methods are bundled inside a class to prevent direct access to object details.
o Example: Private variables in a class can only be accessed through getter and setter
methods.
2. Inheritance
o Allows a new class (child class) to derive properties and methods from an existing
class (parent class).
3. Polymorphism
o The ability to use a single interface to represent different data types or behaviors.
o Example: Method overloading (same method name, different parameters) and method
overriding (modifying inherited methods).
4. Abstraction
o Example: A car's driver only needs to know how to start the engine, not how the
engine works internally.
2
Basic Components of OOP
Advantages of OOP
Disadvantages of OOP
✖ Higher Memory Usage – Objects and class structures consume more memory.
✖ Overhead for Small Applications – Not always efficient for simple scripts.
Applications of OOP
3
CHAPTER TWO
POP vs OOP
Definition
1. Top-down Approach – The program is divided into functions that are executed in sequence.
3. Global and Local Variables – Data can be shared across functions using global variables.
4. Control Flow – Uses loops (for, while), conditionals (if-else), and function calls to control
program execution.
• C
• Pascal
• Fortran
• COBOL
• BASIC
1. Simple and Easy to Implement – Best for small programs and applications.
2. Fast Execution – Direct function calls lead to better performance in simpler applications.
4
3. Efficient Memory Usage – Less memory overhead compared to OOP.
1. Not Suitable for Large Programs – Becomes difficult to manage as program size increases.
2. Data Security Issues – No concept of encapsulation; global variables are accessible to all
functions.
5
#include <stdio.h>
int main() {
float length, width, area;
// Get user input
printf("Enter Length: ");
scanf("%f", &length);
printf("Enter Width: ");
scanf("%f", &width);
// Calculate area
area = length * width;
// Display result
printf("Area of Rectangle: %.2f\n", area);
return 0;
}
Definition
Object-Oriented Programming (OOP) is a programming paradigm that focuses on objects and their
interactions. Objects are instances of classes, which contain both data (attributes) and functions
(methods). OOP follows a bottom-up approach and is designed for modularity and reusability.
1. Encapsulation – Data and functions are wrapped inside objects to prevent direct access.
2. Abstraction – Hides implementation details from the user and only exposes essential features.
4. Polymorphism – Enables the same function name to have different behaviors based on the
object.
6
Examples of Object-Oriented Programming Languages
• Java
• Python
• C++
• C#
• Ruby
1. Modular and Reusable Code – Objects and classes promote code reuse.
3. Easier Maintenance – Programs are divided into objects, making debugging easier.
5. Code Flexibility – Inheritance and polymorphism make it easy to extend and modify code.
1. Slower Execution – Object creation and method calls introduce some overhead.
3. Steep Learning Curve – Requires more time to learn and implement properly.
4. Overhead in Small Applications – Using OOP for small programs can be unnecessary.
• Web Development – Java, Python (Django, Flask), and PHP use OOP for web applications.
7
• Mobile Apps – Android (Java/Kotlin), iOS (Swift).
• Enterprise Software – Large-scale applications like CRM, ERP, and financial systems.
• Machine Learning and AI – Python (TensorFlow, Scikit-learn) uses OOP for model
development.
• Example using JAVA and C++ programming Language to compute Area of rectangle
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Get user input
System.out.print("Enter Length: ");
double length = input.nextDouble();
System.out.print("Enter Width: ");
double width = input.nextDouble();
// Calculate area
double area = length * width;
// Display result
System.out.println("Area of Rectangle: " + area);
input.close();
}
}
8
Comparison: Procedural Programming vs Object-Oriented Programming
Data Handling Data is global and shared Data is encapsulated within objects
Performance Faster for small applications Slightly slower due to object overhead
9
CHAPTER THREE
Overview of Java
• 1991: James Gosling and his team at Sun Microsystems started Java as a project called Oak.
• 2010s: Oracle acquired Java and released newer versions like Java 8 (Lambdas), Java 11
(LTS), and later versions.
Features of Java
• Platform Independence: Java uses the Java Virtual Machine (JVM) to run code on different
operating systems.
• Robust and Secure: Features like exception handling and memory management improve
reliability.
java -version
10
javac -version
System.out.println("Hello, Java!");
11
CHAPTER FOUR
• Primitive Types: int, double, char, boolean, byte, short, long, float
Operators in Java
Type Operators
Arithmetic + - * / %
Logical `&&
12
Type Operators
Bitwise `&
import java.util.Scanner;
Printing Output:
System.out.println("Hello, World!");
• Multi-line Comment:
/*
*/
13
CHAPTER FIVE
Definition
Explanation
• Every program follows a sequence unless control is diverted using selection or looping.
• The program starts from the first instruction and moves to the next in the order they appear.
14
2. Selection (Decision-Making)
Definition
• Selection allows a program to make decisions based on conditions.
• It enables a program to execute different statements based on a given condition.
• The most commonly used selection structures in Java are:
o if statement
o if-else statement
o switch statement
Explanation
• The program evaluates a condition.
• If the condition is true, one block of code executes.
• If the condition is false, another block of code (if specified) executes.
• This structure is essential for implementing logic where different outcomes are required based
on user input or computed values.
Example: Selection using if-else
import java.util.Scanner;
public class SelectionExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
}
}
Key Points
15
• The program evaluates the condition number % 2 == 0.
• If true, it prints "Even"; otherwise, it prints "Odd".
• Selection is useful when different actions need to be taken based on conditions, such as:
o Checking login credentials.
o Validating user input.
o Implementing menu-driven applications.
3. Looping (Iteration)
Definition
• Looping allows a program to execute a block of code multiple times.
• This is useful when repetitive tasks need to be performed.
• The three main types of loops in Java are:
o for loop
o while loop
o do-while loop
Explanation
• Loops reduce redundancy by avoiding the need to write repetitive code.
• A loop runs as long as a specified condition remains true.
• Loops consist of:
1. Initialization - Setting up a loop control variable.
2. Condition - Checking whether the loop should continue.
3. Iteration - Updating the loop control variable after each cycle.
Example: Looping using for loop
public class LoopExample {
public static void main(String[] args) {
System.out.println("Counting from 1 to 5:");
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
16
}
Key Points
• The loop starts with i = 1 (initialization).
• The condition i <= 5 is checked; if true, the loop executes.
• After execution, i++ increases i by 1, and the loop repeats until i exceeds 5.
• Loops are useful for:
o Counting and iteration tasks.
o Processing large data sets.
o Implementing repetitive tasks like calculating sums or averages.
17