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

Complete Task List - Java

Practice Problems in java questions

Uploaded by

vinayagam b
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Complete Task List - Java

Practice Problems in java questions

Uploaded by

vinayagam b
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Complete Task List

Basic Operations
1. Input a natural number within 100. Print numbers 1 to 100 except the given input
number.
2. Sum of first n natural numbers with and without loop.
3. Find the third angle of a triangle if the other two angles are provided
4. Check whether the given string is a palindrome or not.
5. Input a character and print its ASCII value using typecasting.
6. Construct the triangle pattern
1
12
123
1234
Math functions
1. Calculate the square root of a number using Math.sqrt().
2. Raise a number to a power using Math.pow().
3. Compute sine, cosine, and tangent values for an angle in radians using
Math.sin(), Math.cos(), and Math.tan()
4. Find the absolute value of a number using Math.abs().
Encapsulation
Task1:Create a Student class with private fields: name, rollNumber, and
grade. Implement getters and setters for each field. Write a main program
to create a Student object and manipulate its data using the provided
methods.

Task2:Create a Car class with private fields: model, year, and price. Add
methods to update the price based on depreciation and display the car
details. Write a program to demonstrate the functionality.

Task3:Write a program to implement a LibraryBook class with private


fields: title, author, ISBN, and isAvailable. Create methods to check out and
return books, ensuring the availability status is updated correctly.
Constructor Overloading
Create a Vehicle class with three constructors:
Default constructor that initializes make to "Unknown" and model to
"Unknown".
Parameterized constructor that initializes make and model with given values.
Copy constructor that initializes a new Vehicle object using another Vehicle
object.
Add a method to display the vehicle details.
Inner Classes
Task1: Create a Car class with a non-static inner class Engine. The Engine class
should have a method to display engine specifications.
Task2: Implement a Calculator class with a static nested class Operations. Add
methods for basic operations (addition, subtraction, etc.) in the nested class.
Task3: Write a program with a local inner class in a method to calculate the factorial
of a number.
Task4: Create an interface Animal with a method speak(). Use an anonymous inner
class to implement the interface for a Dog and Cat.
Arrays
Create a program to reverse a 1D array.
Write a Java program to transpose a 2D array.
Find the largest and smallest elements in a 2D array.
Implement an object array to store students' marks in 3 subjects, name, rollno and
calculate the average.
Create a program to perform insert, update, delete, and search operations on a 1D
array.
ArrayList Task1/4: Basic Operations

Create an ArrayList of type String to store names of your favorite books.


Perform the following operations:
Add at least 5 book names.
Insert a book at index 2.
Remove the book at index 3.
Replace the book at index 1 with another book.
Print the final list of books.
ArrayList Task2/4: Integer ArrayList
Create an ArrayList<Integer> and add the numbers 10, 20, 30, 40, and 50.
Perform the following:
Remove the number 30 (by object).
Remove the element at index 1.
Add the number 60 at the end of the list.
Print the final list.
Sort the list in descending order.
ArrayList Task 3/4: Find max and min
Create an ArrayList<Integer> and add the numbers: 45, 12, 78, 34, 89, 23.
Write a program to:
Find the maximum and minimum values in the list.
Remove the maximum value from the list.
Print the updated list.
ArrayList Task 4/4: Merge two lists
Create two ArrayList<Integer> objects:

list1 with elements: 1, 3, 5, 7, 9.


list2 with elements: 2, 4, 6, 8, 10.
Merge both lists into a new ArrayList and print the combined list.
Sort the merged list in ascending order.
String Methods
Check whether a character is available in a string. And if available, display
its index and its number of occurrences.
Replace all occurrences of the first character with *
Eg: bubble --> *u**le
Replace all occurrences of the first character with * except the first
character
Eg: mammal --> ma**al
Streams
Read the content of a file. Replace a particular character of user choice
with *
Write a text file with a paragraph. Append the text file content with all the
alphabets capitalized
Write a Java program that reads data from an Excel or CSV file
Runtime Polymorphism - Task 1/4
Create a base class Customer with a method getDiscount() that returns 0%.
Create two subclasses:

RegularCustomer that overrides getDiscount() to return 5%.


PremiumCustomer that overrides getDiscount() to return 15%.
Use runtime polymorphism to calculate the final price after discount
Runtime Polymorphism - Task 2/4
Create a Student base class with methods study() and getFees().
Create two subclasses:

UndergraduateStudent and PostgraduateStudent.


Upcast objects of the subclasses to the Student type and call study().
Downcast back to specific student types to call subclass-specific methods, such
as submitThesis() for PostgraduateStudent
Runtime Polymorphism - Task 3/4
Create a Vehicle base class.
Override the finalize() method to display a message when the object is garbage
collected.
Create subclasses like Car and Bike.
Create and nullify instances of these classes, then trigger garbage collection
using System.gc().
Runtime Polymorphism - Task 4/4
Create a base class BankAccount with methods deposit() and withdraw().
Subclasses:

SavingsAccount (fixed minimum balance enforcement).


CurrentAccount (overdraft allowed).
Use a BankAccount reference to demonstrate polymorphic behavior.
Multiple Inheritance - Task 1/4:
Objective: Implement multiple behaviors for a Student class.
Interfaces:

Person: Contains getName() and getAge() methods.


ScholarshipEligible: Contains checkScholarshipEligibility() method.
FeePayable: Contains payFees() method.
Implementation:
Create a Student class that implements all three interfaces.
Add conditions for scholarship eligibility and fee payment.Example Methods:

checkScholarshipEligibility() !’ Based on grades.


payFees() !’ Print amount paid.
Multiple Inheritance - Task 2/4:
Objective: Implement multiple behaviors for a Student class.
Interfaces:

Person: Contains getName() and getAge() methods.


ScholarshipEligible: Contains checkScholarshipEligibility() method.
FeePayable: Contains payFees() method.
Implementation:
Create a Student class that implements all three interfaces.
Add conditions for scholarship eligibility and fee payment.Example Methods:

checkScholarshipEligibility() !’ Based on grades.


payFees() !’ Print amount paid.
Multiple Inheritance - Task 3/4:
Objective: Simulate an ATMmachine with different roles.
Interfaces:

ATMOperations: Contains methods for withdraw(), checkBalance(), and


deposit().
CardHolder: Contains validateCard() method.
Implementation:
Create an ATM class that implements both interfaces.
Add logic to validate card, perform transactions, and update balance.Example
Scenario:
Validate the card, allowwithdrawal/deposit, and display the updated balance.
Multiple Inheritance - Task 4/4:
Objective: Simulate a real-world bank customer performingvarious actions.
Interfaces:
BankServices: Contains methods for openAccount(), closeAccount().
ATMOperations: Contains withdraw() and checkBalance().
LoanProcessing: Contains applyLoan() and repayLoan().
Implementation:
Create a class BankCustomer that implements all three interfaces.
Simulate:
Opening and closing accounts.
Loan application and repayment.
ATMwithdrawals and balance checking.
Abstract Class - Task 1/2
Create an abstract class Employee with:
Twofields: name (String) and salary (double).
A concrete method displayInfo() that prints the name and salary.
Create two subclasses:
Manager: Adds a department field and overrides displayInfo() to include the
department name.
Engineer: Adds a specialization field and overrides displayInfo() to include the
specialization.
In the main method, create instances of both Manager and Engineer and call
their displayInfo() methods.
Abstract Class - Task 2/2
Create an abstract class Appliance with a constructor that initializes the appliance
name and power status (on/off).
Create a concrete method turnOn() that prints "Appliance is now on".
Create two subclasses:

WashingMachine: Inherits the constructor and method.


Refrigerator: Inherits the constructor and method.
In the main method, create instances of both WashingMachine and Refrigerator,
then call their turnOn() methods.
Exception Handling - Task 1/4
Objective: Write a program that divides two numbers. Use a try-catch block to
handle a possible ArithmeticException if division by zero occurs.
Steps:
Create two integervariables.
Perform division within a try block.
In the catch block, catch the ArithmeticException and print a custom error
message if division by zero happens.
Exception Handling - Task 2/4
Parse a list of strings into integers and handle:
NumberFormatException: If any string is not a valid number.
Handle the case where the list is empty or null.
Exception Handling - Task 3/4
Create a simple login system that validates user credentials. Handle:
NullPointerException: If the username or password is null.
Custom exception: Throw a custom InvalidCredentialsException if the
credentials are incorrect.
Exception Handling - Task 4/4
Objective: Write a program where one exception (FileNotFoundException) is caught
and another exception (IOException) is thrown, while preserving the original cause
of the exception.
Steps:
Create a program that attempts to open a file.
Catch FileNotFoundException and throw a new IOException with the original
exception as the cause.
In the catch block, print both the new exception message and the cause.
Multithreading - Task
1) Multiplication Table using multi threading.

2) Create a program to demonstrate race conditions and synchronization using a


bank account. Multiple threads will deposit and withdraw money simultaneously,
and you need to ensure the account balance is consistent.
Instructions:
Implement a BankAccount class with synchronized methods for deposit and
withdraw.
Create multiple threads performing deposits and withdrawals.
Observe the output to ensure no data inconsistency.
Collections - Task
Create a Java program that uses a HashMap to count the frequency of words
in a given text.
Steps:
Read a string input (this could be a sentence or paragraph).
Split the string into words.
Use a HashMap where the keys are the words, and the values are their
frequencies.
Print out each word and its frequency.
JDBC - Task
Create a table which stores the published messages from the connected IoT devices.
DeviceID, DeviceName, IPAddress, Time, Message, Retain(boolean)
Example:
T-001,DHT11, 192.168.1.4, '2023-12-18 10:30:45', {temp:18,humidity:54,status:'ON'},1

Write a Java program to perform CRUD operations in the device table

You might also like