0% found this document useful (0 votes)
30 views

Java Assignment 2

Uploaded by

Aman
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)
30 views

Java Assignment 2

Uploaded by

Aman
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
You are on page 1/ 2

JAVA Assignments

Attach these assignments to the previous assignments


Handwritten with outputs
Last date-26/10/2024
Assignments -9 (any five)
1. Create a method that takes two integers and performs division. Use try-catch to handle
ArithmeticException (division by zero) and NumberFormatException (if inputs are not valid integers).
Provide meaningful messages for each exception.

2. Define a custom exception called InvalidAgeException. Write a method that throws “custom
exception called InvalidAgeException” exception if the provided age is less than 18. Use this method
in a program and handle the exception using a try-catch block.

3. File Creation: Write a program that creates a new text file named "testfile.txt" using the File class.
Check if the file was created successfully and print the appropriate message.

4. Writing to a File: Create a Java program that writes user input to a file named "output.txt". Use
FileWriter and BufferedWriter for writing, and ensure that the resources are closed properly.

5. Appending to a File: Modify your previous writing program to append text to "output.txt" instead of
overwriting it. Demonstrate this with a user input option to continue appending.

6. Default Methods in Interfaces: Write a program that defines an interface with a default method.
Create a class that implements the interface and overrides the default method. Show how the default
method can be used in the implementation.

Assignment -10 (any five)


1. Constructor Overloading: Write a class Rectangle that has two constructors: one that takes no
parameters and initializes length and width to 1, and another that takes parameters for length and
width. Include a method to calculate the area.

2. Multilevel Inheritance: Write a base class Vehicle, a derived class Car, and a further derived class
ElectricCar. Override a method in each class that displays the type of vehicle.

3. Method Overriding: Write a class Employee with a method display(). Create a subclass Manager that
overrides this method. Show the difference in behavior when invoking the method on objects of both
classes.

4. Final Keyword: Create a class FinalClass with a final method show(). Try to override this method in a
derived class and explain the outcome.

5. Using this Keyword: Write a class Book with a constructor that initializes the title and author. Use the
this keyword to differentiate between the class attributes and constructor parameters.

6. Using super Keyword: Create a base class Person and a derived class Student. Use the super keyword
in the derived class constructor to call the base class constructor and initialize its fields.

Assignment -11 (any five)


1. Static Variables in Inheritance: Create a base class Animal with a static variable count. In a derived
class Dog, increment this count each time a Dog object is created. Show the behavior of the static
variable across instances of both classes.
2. Run-Time Polymorphism: Implement a base class Animal with a method sound(). Create subclasses
Cat and Dog that override this method. Demonstrate run-time polymorphism by calling the method
on a base class reference.

3. Collection Operations: Write a program that creates a List of integers, adds five elements to it, and
prints the list. Then, convert the list to a Set to remove duplicates and display the unique elements.

4. Using Iterators: Write a Java program that creates an ArrayList of strings. Use an Iterator to traverse
the list and print each element. Show how to safely remove elements during iteration.

5. Enumeration Example: Create a Vector of integers and demonstrate the use of Enumeration to iterate
through the vector. Compare its functionality with that of an Iterator.

6. ArrayList Operations: Write a program that demonstrates adding, removing, and updating elements
in an ArrayList. Include methods to display the contents of the list before and after modifications.

Assignment -12 (any five)


1. LinkedList Usage: Write a program that demonstrates the creation of a LinkedList, adding elements
to the front and back, and removing elements from both ends. Print the list after each operation.

2. Basic HashMap Operations: Write a program that creates a HashMap to store student names as keys
and their grades as values. Implement methods to add, remove, and retrieve grades based on student
names.

3. Iterating Over a HashMap: Demonstrate how to iterate over a HashMap using forEach, entrySet(),
and keySet(). Print both keys and values in each case.

4. Synchronized Method: Write a program that simulates a bank account where multiple threads can
deposit and withdraw money. Ensure that the operations are synchronized to prevent race
conditions.

5. Thread Coordination: Create a program using wait() and notify() to coordinate two threads: one that
produces numbers and another that consumes them. Implement a simple producer-consumer
problem.

6. String Manipulation: Write a Java program that demonstrates various operations on strings, such as
concatenation, substring extraction, and string comparison. Include examples using the StringBuilder
class for efficient manipulation.

7. Regular Expressions: Create a program that uses the String package to validate email addresses using
regular expressions. Print whether each test email is valid or invalid.

You might also like