java
java
(FPI)
ASSIGNMENT
NAME: BANKOLE ADEBISI TOLULOPE
MATRIC NUMBER: PN/CS/23/1892
BY MR. AYODELE
1. Write short note on object and class
Class:A class is a formal description of a group of entities that share common attributes and methods. It acts
as a programming template for creating objects.
Purpose: A class defines what an object is, what attributes it will have, and how it will behave.
Logical Entity: A class is a logical entity that exists in the code but does not allocate memory space.
Example: Consider a class named Car. It defines properties like make, model, and methods
like startEngine() and drive().
Object:An object is an instance of a class. It represents a physical entity created based on the class
blueprint.
Purpose: Objects are used to store data and perform actions. They encapsulate both data (attributes) and
behavior (methods).
Physical Entity: When you create an object, memory is allocated for its data members.
Example: If Car is the class, an object could be an actual car instance with specific values
for make and model.
2. Write a java program that will print a in the index of java program
public class Main {
public static void main(String[] args) {
System.out.print("Index of a:");
String index="Java Program";
for(int a=0;a<index.length();a++){
if(index.charAt(a)=='a'){
System.out.print(a + " ");
}
}
}
}
}
class Mynewtread extends MyThread {
@Override
public void run() {
System.out.println("Thread is isAlive");
}
}
}
}
4. How will achieve code reusability in java and retain ability in java
1. Reusability in Java
1. Inheritance :You can extend a parent class to create a new class that inherits its properties
and behavior.
4. Composition :Instead of inheritance, using objects of other classes inside a class promotes
flexible reusability.
1. Encapsulation
o By hiding internal details and exposing only necessary methods, Java promotes
maintainable code.
o Use private variables and public getters/setters:
2. Modularity
Divide code into small, independent modules or classes.
This allows changes in one module without affecting others.
3. Consistent Coding Practices: Follow standard naming conventions and documentation for easier
readability and maintenance.
4. Exception Handling :Using proper try-catch blocks ensures that code handles errors gracefully
without breaking the entire system.
Characteristics:
o Fixed Size: The size is defined when created and cannot be changed.
o Efficient Access: Direct access to any element using its index.
Characteristics:
o Dynamic Size: Can grow or shrink as needed.
o Random Access: Provides fast access to elements.
o Non-Synchronized: Not thread-safe, but more efficient.
Characteristics:
o Dynamic Size: Like ArrayList, it grows or shrinks automatically.
o Synchronized: All methods are synchronized, making it thread-safe but slower.
o Legacy Class: Often replaced by ArrayList in modern Java development.
Full GC:
oTuning GC:
Developers can tune GC parameters based on application requirements.
Options include adjusting heap size, choosing GC algorithms, and setting thresholds.
G1 (Garbage-First) GC is a popular choice for balancing throughput and latency.
3. GC Limitations:
o No Guarantee Against Out of Memory (OOM):
GC does not guarantee that a program won’t run out of memory.
If an application keeps references to objects (even unintentionally), GC cannot
reclaim their memory.
Example: Infinite loops that keep adding objects to a list without releasing them.
o OutOfMemoryError (OOM):
When memory is exhausted, an OOM error occurs.
GC cannot prevent this if the application allocates more memory than available.
o Responsibility Lies with Developers:
Developers must manage object references properly.
Release unused objects explicitly (set references to null).
Avoid circular references that prevent GC.
Use tools (profilers) to identify memory leaks.
7. Write short note on Exception handling
EXCEPTION HANDLING
Exception handling is a mechanism in Java that allows you to handle runtime errors gracefully. In Java,
exceptions are objects that are thrown at runtime when an error occurs. Exception handling allows you to
catch these exceptions and take appropriate action.
publicclassExceptionHandlingExample {
publicstaticvoidmain(String[] args) {
try {
int[] arr = newint[5];
arr[10] = 50;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: " + e);
}
}
}
In this example, we are trying to access an array element that is out of bounds. This will cause an
ArrayIndexOutOfBoundsException to be thrown at runtime. To handle this exception, we have enclosed the
code that might throw the exception inside a try block. If an exception is thrown, it is caught by the catch
block, which prints a message to the console.
There are several other types of exceptions in Java, such as NullPointerException, ArithmeticException,
FileNotFoundException, etc. You can handle these exceptions in a similar way by enclosing the code that
might throw the exception inside a try block and catching the exception in a catch block.