JAVA
PRACTICE NOTEBOOK
BY/ MAHMOUD AHMED
Practice
Beginner Level:
1. Data Types and Variables:
❖ Declare and initialize variables of different data types (int, double, char,
boolean).
❖ Write a program to swap two numbers without using a temporary variable.
❖ Calculate the area and perimeter of a circle.
2. Control Flow:
❖ Write a program to check if a number is prime or not.
❖ Implement a simple calculator using switch-case.
❖ Create a program to print the Fibonacci series up to a given number.
❖ Print a pattern like a pyramid or a triangle using nested loops.
❖ Create a multiplication table using nested loops.
❖ Write a program to find the factorial of a number using a while loop.
❖ Create a program to check if a number is a palindrome using a while loop.
3. Arrays:
❖ Find the largest and smallest element in an array.
❖ Reverse an array without using an extra array.
❖ Sort an array using bubble sort or selection sort.
❖ Find the average of all elements in an array.
❖ Find the second largest element in an array.
❖ Rotate an array by a given number of positions.
❖ Print a matrix in spiral order.
❖ Find the saddle point in a matrix.
PAGE 1
Intermediate Level:
1. Strings:
❖ Reverse a string without using any built-in functions.
❖ Check if a string is a palindrome.
❖ Count the number of vowels and consonants in a string.
2. Objects and Classes:
❖ Create a class Person with attributes like name, age, and address.
❖ Implement a method to calculate the age of a person.
❖ Create a class BankAccount with methods to deposit, withdraw, and check
balance.
3. Inheritance and Polymorphism:
❖ Create a class Animal with methods eat() and sleep().
❖ Create subclasses Dog and Cat that inherit from Animal and override the
methods.
❖ Demonstrate polymorphism by creating an array of Animal objects and
calling the methods on each object.
4. Exception Handling:
❖ Write a program to handle ArithmeticException and
ArrayIndexOutOfBoundsException.
❖ Create a custom exception to handle invalid input.
5. File I/O:
❖ Read a file and count the number of words and lines.
❖ Write a program to append text to an existing file.
PAGE 2
Input/Output:
1. Console Input/Output:
❖ Write a program to read a number from the user and print its square root.
❖ Create a program to calculate the simple interest using input from the user.
2. File I/O:
❖ Write a program to read a text file and count the number of words.
❖ Create a program to write a list of numbers to a file.
Advanced Level:
1. Collections Framework:
❖ Implement a custom ArrayList class.
❖ Sort a list of objects using a custom comparator.
❖ Create a program to find the frequency of words in a text file.
2. Generics:
❖ Create a generic class to store any type of data.
❖ Implement a generic method to swap two elements in an array.
3. Concurrency:
❖ Create a multi-threaded program to count the number of prime numbers
within a range.
❖ Implement a producer-consumer problem using threads and
synchronization.
4. Design Patterns:
❖ Design a singleton pattern to create a single instance of a class.
❖ Implement the observer pattern to notify multiple objects of a change in
state.
5. Functional Programming:
❖ Use lambda expressions to sort a list of strings.
❖ Implement a stream pipeline to filter and map elements from a list.
PAGE 3
Object-Oriented Programming:
1. Inheritance and Polymorphism:
❖ Create a hierarchy of classes for vehicles (Car, Bike, Truck) with properties
like speed, color, and methods like start(), stop(), accelerate().
❖ Implement polymorphism to call the start() method on different vehicle
objects.
2. Encapsulation:
❖ Create a class Person with private fields for name, age, and address.
❖ Provide public getter and setter methods to access and modify these fields.
3. Abstraction:
❖ Create an abstract class Shape with abstract methods area() and
perimeter().
❖ Create concrete subclasses Circle, Rectangle, and Triangle that extend
Shape and implement the abstract methods.
Collections Framework:
1. ArrayList and LinkedList:
❖ Implement a custom stack using an ArrayList.
❖ Create a program to check if a given string is a palindrome using a
LinkedList.
2. HashMap and HashSet:
❖ Create a program to count the frequency of words in a text file using a
HashMap.
❖ Implement a phonebook using a HashMap to store names and phone
numbers.
3. TreeSet and TreeMap:
❖ Sort a list of objects based on multiple criteria using a TreeSet.
❖ Create a program to store student records (name, roll number, marks) in a
TreeMap, sorted by roll number.
PAGE 4
Exception Handling:
1. Custom Exceptions:
❖ Create a custom exception InvalidAgeException to handle invalid age
values.
❖ Throw and catch the custom exception in a program.
2. Exception Propagation:
❖ Implement a method that throws an exception if a file is not found.
❖ Handle the exception in the calling method.
File I/O:
1. Serialization:
❖ Serialize an object of a custom class to a file.
❖ Deserialize the object from the file.
2. RandomAccessFile:
❖ Create a program to read and write data to a random access file.
Concurrency:
1. Thread Synchronization:
❖ Implement a multi-threaded program to count the number of prime
numbers within a range using synchronization.
2. Producer-Consumer Problem:
❖ Create a producer-consumer problem using threads and a shared buffer.
PAGE 5
Hints & Tips
Beginner Level:
1. Data Types and Variables:
❖ Understand basic data types like int, double, char, and boolean.
❖ Learn how to declare and initialize variables.
❖ Practice simple arithmetic operations.
2. Control Flow:
❖ Use if-else statements for conditional execution.
❖ Use switch-case statements for multiple-choice decisions.
❖ Use for and while loops for repetitive tasks.
3. Arrays:
❖ Learn how to store and access elements in an array.
❖ Practice array operations like finding minimum, maximum, and average.
❖ Implement sorting algorithms like bubble sort and selection sort.
Intermediate Level:
1. Strings:
❖ Understand string manipulation techniques like concatenation, substring,
and character extraction.
❖ Practice string comparison and searching.
2. Objects and Classes:
❖ Grasp the concept of object-oriented programming (OOP).
❖ Learn how to define classes, objects, and methods.
❖ Understand encapsulation, inheritance, and polymorphism.
3. Exception Handling:
❖ Learn how to handle exceptions using try-catch blocks.
❖ Create custom exceptions to handle specific error conditions.
PAGE 6
4. File I/O:
❖ Learn how to read from and write to files.
❖ Use file input/output streams to perform file operations.
Advanced Level:
1. Collections Framework:
❖ Understand the different types of collections (List, Set, Map).
❖ Learn how to use ArrayList, LinkedList, HashSet, HashMap, TreeSet, and
TreeMap.
❖ Practice iterator-based traversal and collection operations.
2. Generics:
❖ Learn how to create generic classes and methods to work with different
data types.
❖ Understand type parameters and type arguments.
3. Concurrency:
❖ Learn how to create and manage threads.
❖ Understand thread synchronization and inter-thread communication.
❖ Practice using synchronization primitives like synchronized blocks and
volatile keywords.
4. Design Patterns:
❖ Learn about common design patterns like Singleton, Observer, Factory,
and Strategy.
❖ Understand the problem they solve and how to implement them.
5. Functional Programming:
❖ Learn about functional interfaces, lambda expressions, and streams.
❖ Practice functional programming concepts like higher-order functions and
lazy evaluation.
PAGE 7
Additional Tips for Practice:
• Breakdown problems: Divide complex problems into smaller, more
manageable subproblems.
• Test your code: Use a debugger to step through your code and identify errors.
• Learn from others: Look at other people's code and learn from their solutions.
• Practice regularly: Consistent practice is key to improving your skills.
• Use online resources: There are many online resources available, such as
tutorials, forums, and coding challenges.
• Participate in coding challenges: Websites like HackerRank, LeetCode, and
Codewars offer a variety of coding challenges to test your skills.
• Contribute to open-source projects: This is a great way to learn from
experienced developers and contribute to real-world projects.
• Build your own projects: Create your own projects to apply your knowledge
and learn new skills.
• Stay up-to-date with the latest trends: Java is constantly evolving, so it's
important to stay up-to-date with the latest trends and technologies.
PAGE 8