Moksha-CoreJava-PartA-CodingAssignments





Core Java Part ‘A’ Coding Assignments

 

Arrays:

1.       Assignment: Write a program that finds the maximum element in an integer array.

·         Algorithm:

·         Initialize a variable max to the first element of the array.

·         Iterate through the array, comparing each element with max.

·         If an element is greater than max, update max with that element.

·         After the loop, max will contain the maximum element.

2.       Assignment: Implement a program to reverse an array.

·         Algorithm:

·         Initialize two pointers, one at the beginning and one at the end of the array.

·         Swap the elements at the pointers and move them towards each other until they meet in the middle.

3.       Assignment: Write a function to check if an array contains duplicate elements.

·         Algorithm:

·         Use a hash set to keep track of unique elements while iterating through the array.

·         If you encounter an element that's already in the set, it's a duplicate.

4.       Assignment: Create a program to find the sum of all positive and negative numbers in an array separately.

·         Algorithm:

·         Initialize two variables, positiveSum and negativeSum to zero.

·         Iterate through the array and add positive numbers to positiveSum and negative numbers to negativeSum.

5.       Assignment: Implement a program to merge two sorted arrays into a single sorted array.

·         Algorithm:

·         Create a new array to store the merged result.

·         Use two pointers to iterate through the sorted arrays and merge them by comparing elements.

6.       Assignment: Write a program to find the second largest element in an array.

·         Algorithm:

·         Initialize two variables, max and secondMax, to negative infinity.

·         Iterate through the array and update max and secondMax based on element values.

7.       Assignment: Implement a program to rotate an array to the left by a given number of positions.

·         Algorithm:

·         Use a temporary array to store the first n elements.

·         Shift the remaining elements to the left.

·         Copy the temporary array back to the original array.

8.       Assignment: Create a program that finds the intersection of two arrays (elements common to both).

·         Algorithm:

·         Use two nested loops to compare elements from both arrays and add common elements to a new array.

9.       Assignment: Write a function to remove all occurrences of a specific element from an array.

·         Algorithm:

·         Iterate through the array and copy non-matching elements to a new array.

10.   Assignment: Implement a program to find the frequency of each element in an array.

·         Algorithm:

·         Use a hash map to store element-frequency pairs while iterating through the array.

Exception Handling:

1.       Assignment: Create a program that handles a division by zero exception.

·         Algorithm:

·         Use a try-catch block to catch a DivideByZeroException and handle it gracefully.

2.       Assignment: Write a program that reads an integer from the user but handles the case where the input is not a valid integer.

·         Algorithm:

·         Use a try-catch block to catch a NumberFormatException and prompt the user to enter a valid integer.

3.       Assignment: Implement a program that demonstrates a custom exception for an invalid input.

·         Algorithm:

·         Define a custom exception class, throw it when an invalid input is detected, and catch it in the program.

4.       Assignment: Create a program that reads a file but handles the case where the file does not exist.

·         Algorithm:

·         Use a try-catch block to catch a FileNotFoundException and display an appropriate error message.

5.       Assignment: Write a program that demonstrates multiple catch blocks for different types of exceptions.

·         Algorithm:

·         Use multiple catch blocks to catch different types of exceptions that may occur in the program.

6.       Assignment: Implement a program that opens a network connection but handles network-related exceptions.

·         Algorithm:

·         Use try-catch blocks to catch exceptions related to network operations, such as IOException.

7.       Assignment: Create a program that demonstrates the use of the finally block.

·         Algorithm:

·         Use a try-catch-finally block to ensure that a piece of code in the finally block always executes.

8.       Assignment: Write a program that demonstrates the use of the throw keyword to manually throw an exception.

·         Algorithm:

·         Use the throw keyword to throw a custom exception in a specific scenario.

9.       Assignment: Implement a program that handles exceptions in a multi-threaded environment.

·         Algorithm:

·         Create multiple threads that may throw exceptions and handle them gracefully.

10.   Assignment: Create a program that catches an exception in one method and propagates it to a higher-level method for further handling.

·         Algorithm:

·         Catch an exception in a lower-level method, and use throws or re-throw it to a higher-level method for centralized handling.

Access Specifiers:

1.       Assignment: Define a class with private data members and access them using public methods (getters and setters).

·         Algorithm:

·         Declare private data members in a class.

·         Create public getter and setter methods to access and modify the private data members.

2.       Assignment: Create a package with multiple classes and demonstrate the use of different access specifiers (public, protected, default, private) within the package.

·         Algorithm:

·         Create a package with multiple classes, and within those classes, use different access specifiers for methods and data members.

3.       Assignment: Write a program that demonstrates the concept of encapsulation by making data members private and providing controlled access.

·         Algorithm:

·         Define a class with private data members and public methods to set and get those members.

4.       Assignment: Implement a program that uses the protected access specifier to access members of a base class in a derived class.

·         Algorithm:

·         Create a base class with protected members and derive a class from it to access those protected members.

5.       Assignment: Define a class in one package and try to access its private members from another package. Demonstrate the use of access modifiers in this context.

·         Algorithm:

·         Create a class in one package with private members, then attempt to access those members from another package.

6.       Assignment: Write a program that demonstrates the use of the default access modifier (package-private) within a package.

·         Algorithm:

·         Create multiple classes within a package and use the default access modifier for some members, which allows them to be accessible only within the same package.

7.       Assignment: Create a program with a base class and a derived class in different packages. Demonstrate how the protected access specifier allows access to base class members in the derived class.

·         Algorithm:

·         Create a base class in one package with protected members and derive a class from it in another package to access those protected members.

8.       Assignment: Implement a program that showcases the use of the public access specifier for methods that can be accessed from anywhere.

·         Algorithm:

·         Define a class with public methods that can be accessed from any part of the code.

9.       Assignment: Create a class with private, protected, and public members. Write a program that accesses all three types of members from within the class itself.

·         Algorithm:

·         Define a class with members having different access specifiers, and demonstrate how they can be accessed from within the same class.

10.   Assignment: Write a program that demonstrates the concept of encapsulation by encapsulating data members and providing access through public methods.

·         Algorithm:

·         Define a class with private data members and public methods to access and modify those members, encapsulating the data.

Inheritance:

1.       Assignment: Create a base class Shape with methods to calculate area and perimeter. Derive classes Circle and Rectangle from it and calculate their areas and perimeters.

·         Algorithm:

·         Define a base class Shape with methods for area and perimeter.

·         Derive classes Circle and Rectangle and implement their area and perimeter calculation methods.

2.       Assignment: Implement a program with a base class Vehicle and derived classes Car and Bicycle. Demonstrate inheritance and method overriding.

·         Algorithm:

·         Define a base class Vehicle with common properties and methods.

·         Derive classes Car and Bicycle and override methods as needed.

3.       Assignment: Write a program that demonstrates multiple levels of inheritance. Create a base class, a derived class, and another class derived from the second class.

·         Algorithm:

·         Define a base class.

·         Derive a class from it.

·         Derive another class from the second class, creating a multiple-level inheritance hierarchy.

4.       Assignment: Create a program that uses the super keyword to call a constructor of the base class from a derived class.

·         Algorithm:

·         Define a base class constructor and a derived class constructor.

·         Use the super keyword in the derived class constructor to call the base class constructor.

5.       Assignment: Implement a program that demonstrates the use of method overloading in an inheritance hierarchy. Define a base class with overloaded methods and a derived class.

·         Algorithm:

·         Define a base class with overloaded methods.

·         Derive a class and override one or more of the overloaded methods.

6.       Assignment: Create a program that shows how constructors are inherited in an inheritance hierarchy.

·         Algorithm:

·         Define a base class with constructors.

·         Derive a class and observe how constructors are inherited and called.

7.       Assignment: Write a program that demonstrates the concept of method overriding in an inheritance hierarchy.

·         Algorithm:

·         Define a base class with a method.

·         Derive a class and override the method to provide a specialized implementation.

8.       Assignment: Implement a program that showcases the use of the instanceof operator to check the type of objects in an inheritance hierarchy.

·         Algorithm:

·         Create objects of different derived classes.

·         Use the instanceof operator to check their types.

9.       Assignment: Create a program that uses abstract classes and methods to model shapes. Define an abstract class Shape with abstract methods like calculateArea and calculatePerimeter, then derive concrete classes like Circle and Rectangle.

·         Algorithm:

·         Define an abstract class Shape with abstract methods.

·         Derive concrete classes like Circle and Rectangle and implement the abstract methods.

10.   Assignment: Write a program that demonstrates the concept of method overriding and polymorphism by creating an array of base class objects and calling overridden methods on derived class objects.

·         Algorithm:

·         Create an array of base class objects.

·         Populate it with derived class objects.

·         Call overridden methods on objects to demonstrate polymorphism.

Polymorphism:

1.       Assignment: Create a program that demonstrates compile-time polymorphism (method overloading) by defining multiple methods with the same name but different parameter lists.

·         Algorithm:

·         Define a class with multiple methods having the same name but different parameters.

2.       Assignment: Implement a program that showcases runtime polymorphism (method overriding) by creating a base class with a method and a derived class that overrides it.

·         Algorithm:

·         Define a base class with a method.

·         Derive a class and override the method.

3.       Assignment: Write a program that demonstrates polymorphism through interface implementation. Define an interface Drawable with a draw method and have multiple classes implement it.

·         Algorithm:

·         Define an interface Drawable with a draw method.

·         Implement the draw method in multiple classes.

4.       Assignment: Create a program that uses polymorphism to process a collection of objects with a common base class or interface.

·         Algorithm:

·         Define a base class or interface.

·         Create a collection of objects of different derived classes or implementations of the interface.

·         Use polymorphism to process and manipulate the objects.

5.       Assignment: Implement a program that demonstrates method overriding and the super keyword to call a method from the base class.

·         Algorithm:

·         Define a base class with a method.

·         Derive a class and override the method.

·         Use the super keyword in the derived class to call the base class method.

6.       Assignment: Write a program that uses polymorphism to perform operations on different geometric shapes (e.g., calculating areas).

·         Algorithm:

·         Define a base class or interface for shapes with methods like calculateArea.

·         Create classes for various geometric shapes and implement their area calculations.

·         Use polymorphism to calculate and display areas for different shapes.

7.       Assignment: Create a program that demonstrates polymorphism through method overloading in a class hierarchy.

·         Algorithm:

·         Define a base class with a method.

·         Derive multiple classes and overload the method in each derived class.

8.       Assignment: Implement a program that uses polymorphism to process a list of animals, each with different behavior (e.g., a cat meows, a dog barks).

·         Algorithm:

·         Create a base class Animal with a method like makeSound.

·         Derive classes like Cat and Dog and override the makeSound method to implement specific behavior.

·         Use polymorphism to process a list of animals and call their makeSound methods.

9.       Assignment: Write a program that demonstrates polymorphism through the use of abstract classes and methods.

·         Algorithm:

·         Define an abstract class with abstract methods.

·         Derive concrete classes and implement the abstract methods.

·         Use polymorphism to work with objects of these classes.

10.   Assignment: Create a program that uses polymorphism to calculate the total cost of items in a shopping cart, where each item has a different price calculation method.

·         Algorithm:

·         Define a base class Item with a method to calculate its price.

·         Derive classes for different types of items (e.g., Book, Electronics) and override the price calculation method.

·         Use polymorphism to calculate the total cost of items in a shopping cart.

Encapsulation:

1.       Assignment: Define a class with private data members and provide public methods to set and get their values (getters and setters).

·         Algorithm:

·         Declare private data members in a class.

·         Create public getter and setter methods to access and modify the private data members.

2.       Assignment: Create a program that demonstrates the concept of encapsulation by making data members private and providing controlled access.

·         Algorithm:

·         Define a class with private data members and public methods to set and get those members.

3.       Assignment: Implement a program that uses encapsulation to protect sensitive data in a class, such as a bank account balance.

·         Algorithm:

·         Define a class for a bank account with a private balance data member.

·         Provide public methods to deposit, withdraw, and get the balance while ensuring data integrity.

4.       Assignment: Write a program that encapsulates a student's information (name, age, roll number) and provides methods to access and modify this information.

·         Algorithm:

·         Define a class to encapsulate student information with private data members.

·         Create public methods to set and get the student's details.

5.       Assignment: Create a program that demonstrates encapsulation by defining a class for a mobile phone with private data members like IMEI number and providing methods to access and update these details.

·         Algorithm:

·         Define a class for a mobile phone with private data members like IMEI number.

·         Implement public methods to access and update these details.

6.       Assignment: Implement a program that showcases encapsulation by creating a class for a library book with private data members like book title and author, and public methods to access and update these details.

·         Algorithm:

·         Define a class for a library book with private data members.

·         Provide public methods to access and update book details while maintaining data integrity.

7.       Assignment: Write a program that demonstrates encapsulation by creating a class for a bank customer's personal information (name, address, phone number) with private data members and public methods.

·         Algorithm:

·         Define a class to encapsulate a bank customer's personal information with private data members.

·         Create public methods to access and modify these details securely.

8.       Assignment: Create a program that uses encapsulation to protect data related to a game character (e.g., health, score, level).

·         Algorithm:

·         Define a class for a game character with private data members like health, score, and level.

·         Provide public methods to access and update these attributes securely.

9.       Assignment: Implement a program that demonstrates encapsulation by defining a class for a vehicle (e.g., car) with private data members (e.g., speed) and methods to control and access these attributes.

·         Algorithm:

·         Define a class for a vehicle (e.g., car) with private data members (e.g., speed).

·         Create public methods to control and access these attributes safely.

10.   Assignment: Write a program that showcases encapsulation by creating a class for a person's contact information (e.g., name, email, phone number) with private data members and public methods for access.

·         Algorithm:

·         Define a class for a person's contact information with private data members.

·         Provide public methods to access and update this information securely.

 

Conditional Statements:

1.       Assignment: Create a program that determines whether a given number is positive, negative, or zero.

·         Algorithm:

·         Use an if-else statement to check if the number is positive, negative, or zero and display the result.

2.       Assignment: Write a program that determines if a year is a leap year or not (consider leap year rules).

·         Algorithm:

·         Use if-else statements to check if the year is divisible by 4, not divisible by 100, or divisible by 400 to determine if it's a leap year.

3.       Assignment: Implement a program that determines the largest of three numbers.

·         Algorithm:

·         Use nested if-else statements to compare the three numbers and find the largest.

4.       Assignment: Create a program that checks if a given character is a vowel or a consonant.

·         Algorithm:

·         Use an if-else statement to check if the character is one of the vowels (a, e, i, o, u) or not.

5.       Assignment: Write a program that determines the day of the week for a given date (considering Sunday as the first day of the week).

·         Algorithm:

·         Use a series of if-else statements and calculations to determine the day of the week based on the date.

6.       Assignment: Implement a program that calculates the grade of a student based on their exam score.

·         Algorithm:

·         Use if-else or a switch statement to assign a grade (A, B, C, D, F) based on the exam score.

7.       Assignment: Create a program that determines whether a given year is a century year (ending in 00) and if it's a leap year.

·         Algorithm:

·         Use if-else statements to check if the year is a century year and whether it's a leap year.

8.       Assignment: Write a program that checks if a given string is a palindrome (reads the same forwards and backwards).

·         Algorithm:

·         Use a loop and conditional statements to compare characters from the beginning and end of the string to determine if it's a palindrome.

9.       Assignment: Implement a program that calculates the factorial of a given number using conditional statements.

·         Algorithm:

·         Use a loop and conditional statements to calculate the factorial of the input number.

10.   Assignment: Create a program that determines the type of a triangle based on the lengths of its sides (equilateral, isosceles, or scalene).

·         Algorithm:

·         Use if-else statements to compare the lengths of the sides to determine the type of triangle.

Loops:

1.       Assignment: Write a program that prints the numbers from 1 to 10 using a for loop.

·         Algorithm:

·         Use a for loop to iterate from 1 to 10 and print the numbers.

2.       Assignment: Create a program that calculates the sum of all even numbers from 1 to 100 using a while loop.

·         Algorithm:

·         Use a while loop to iterate from 1 to 100 and add even numbers to a running sum.

3.       Assignment: Implement a program that prints the multiplication table of a given number using a for loop.

·         Algorithm:

·         Use a for loop to iterate from 1 to 10 and print the multiplication results.

4.       Assignment: Write a program that calculates the factorial of a given number using a do-while loop.

·         Algorithm:

·         Use a do-while loop to calculate the factorial of the input number.

5.       Assignment: Create a program that finds the sum of all prime numbers from 1 to 100 using a for loop.

·         Algorithm:

·         Use a for loop to iterate from 1 to 100 and check if each number is prime before adding it to the sum.

6.       Assignment: Implement a program that prints a pattern of stars in a pyramid shape using nested for loops.

·         Algorithm:

·         Use nested for loops to print a pattern of stars in a pyramid shape.

7.       Assignment: Write a program that calculates the Fibonacci series up to a given number using a while loop.

·         Algorithm:

·         Use a while loop to generate the Fibonacci series until reaching the given number.

8.       Assignment: Create a program that counts the number of digits in a given integer using a for loop.

·         Algorithm:

·         Use a for loop and division by 10 to count the number of digits in the integer.

9.       Assignment: Implement a program that finds the greatest common divisor (GCD) of two numbers using a do-while loop.

·         Algorithm:

·         Use a do-while loop to calculate the GCD of two numbers using the Euclidean algorithm.

10.   Assignment: Write a program that prints the first n terms of the geometric progression series using a for loop.

·         Algorithm:

·         Use a for loop to generate and print the first n terms of a geometric progression series.

Constructors:

1.       Assignment: Create a class Student with a parameterized constructor to initialize student details (name, roll number, marks).

·         Algorithm:

·         Define a class Student with a parameterized constructor to set student details during object creation.

2.       Assignment: Implement a program that demonstrates constructor overloading by defining multiple constructors in a class.

·         Algorithm:

·         Define a class with multiple constructors that accept different sets of parameters.

3.       Assignment: Write a program that creates an array of objects using a constructor with parameters to initialize the objects.

·         Algorithm:

·         Define a class with a parameterized constructor.

·         Create an array of objects, initializing them with different sets of parameters using the constructor.

4.       Assignment: Create a program that uses the default constructor to initialize objects of a class.

·         Algorithm:

·         Define a class with a default constructor that initializes default values.

5.       Assignment: Implement a program that demonstrates constructor chaining by calling one constructor from another within the same class.

·         Algorithm:

·         Define a class with multiple constructors.

·         Use constructor chaining to call one constructor from another within the class.

6.       Assignment: Write a program that uses constructors to create objects of a class and initializes their attributes.

·         Algorithm:

·         Define a class with a constructor that initializes object attributes.

·         Create objects of the class, passing values to the constructor during object creation.

7.       Assignment: Create a program that demonstrates the use of constructors to set default values for object attributes.

·         Algorithm:

·         Define a class with a constructor that sets default values for object attributes.

8.       Assignment: Implement a program that uses constructors to initialize objects of a class, including objects contained within other objects (composition).

·         Algorithm:

·         Define classes with constructors for objects that are part of a composition.

·         Create objects of the main class, initializing them along with contained objects.

9.       Assignment: Write a program that uses parameterized constructors to initialize objects with different values.

·         Algorithm:

·         Define a class with parameterized constructors that accept different values for object initialization.

·         Create objects with various sets of parameters.

10.   Assignment: Create a program that demonstrates the concept of copy constructors by initializing objects with the values of other objects.

·         Algorithm:

·         Define a copy constructor in a class to initialize an object with the values of another object.

·         Use the copy constructor to create objects based on existing objects.

 

Comments

Popular posts from this blog

FrontEnd - FAQs - Part 1

CoreJava - ClassesObjectsMethods - Assignment

Java Script - FAQs