0% found this document useful (0 votes)
23 views3 pages

Java Lab Programs for AY 2024-25

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Java Lab Programs for AY 2024-25

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA Lab Record Programs

AY 2024-25 Odd Sem


Part-1

1. Write a program to print prime numbers with in range

2. Write a program to find min, max and average of array elements read from the user

dynamically.

3. Write a program to implement matrix multiplication.

4. Program to demonstrate jagged arrays

5. Program to read n number and display them in sorted order

6. Write a program to demonstrate bitwise operators

7. Program to demonstrate type casting.

8. Create a class Student with attributes such as name, roll number, and marks.

9. Write a program to create five student objects and display details as a report.

10. Program to demonstrate Account class.

11. Program to demonstrate any 15 methods of String class.

12. Write a program to implement Single Inheritance with super keyword. (Account class

SavingsAccount class)

13. Write a program to demonstrate Hierarchical inheritance: Super class - Emp, its

subclasses (PEmp, FEmp). Include necessary fields and methods

14. Write a program to demonstrate MultiLevel Inheritance (Person Emp Manager)

15. Write a program to demonstrate method overloading

16. Write a program to implement Dynamic Polymorphism using Dynamic Method

Dispatch (DMD) concept.

17. Program to demonstrate Abstract class.

18. Program to implement multiple inheritance using interfaces


Create an ArrayStack class which implements Arr (disp() method) and Stack (push() and

pop() methods) interfaces.

19. Program to implement user defined package

20. Write a program to handle any two predefined exceptions using try catch blocks.

21. Write a program to demonstrate user defined exception (AgeException)

22. Write a program to create three user threads by implementing Runnable interface.

23. Write a Program to create Two threads by extending thread class. (One thread finding

the square of given array, other thread converts every character of string to uppercase)

24. Write a Java program that correctly implements producer consumer problem using the

concept of inter thread communication.

Lab Internal-1 on 6th Nov 2024: online GDB Assignments, Lab Record, Oracle Academy

Certificate will be evaluated.

Test includes 2 to three programs from part-1 (24 programs)

Part-2

25. Write a Java program to create File object on a file, and then displays information about

whether the file exists, whether the file is readable, whether the file is writable, the type of

file and the length of the file in bytes. display contents of file.

26. Write a program to copy the data from one file to another using FileIO.

27. Write a Java program to implement serialization concept

28. Write a program to create FileReader on a file and print Type of file, number of

characters, number of words, number of lines

29. Write a Java program to implement iteration over Collection using Iterator interface

and Listlterator interface

30. Write a program to implement LinkedList of product (pid, pname). Implement search,

update and delete based pid.


31. Program to implement sorting using Stack

32. Write a program to check given string is palindrome or not using deque.

33. Write a program to remove duplicates from the list of numbers using HashSet

34. Write a program to demonstrate HashMap. Store Country name and Capital city in Map.

Implement search and remove functionality

35. Write a program to demonstrate TreeMap. Store the Roll number and student name.

Add data in random order and print sorted order, first student data and last student data

36. Write a program to read multiple lines of text and print largest and shortest word using

StringTokenizer

37. Write an AWT program to implement a simple calculator with two TextFields, four

Buttons (named Add, Sub, Mul, Div) and a Label to display results. Use ActionListener to

handle events.

38. Program to demonstrate Mouse Events & Key events.

39. Write a Java program to perform following operations on student data using JDBC

i. create & insert a student record

40. Write a Java program to perform following operations on student data using JDBC

a) select b) update c) delete

Lab Internal-1 on 13th Dec 2024 2.30pm to 4.15pm

Common questions

Powered by AI

Java allows developers to create user-defined exceptions by extending the `Exception` class. This makes exception handling straightforward by using the familiar try-catch paradigm. For example, define an `AgeException` that is thrown when an invalid age is encountered: `class AgeException extends Exception { AgeException(String message) { super(message); } }`. Throw this exception using `throw new AgeException("Age must be greater than zero.");` in validation logic. Catch this in a catch block to handle scenarios where invalid data might disrupt the flow .

JDBC in Java connects to databases to perform CRUD operations (Create, Read, Update, Delete). For a student database, use a DriverManager to establish a connection: `Connection con = DriverManager.getConnection(url, user, password)`. Use Statement or PreparedStatement to execute SQL commands. For creation and insertion, `PreparedStatement pstmt = con.prepareStatement("INSERT INTO students VALUES (?, ?, ?)");` sets parameters and executes updates. Reading uses `ResultSet rs = stmt.executeQuery("SELECT * FROM students");` to fetch and iterate results. Updating and deleting involve executing `UPDATE` and `DELETE` statements, respectively, and using executeUpdate() to apply changes. This process streamlines database interactions via a consistent, SQL-based approach .

Java allows multiple inheritance through interfaces, as classes can implement multiple interfaces. For example, consider an `ArrayStack` class that implements two interfaces, `Arr` with a `disp()` method and `Stack` with `push()` and `pop()` methods. An implementation might look like: `public class ArrayStack implements Arr, Stack { ... }`. This design allows `ArrayStack` to inherit the functionality specified by both interfaces without encountering the complexity of inheriting from multiple concrete classes, thereby adhering to the concept of multiple inheritance .

Java handles file operations using the java.io package, which includes classes like FileReader, FileWriter for reading and writing files. For reading, create a FileReader: `FileReader fr = new FileReader("filename.txt");`. To write, use FileWriter: `FileWriter fw = new FileWriter("filename.txt");`. Use BufferedReader for efficient reading: `BufferedReader br = new BufferedReader(fr);`. For example, copying data from one file to another involves reading content with BufferedReader and writing with BufferedWriter: `BufferedWriter bw = new BufferedWriter(fw); String line; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); }`. This setup allows flexible and efficient file manipulation .

The producer-consumer problem is solved in Java using inter-thread communication through the wait() and notify() methods. A shared resource, typically a buffer, is accessed by two threads: a producer, which adds data to the buffer, and a consumer, which removes data. Java's synchronized methods or blocks are used to ensure that the threads do not interfere with each other when modifying the buffer. The producer calls wait() when the buffer is full, and notify() is called by the consumer when it removes data from the buffer, thereby allowing the producer to resume work. Conversely, the consumer waits when the buffer is empty, and is notified once data is available .

A TreeMap in Java is a Red-Black tree-based implementation of the Map interface, where data is stored in sorted order of keys. To manage student data, create a TreeMap with roll numbers as keys and student names as values: `TreeMap<Integer, String> studentMap = new TreeMap<>();`. Add students using `studentMap.put(rollNumber, studentName);`. The TreeMap automatically sorts entries by roll numbers, allowing operations like retrieving the first or last entry using `firstEntry()` and `lastEntry()`, respectively .

Jagged arrays in Java are arrays of arrays with different column sizes. An example could involve creating a two-dimensional array where each row is a different length. For instance, `int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[2]; jaggedArray[1] = new int[3]; jaggedArray[2] = new int[1];` This allows each row in the array to hold a different number of elements, demonstrating how jagged arrays can accommodate non-uniform data structures .

Dynamic polymorphism in Java is achieved through dynamic method dispatch, which allows method calls to be resolved at runtime. This is done by using a superclass reference to refer to a subclass object. Method overriding is the basis for this mechanism. For example, suppose there is a superclass Person with a method `displayDetails()`, and subclasses Employee and Manager that override this method. At runtime, the overridden method of the actual object pointed to by the superclass reference is called, implementing dynamic method dispatch. This allows for flexible and reusable code structures .

A Java program can handle predefined exceptions using try-catch blocks by encasing the code that may throw exceptions within a try block and providing catch blocks to handle different exceptions. For example, handle an `ArithmeticException` and `ArrayIndexOutOfBoundsException` as follows: `try { int data = 100 / 0; } catch (ArithmeticException e) { System.out.println("Arithmetic exception caught: " + e); } try { int[] arr = new int[5]; System.out.println(arr[10]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array index out of bounds: " + e); }`. This allows the program to continue running after exceptions occur .

Single inheritance in Java can be demonstrated by creating a super class Account with attributes and methods common to all accounts, and a subclass SavingsAccount that extends Account. The 'super' keyword is used within SavingsAccount to access constructors, methods, and fields of the superclass. For instance, in the constructor of SavingsAccount, `super(balance);` would call the constructor of Account to initialize balance. This illustrates how derived classes can reuse and extend the functionality of their base classes .

You might also like