Oops Program List
Oops Program List
1
Task 2: Using the Product Class
● In the main method, create three instances of the Product class with thefollowing data:
○ Product 1: ID = 101, Name = "Laptop", Price = 999.99
○ Product 2: ID = 202, Name = "Smartphone", Price = 499.95
○ Product 3: ID = 303, Name = "Tablet", Price = 299.50
● Display the information of all three products using the displayProductInfomethod.
Task 3: Additional Operations
● Implement a method in the Product class to calculate and return the discountedprice of
a product. Allow specifying a discount percentage.
● Create a new Product object and calculate and display the discounted price witha 10%
discount.
● Modify the Product class to include a static variable for tracking the total numberof
products created. Implement a static method to retrieve this count.
● Display the total number of products created in the main method.
2
4) Overloading Constructors and Returning Object
Task 1: Creating the Product Class
● Create a Java class named "Product."
● Add instance variables for productId (int), productName (String), and price(double).
● Implement multiple constructors for initializing these variables.
Task 2: Using the Product Class
● In the main method, create two Product objects using different constructors.
● Display the details (id, name, price) of both products.
Task 3: Additional Operations
● Implement a method in the Product class that calculates the total cost by
multiplying the price with a given quantity.
● Create a new Product object and calculate the total cost for a quantity of 5 units.
● Display the total cost of the product.
5) Introducing Nested and Inner Classes
Task 1: Creating the University Class
● Develop a Java class named "University" with an inner class "Department."
● The University class should have attributes such as universityName (String) and
yearFounded (int).
● The Department inner class should include attributes like departmentName(String) and
numberStudents (int).
Task 2: Using Nested and Inner Classes
● Instantiate the University class and create instances of multiple Departmentinner
classes within it.
● Display the details of the university and each department created.
Task 3: Additional Operations
● Implement a method in the University class to calculate the total number ofstudents
across all departments.
● Use this method to display the overall student count in the university.
6) Exploring the String Class and Command-Line Arguments
Task 1: String Manipulation
● Develop a Java program to manipulate strings using various methods from theString
class.
● Include operations such as concatenation, substring extraction, and character
replacement.
Task 2: Command-Line Argument Processing
● Create a program that takes command-line arguments representingmathematical
operations (e.g., add, subtract, multiply) and correspondingoperands.
● Perform the specified operation on the provided operands and display the result.
Task 3: Additional Operations
● Extend the command-line argument program to handle different data types for
operands (integers, doubles).
● Ensure robust error handling for incorrect inputs and display appropriate error
messages.
3
7) Inheritance Basics and Method Overriding
Task 1: Member Access and Inheritance
● Create a superclass "Vehicle" with attributes like make (String), model (String),and
Year(int).
● Develop a subclass "Car" inheriting from "Vehicle" with an additional attribute,color
(String).
● Implement methods in both classes for displaying details.
Task 2: Using super and Method Overriding
● Use "super" to call the superclass constructor from the subclass "Car".
● Override the display method in "Car" to include color information.
Task 3: Dynamic Method Dispatch and Object Class
● Demonstrate dynamic method dispatch by storing "Car" objects in an array of
"Vehicle".
● Utilize the overridden method to display details, showcasing runtimepolymorphism.
4
10) Packages and Access Protection
Task 1: Defining a Package
● Create a package named "utilities".
● Define a class "Calculator" within the "utilities" package with basic
Arithmetic operations (addition, subtraction, multiplication, division).
Task 2: Access Protection and Importing Packages
● Develop another package named "app" outside the "utilities" package.
● Create a class "MainApp" in the "app" package to utilize the Calculator class fromthe
"utilities" package.
● Use different access modifiers (public, private, default) within this setup Andexplore
their effects.
Task 3: CLASSPATH and Packages
● Discuss and demonstrate the concept of the CLASSPATH in relation to packages.
● Execute the MainApp class, highlighting how the CLASSPATH helps in Locating the
classes from different packages.
11) Interfaces and Default Interface Methods
Task 1: Defining and Implementing Interfaces
● Create an interface "Shape" defining methods for calculating area and perimeter.
● Implement the "Shape" interface in classes like "Rectangle" and "Circle"
Providing area and perimeter calculations for each shape.
Task 2: Applying Interfaces and Default Methods
● Introduce another interface "Resizable" with a default method for resizingshapes.
● Implement this interface in the "Rectangle" class and demonstrate the useof the default
method to resize a rectangle.
Task 3: Multiple Interface Implementation
● Implement the "Shape" and "Resizable" interfaces in another class "CustomShape".
● Discuss and showcase how a class can implement multiple interfaces, Utilizingmethods
from both interfaces.
12) Nested Interfaces and CLASSPATH
Task 1: Nested Interfaces and Variables in Interfaces
● Define an interface "Outer" containing a nested interface "Inner" with Methodsand
variables within both interfaces.
● Implement the "Inner" interface in a class and use the interface variables in the
implementing class.
Task 2: Using Static Methods in Interfaces
● Extend the "Outer" interface with a static method.
● Demonstrate the usage of this static method without implementing the interface,
directly accessing it through the interface name.
Task 3: Applying Nested Interfaces
● Create another class "NestedInterfaceClass" implementing the "Outer.Inner"interface.
● Discuss the syntax and implications of implementing a nested interface Outsideits
enclosing interface and utilizing its functionality.
5
13) Exception Handling Fundamentals
Task 1: Exception Types and Using try-catch
● Introduce a scenario where an ArithmeticException might occur (e.g., dividing by
zero).
● Use try-catch blocks to handle the ArithmeticException gracefully and display an
error message.
Task 2: Multiple catch Clauses and Nested try Statements
● Create a situation involving a FileNotFoundException (e.g., attemptingto access anon-
existent file).
● Implement multiple catch clauses to handle different exceptions, including
IOExceptionand FileNotFoundException.
● Utilize nested try statements to demonstrate handling exceptions within deeper
Code blocks.
Task 3: Finally and Uncaught Exceptions
● Illustrate a scenario where a NullPointerException might occur (e.g., accessing a
method of a null object).
● Utilize a finally block to execute essential cleanup or finalization code.
● Discuss uncaught exceptions and their impact on the program's flow, demonstrating
an uncaught exception.
6
15) Java’s Built-in Exceptions and Advanced Exception Handling
Task 1: Java’s Built-in Exceptions
● Explore the IndexOutOfBoundsException by attempting to access an index beyond
the size of an array.
● Handle this exception using try-catch and display a meaningful error message.
Task 2: Recently Added Exception Features
● Demonstrate the use of try-with-resources to automatically close resources, such as
file streams or database connections.
● Utilize the improved handling of multi-catch exceptions introduced in Java 7 or later.
Task 3: Using Exceptions in Practice
● Create a scenario where an IllegalArgumentException might occur (e.g.,
Invalidargument passed to a method).
● Implement a method that validates the input and throws IllegalArgumentException
If invalid.
● Handle this exception effectively by providing guidance on correct inputs.
7
17) Debug the given program and find the output.
public class GFG {
static void swapValuesUsingThirdVariable(int m, int n)
{
temp = m;
m = n;
n = temp;
System.out.println("Value of m is " + m + " and Value of n is " + n)
}
8
20) Debug the given program and find the output.