Java Assignment 2
Java Assignment 2
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.
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.
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.
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.