Oops Practical Combination List
Oops Practical Combination List
I)
1) Creating and Using a Simple Class
Task 1: Creating the Person Class
● Create a Java class named Person.
● Add instance variables for firstName (String), lastName (String), and age (int).
● Create a default constructor to initialize these variables.
● Implement a parameterized constructor to set the values for firstName,lastName, and
age.
● Implement a method called getFullName that returns the full name(concatenation of
firstName and lastName).
Task 2: Using the Person Class
● In the main method, create two Person objects with the following data:
○ Person 1: First Name = "John", Last Name = "Doe", Age = 30
○ Person 2: First Name = "Alice", Last Name = "Smith", Age = 25
● Display the full names and ages of both persons using the getFullName method.
● Calculate and display the average age of the two persons.
Task 3: Additional Operations
● Implement a method in the Person class to check if a person is a teenager (agebetween
13 and 19).
● Create a new Person object with age 18 and use the method to check if they area
teen ager.
● Modify the Person class to include a toString method that returns a formattedstring
representation of a person's information.
● Display the formatted information of all created persons using the toStringmethod.
2) Debug the given program and find the output.
class A {
i;
int j;
void setij(int x, int y)
{
i = x;
j = y;
}
}
class B extend A {
int total;
void sum() {
total = i + j;
}
}
public class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total)
}
}
II)
1) Constructors and the this Keyword
Task 1: Creating the Product Class
● Create a Java class named Product to represent a product with attributes:
productId (int), productName (String), and price (double).
● Implement a parameterized constructor that initializes these attributes.
● Use the this keyword to distinguish between instance variables and constructor
parameters.
● Implement a method called displayProductInfo to display the productinformation (ID,
name, and price).
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) Debug the given program and find the output.
VI)
1) 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.
2) Debug the given program and find the output.
class A {
i;
int j;
void setij(int x, int y)
{
i = x;
j = y;
}
}
class B extend A {
int total;
void sum() {
total = i + j;
}
}
public class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total)
}
}
VII)
VIII)
class A {
i;
int j;
void setij(int x, int y)
{
i = x;
j = y;
}
}
class B extend A {
int total;
void sum() {
total = i + j;
}
}
public class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total)
}
}
X)
XI)
1) 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.