Ex.
No: 1
Date:
Java Program Using Object, Classes, and Constructors
AIM:
To develop Java programs using classes, objects, arrays, and methods to find the largest number in
a list and to calculate and display a student's grade based on given marks.
a. ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of elements n.
Step 3: Create a one-dimensional array of size n.
Step 4: Read all n elements from the user and store them in the array.
Step 5: Initialize a variable max with the value of the first element of the array.
Step 6: Traverse the array from the second element to the last element.
Step 7: For each element, compare it with max. If the current element is greater than max, update
max with this value.
Step 8: After completing the traversal, max will hold the largest element.
Step 9: Display the largest element.
Step 10: End the program.
b. ALGORITHM:
Step 1: Start the program.
Step 2: Read the student's name, roll number, and mark.
Step 3: Create a Student class having the attributes:
• name
• rollNo
• mark
Step 4: Inside the class, write a method calculateGrade() to determine the grade using the given
conditions:
• If mark > 95 → Grade = O
• If mark > 90 and ≤ 95 → Grade = A+
• If mark > 80 and ≤ 90 → Grade = A
• If mark > 70 and ≤ 80 → Grade = B+
• If mark > 60 and ≤ 70 → Grade = B
• If mark ≥ 50 and ≤ 60 → Grade = C
• If mark < 50 → Grade = RA
Step 5: Store the calculated grade in a variable.
Step 6: Create another method display() to print the result in the format:
name(rollNo) secured <grade> Grade
Step 7: Create the main method. Create an object of Student, assign input values, and call the
methods.
Step 8: End the program.
RESULT:
Thus the Java programs were successfully developed and executed using Object, Classes, and
Constructors.
Ex. No: 2
Date: Develop a Java Program using static members
AIM:
To develop Java programs that demonstrate the use of static variables and static methods by
counting the number of objects created in a class and by computing the factorial of a given number.
a. ALGORITHM:
Step 1: Start the program.
Step 2: Create a class named Student.
Step 3: Inside the class, declare a static integer variable (e.g., count) and initialize it to 0. This
variable will keep track of how many objects are created.
Step 4: Create a constructor for the Student class. Inside the constructor, increment the static
variable count by 1 each time the constructor is executed.
Step 5: Write a method display() inside the class to print the total number of Student objects
created.
Step 6: In the main method:
• Create Student objects as required
• Call display() using any object (preferably the last object)
Step 7: The display method should print: "No. of Objects Created : <count>"
Step 8: End the program.
b. ALGORITHM:
Step 1: Start the program.
Step 2: Create a class (e.g., FactorialDemo).
Step 3: Inside the class, define a static method named factorial(int n).
Step 4: Inside the method, initialize a variable fact = 1.
Step 5: Use a loop (for or while) from 1 to n. For each iteration, multiply fact with the loop
variable.
Step 6: After the loop ends, fact holds the factorial of the number.
Step 7: Display the factorial in the format: "The factorial is <value>"
Step 8: In the main method, call the static method using the class name, e.g.,
[Link](5);
Step 9: End the program.
RESULT:
Thus the Java programs were successfully developed and executed using Static members.
Ex. No: 3
Date: Develop a Java Program to demonstrate Inheritance
AIM:
To develop Java programs that demonstrate different types of inheritance—single, multilevel, and
hierarchical—by extending classes, overriding methods, and managing details across related
classes.
a. ALGORITHM:
Step 1: Start the program.
Step 2: Create a class Person with the fields:
• name
• age
Step 3: Create a constructor for the Person class and initialize the fields.
Step 4: Define a method display() in the Person class to print:
• Name
• Age
Step 5: Create a subclass Employee that extends Person.
Step 6: In Employee class, add an additional field: salary
Step 7: Create a constructor for Employee that calls the parent class constructor using super() and
initializes the salary.
Step 8: Override the display() method in Employee class to print:
• Name
• Age
• Salary
Step 9: In the main method:
• Create a Person object and call the display() method.
• Create an Employee object and call its display() method.
Step 10: End the program.
b. ALGORITHM:
Step 1: Start the program.
Step 2: Create a superclass Mobiles with attributes:
• cost
• color
• RAM
Step 3: Provide getter and setter methods for each attribute.
Step 4: Create a subclass Android that extends Mobiles.
Step 5: Add a new attribute in Android class: androidVersion
Step 6: Provide getter and setter methods for androidVersion.
Step 7: Create subclasses Samsung, Nokia, and Xiaomi that extend **Android`.
Step 8: In each subclass, write a display() method to print:
• Brand name
• Price
• Color
• RAM
• Android version
Step 9: In the main method:
• Create objects of Samsung, Nokia, and Xiaomi
• Set values using setter methods
• Call display() for each object
Step 10: End the program.
RESULT:
Thus the Java programs to demonstrate Inheritance were successfully developed and executed.
Ex. No: 4
Date: Solve Real time problems using Java Interface
AIM:
To write a Java program that uses an interface to perform mathematical operations such as finding
the square, factorial, and generating the Fibonacci series for a given number.
ALGORITHM:
Step 1: Start the program.
Step 2: Create an interface (e.g., MathOperations) with abstract methods:
• square(int n)
• factorial(int n)
• fibonacci(int n)
Step 3: Create a class (e.g., MathDemo) that implements the interface.
Step 4: In the class, implement the square() method to compute: square = n × n Then display:
―Squarer is <value>‖
Step 5: Implement the factorial() method:
• Initialize fact = 1
• Loop from 1 to n and multiply to get factorial
• Display: ―Factorial is <value>‖
Step 6: Implement the fibonacci() method:
• Initialize first two terms: 0, 1
• Generate n Fibonacci numbers
• Display them in the format: ―Fibonacci numbers 0 1 …‖
Step 7: In the main method:
• Read the input number from the user
• Create an object of the class
• Call all three methods (square, factorial, and fibonacci)
Step 8: End the program.
RESULT:
Thus the Java program to solve real time problems using Java Interface has been successfully
developed and executed.
Ex. No: 5
Date: Develop Java Program to demonstrate packages
AIM:
To create and use user-defined packages in Java by defining classes in separate packages (student
and college) and accessing them in another program using the import statement.
ALGORITHM:
Step 1: Create a package named student and define a class ClassA with data members rollno, name
and a method displayA().
Step 2: Compile the file [Link] inside the student package directory.
Step 3: Create another package named college and define a class ClassB with data members mark1,
mark2 and a method displayB().
Step 4: Compile the file [Link] inside the college package directory.
Step 5: In a separate program ([Link]), import both packages using:
import [Link];
import [Link];
Step 6: Inside the main() method, create objects for both classes (ClassA and ClassB) and initialize
values using constructors.
Step 7: Call the methods displayA() and displayB() to display roll number, name, and marks.
Step 8: Compute the total marks using values from ClassB.
Step 9: Display the calculated total.
Step 10: Compile and run the program to get the final output.
Program:
I.) Package student:
package student;
public class ClassA
{
public int rollno;
public String name;
public ClassA(int r1,String n1)
{
rollno=r1;
name=n1;
}
public void displayA()
{
[Link](" Roll Number : "+rollno);
[Link](" Name : "+name);
}
}
II.) Package College:
package college;
public class ClassB
{
public int mark1;
public int mark2;
public ClassB(int m1,int m2)
{
mark1=m1;
mark2=m2;
}
public void displayB()
{
[Link]("Mark1 : "+mark1);
[Link]("Mark : "+mark2);
}
}
Import the packages college and student:
import [Link];
import [Link];
class PackageTest
{
public static void main(String args[])
{
int total;
ClassA objectA=new ClassA(1001,"Raja");
ClassB objectB=new ClassB(90,100);
total=objectB.mark1+objectB.mark2;
[Link]();
[Link]();
[Link](" Total : "+total);
}
}
Output:
D:\>cd Oops..
D:\Oops>cd project ..
D:\ Oops \Project>cd student..
D:\ Oops \Project\student>javac [Link]
D:\ Oops \Project\college>cd..
D:\ Oops \Project>cd college..
D:\ Oops \Project\college>javac [Link]
D:\ Oops \Project\college>cd..
D:\ Oops \Project>javac [Link]
D:\ Oops \Project>java PackageTest
Roll Number : 1001
Name : Raja
Mark1 : 90
Mark : 100
Total : 190
RESULT:
Thus the Java program for packages in Java has been successfully developed and executed.
Ex. No: 6 Implement exception handling and creation of user defined
Date:
exceptions in Java
AIM:
To develop Java programs that use exception handling to validate input data—one to check for
vowels in a string using a custom exception, and another to detect duplicate numbers in a list and
compute the sum when no duplicates are found
a. ALGORITHM:
Step 1: Start the program.
Step 2: Create a custom exception class VowelException that extends Exception.
Step 3: Read a string as input from the user.
Step 4: Define a method checkVowels(String str) to process the string.
Step 5: Convert the string to lowercase.
Step 6: Initialize a counter variable count = 0.
Step 7: Traverse each character of the string. If the character is a vowel (a, e, i, o, u), increment the
count.
Step 8: After traversal, check the count:
• If count == 0, throw VowelException with the message "No vowels in the String".
• Otherwise, display "Vowels Exception : No. of Vowels is <count>".
Step 9: In the main method, call checkVowels() inside a try-catch block.
Step 10: Catch the thrown exception and print the exception message.
Step 11: End the program.
b. ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of elements n from the user.
Step 3: Create an array or list to store the n integers.
Step 4: Read all the elements from the user and store them in the array.
Step 5: Create an empty list or set to keep track of numbers already seen.
Step 6: Initialize an empty list to store duplicate numbers (if any).
Step 7: Traverse through each element of the array:
• If the element is already present in the set, add it to the duplicate list.
• Otherwise, add the element to the set.
Step 8: After checking all elements:
• If the duplicate list is not empty, throw a custom exception displaying all duplicate numbers.
• If the duplicate list is empty, proceed to calculate the sum.
Step 9: If no duplicates found, compute the sum of all elements.
Step 10: Display: "Sum of the Elements are <sum>"
Step 11: If duplicates exist, display: "Duplicate Number Exception: Number <x>, <y>, … are
Duplicates"
Step 12: End the program.
RESULT:
Thus the Java programs to implement exception handling and creation of user defined exceptions in
Java were successfully developed and executed.
Ex. No: 7
Date:
Implementing multi-threaded application
AIM:
To demonstrate multithreading in Java using a class that extends Thread and a class that implements
Runnable, and show concurrent execution of multiple threads.
a. ALGORITHM:
Step 1: Start the program.
Step 2: Display the message "Program starts...".
Step 3: Initialize a counter i = 1 for Task 1.
Step 4: Repeat the following steps while i <= 5:
• Display "Task 1 - Count: i".
• Increment i by 1.
Step 5: Reinitialize the counter i = 1 for Task 2.
Step 6: Repeat the following steps while i <= 5:
• Display "Task 2 - Count: i".
• Increment i by 1.
Step 7: Display the message "Program ends...".
Step 8: End the program.
b. ALGORITHM:
Step 1: Create a class NumberThread that extends Thread and override the run() method. Inside the
run() method, print numbers 1 to 5 with a 500 ms delay using [Link]().
Step 2: Create another class LetterRunnable that implements Runnable and override the run()
method. Inside the run() method, print letters ‗A‘ to ‗E‘ with a 500 ms delay.
Step 3: Create a main class MultiThreadDemo containing the main() method.
Step 4: Inside main(), create an object of NumberThread (t1).
Step 5: Create an object of Thread (t2) by passing a new LetterRunnable object to its constructor.
Step 6: Start both threads by calling:
[Link]()
[Link]()
Step 7: Use [Link]() and [Link]() so that the main thread waits until both threads complete execution.
Step 8: After both threads finish, print the message "Main Thread Finished".
Step 9: Compile and run the program to observe concurrent execution of numbers and letters.
a. Program:
public class SingleThreadDemo {
public static void main(String[] args) {
[Link]("Program starts...");
for (int i = 1; i <= 5; i++) {
[Link]("Task 1 - Count: " + i);
}
for (int i = 1; i <= 5; i++) {
[Link]("Task 2 - Count: " + i);
}
[Link]("Program ends...");
}
}
Output:
Program starts...
Task 1 - Count: 1
Task 1 - Count: 2
Task 1 - Count: 3
Task 1 - Count: 4
Task 1 - Count: 5
Task 2 - Count: 1
Task 2 - Count: 2
Task 2 - Count: 3
Task 2 - Count: 4
Task 2 - Count: 5
Program ends...
b. Program:
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Number: " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]("NumberThread Interrupted");
}
}
}
}
class LetterRunnable implements Runnable {
public void run() {
for (char ch = 'A'; ch <= 'E'; ch++) {
[Link]("Letter: " + ch);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]("LetterRunnable Interrupted");
}
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
NumberThread t1 = new NumberThread();
Thread t2 = new Thread(new LetterRunnable());
[Link]();
[Link]();
try {
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Main Thread Interrupted");
}
[Link]("Main Thread Finished");
}
}
Output:
Main Thread Finished
Letter: A
Number: 1
Letter: B
Number: 2
Letter: C
Number: 3
Letter: D
Number: 4
Letter: E
Number: 5
RESULT:
Thus the Java program for implementing a multi-threaded application in Java has been successfully
developed and executed
Ex. No: 8
Date: Develop applications to demonstrate the features of generics
classes and String class
AIM:
To write a Java program that uses a generic class to sort integer values, floating-point numbers, and
strings in ascending order.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a generic class (e.g., Sorter<T extends Comparable<T>>) that stores an array of
elements of type T.
Step 3: Inside the class, write a sort() method that sorts the elements using [Link]() or any
sorting logic, since T implements Comparable.
Step 4: In the main method:
Read the number of elements n from the user.
Step 5: Create:
An integer array of size n
A float array of size n
A string array of size n
Step 6: Read n integers from the user and store them in the integer array.
Step 7: Read n floating-point values and store them in the float array.
Step 8: Read n strings (names) and store them in the string array.
Step 9: Create three objects of the generic Sorter class:
One for integers
One for floats
One for strings
Step 10: Call the sort() method for each object.
Step 11: Display the results in the format:
"The sorted integers are …"
"The sorted float values are …"
"The sorted names are …"
Step 12: End the program.
RESULT:
Thus the Java program to develop applications to demonstrate the features of generics classes and
String class has been successfully developed and executed.
Ex. No: 9
Date:
Performing I/O and file operations
AIM:
To write a Java program that reads SQL queries from a file and displays the number of SELECT,
UPDATE, INSERT, DELETE queries, the number of SELECT queries with WHERE clause, and
the table names in the order they appear.
ALGORITHM:
Step 1: Open the file [Link].
Step 2: Initialize counters for SELECT, WHERE, UPDATE, INSERT, DELETE.
Step 3: Create a list to store table names.
Step 4: Read each line from the file.
Step 5: Convert the line to uppercase.
Step 6: Identify the query type and update the respective counter.
Step 7: Extract the table name and add it to the list if not already present.
Step 8: After reading all lines, display all the counts.
Step 9: End the program.
RESULT:
Thus the Java program to perform I/O and file operations in Java has been successfully developed
and executed.
Ex. No: 10
Date: Content Beyond the Syllabus
Demonstrating HashMap, LinkedHashMap and TreeMap
AIM:
To demonstrate the use of three different Map implementations in Java—HashMap,
LinkedHashMap, and TreeMap—and show how each one handles the ordering of stored key-value
pairs.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a HashMap<Integer, String> object.
Step 3: Insert four key–value pairs into the HashMap using put().
Step 4: Print the HashMap to show that it does not maintain any order.
Step 5: Create a LinkedHashMap<Integer, String> object.
Step 6: Insert the same key–value pairs into the LinkedHashMap.
Step 7: Print the LinkedHashMap to show that it maintains insertion order.
Step 8: Create a TreeMap<Integer, String> object.
Step 9: Insert the same key–value pairs into the TreeMap.
Step 10: Print the TreeMap to show that it stores entries in sorted order of keys.
Step 11: End the program.
Program:
import [Link].*;
public class MapExample {
public static void main(String[] args) { // HashMap: does not maintain any order
Map<Integer, String> hashMap = new HashMap<>();
[Link](3, "Apple");
[Link](1, "Banana");
[Link](4, "Cherry");
[Link](2, "Date");
[Link]("HashMap (Unordered):");
[Link](hashMap);
[Link]();
// LinkedHashMap: maintains insertion order
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
[Link](3, "Apple");
[Link](1, "Banana");
[Link](4, "Cherry");
[Link](2, "Date");
[Link]("LinkedHashMap (Insertion Order):");
[Link](linkedHashMap);
[Link]();
// TreeMap: sorts keys in ascending order
Map<Integer, String> treeMap = new TreeMap<>();
[Link](3, "Apple");
[Link](1, "Banana");
[Link](4, "Cherry");
[Link](2, "Date");
[Link]("TreeMap (Sorted Order by Keys):");
[Link](treeMap);
}
}
Output:
HashMap (Unordered):
{1=Banana, 2=Date, 3=Apple, 4=Cherry}
LinkedHashMap (Insertion Order):
{3=Apple, 1=Banana, 4=Cherry, 2=Date}
TreeMap (Sorted Order by Keys):
{1=Banana, 2=Date, 3=Apple, 4=Cherry}
RESULT:
Thus the Java program for develop Java program using HashMap, LinkedHashMap and TreeMap
has been successfully developed and executed.