Basic Exercises JAVA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Basic Exercises:

Hello World Program:

Write a Java program that prints "Hello, World!" to the console.

Sum of Two Numbers:

Write a program that takes two numbers as input and prints their sum.

Calculate Area of a Circle:

Write a program to calculate the area of a circle given its radius. Use the formula: Area = π * radius *
radius.

Temperature Converter:

Write a program that converts temperature from Celsius to Fahrenheit. The formula is: Fahrenheit =
(Celsius * 9/5) + 32.

Factorial Calculator:

Write a program to calculate the factorial of a given number. The factorial of a number is the product of
all positive integers less than or equal to that number.

Fibonacci Series:

Write a program to generate the Fibonacci series up to a certain number of terms.

Reverse a String:

Write a program to reverse a given string.

Check for Palindrome:

Write a program to check if a given string is a palindrome or not.

Find the Largest Element in an Array:

Write a program to find the largest element in a given array.


Simple Calculator:

Write a program that acts as a simple calculator, capable of performing addition, subtraction,
multiplication, and division.

Intermediate Exercises:

Prime Number Checker:

Write a program to check if a given number is prime or not.

Binary to Decimal Converter:

Write a program that converts a binary number to its decimal equivalent.

Decimal to Binary Converter:

Write a program that converts a decimal number to its binary equivalent.

Matrix Addition:

Write a program to add two matrices of the same size.

Matrix Multiplication:

Write a program to multiply two matrices.

Find the Nth Prime Number:

Write a program to find the Nth prime number.

Sorting Algorithms:

Implement various sorting algorithms such as Bubble Sort, Selection Sort, Insertion Sort, etc., and use
them to sort arrays of integers.

File Handling:
Write a program to read data from a file, perform some operations, and write the results back to
another file.

Linked List Implementation:

Implement a singly linked list and perform basic operations like insertion, deletion, and traversal.

Stack Implementation:

Implement a stack data structure and perform basic operations like push, pop, peek, and isEmpty.

These exercises cover a range of topics and difficulty levels, helping beginners build a solid foundation in
Java programming.

Basic File Handling Operations:

Reading from a File:

Use classes like File, FileReader, or BufferedReader to read data from a text file line by line or character
by character.

Writing to a File:

Use classes like File, FileWriter, or BufferedWriter to write data to a text file.

File Input/Output Streams:

Java provides FileInputStream and FileOutputStream for reading and writing binary data from and to
files.

Exception Handling:

Proper exception handling is crucial when dealing with file operations, as files may not always be
accessible or may not exist.

Java File Handling Exercises:

Word Counter:
Write a program that reads a text file and counts the number of words in it. Exclude punctuation marks
and consider spaces as word separators.

File Copy:

Create a program that copies the contents of one text file into another.

File Concatenation:

Write a program that concatenates the contents of two text files into a single text file.

File Encryption/Decryption:

Implement a simple encryption/decryption algorithm to encode and decode the contents of a text file.

File Search:

Write a program that searches for a specific word or phrase within a text file and prints the line numbers
where it occurs.

CSV Parser:

Create a program that reads data from a CSV (Comma Separated Values) file, parses it, and performs
some operations like calculating averages, finding maximum/minimum values, etc., on the data.

File Compression/Decompression:

Implement a simple file compression algorithm to compress the contents of a text file and then
decompress it back to its original form.

Serialization/Deserialization:

Learn about Java's Serialization API and write a program to serialize objects into a file and deserialize
them back.

File Information:

Write a program that displays information about a file, such as its size, last modified timestamp,
permissions, etc.
Log Analyzer:

Create a program that reads a log file generated by another application, parses the log entries, and
extracts useful information like error messages, timestamps, etc.

These exercises will help you become familiar with various aspects of file handling in Java while also
enhancing your programming skills. Feel free to explore additional features and functionalities as you
become more comfortable with file handling concepts in Java

You might also like