0% found this document useful (0 votes)
21 views11 pages

CMP202 Assignment

Omox2

Uploaded by

Jesse Abidoye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

CMP202 Assignment

Omox2

Uploaded by

Jesse Abidoye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CMP 202 ASSIGNMENT

ABIDOYE JESSE OYEWALE


BHU/19/01/02/0029
ANSWERS
1. Method Overloading: Method overloading allows defining multiple
methods with the same name but differe1. Method Overloading: Method
overloading allows defining multiple methods with the same name but
different parameters in a class. In Python, it’s achieved through default
arguments and variable-length arguments.
2. Method Overriding: Method overriding is when a subclass provides a
specific implementation for a method already defined in its parent class,
allowing for polymorphism and customization in object-oriented
programming.
3. Nature of Event:
Error: An error typically refers to a severe and unrecoverable issue that occurs at a
system level. Errors often lead to the termination of the program or the system
itself. Examples include hardware failures or out-of-memory errors.
Exception: An exception, on the other hand, is a more specific and manageable
event that occurs within the program’s context. Exceptions can be caught and
handled by the program, allowing it to recover gracefully.
Handling Error:
Errors are typically not handled by the program but are left for the system or
environment to manage. They often result in program crashes or system failures.
Exception: Exceptions are meant to be handled by the program. Developers can
anticipate specific types of exceptions and implement error-handling mechanisms
to deal with them.
Examples of Exceptions:
NullPointerException: This occurs when a program attempts to access an object or
variable that doesn’t exist (null reference). For example, trying to call a method on
a null object.
IOException: This exception is thrown when an input or output operation (e.g., file
reading/writing) encounters an issue, such as a file not being found or permission
denied.
ArithmeticException: This exception occurs when an arithmetic operation is
performed with inappropriate operands, such as dividing by zero.
4. Public class EvenNumberCounter {
Public static int countEvenNumbers(int[] arr) {
Int count = 0; // Initialize a counter to keep track of even numbers

For (int num : arr) {


If (num % 2 == 0) {
Count++; // Increment the counter if the number is even
}
}

Return count; // Return the count of even numbers


}

Public static void main(String[] args) {


Int[] numbers = {6, 5, 6, 7, 5, 8, 3, 9, 3};
Int evenCount = countEvenNumbers(numbers);
System.out.println(“Number of even numbers: “ + evenCount);
}
}
5. Import java.util.*;

Public class DuplicateNumberFinder {


Public static List<Integer> findDuplicates(int[] arr) {
List<Integer> duplicates = new ArrayList<>();
Set<Integer> seenNumbers = new HashSet<>();

For (int num : arr) {


If (seenNumbers.contains(num)) {
Duplicates.add(num); // Add the duplicate number to the list
} else {
seenNumbers.add(num); // Add the number to the set if it’s not seen
before
}
}

Return duplicates;
}

Public static void main(String[] args) {


Int[] numbers = {6, 5, 6, 7, 5, 8, 3, 9, 3};
List<Integer> duplicateNumbers = findDuplicates(numbers);

If (!duplicateNumbers.isEmpty()) {
System.out.println(“Duplicate numbers in the array: “ + duplicateNumbers);
} else {
System.out.println(“No duplicate numbers found in the array.”);
}
}
}
6. Method Signature Must Match.
Access Modifier Rules.
No Increase in Exception Types.
Method in Subclass Must Be Marked as @Override (Optional).
The Overriding Method Should Not Reduce Visibility.
Return Type Covariance (For Java 5 and later).
Static Methods Cannot Be Overridden.
Constructors and Private Methods Cannot Be Overridden.
6.
Public class ArrayValueReplacement {
Public static void main(String[] args) {
Int[] arr = {6, 7, 8, 4, 9, 8, 5, 7, 5};
Int valueToReplace = 9;
Int replacementValue = 17;

// Find the index of the value 9 in the array


Int indexToReplace = -1;
For (int I = 0; I < arr.length; i++) {
If (arr[i] == valueToReplace) {
indexToReplace = I;
break;
}
}

// Check if the value was found before replacing


If (indexToReplace != -1) {
// Print the index of the value 9
System.out.println(“Index of value “ + valueToReplace + “: “ +
indexToReplace);

// Replace the value 9 with 17


Arr[indexToReplace] = replacementValue;

// Print the updated array


System.out.print(“Updated Array: “);
For (int num : arr) {
System.out.print(num + “ “);
}
} else {
System.out.println(“Value “ + valueToReplace + “ not found in the array.”);
}
}
}
7. An interface in programming is a blueprint for a class that defines a set of
abstract methods. It’s defined by declaring these methods without
implementations, specifying a contract that classes implementing the
interfacent parameters in a class. In Python, it's achieved through default
arguments and variable-length arguments.
Method Overriding: Method overriding is when a subclass provides a specific
implementation for a method already defined in its parent class, allowing for
polymorphism and customization in object-oriented programming.
2. Nature of Event:
Error: An error typically refers to a severe and unrecoverable issue that occurs at a
system level. Errors often lead to the termination of the program or the system
itself. Examples include hardware failures or out-of-memory errors.
Exception: An exception, on the other hand, is a more specific and manageable
event that occurs within the program's context. Exceptions can be caught and
handled by the program, allowing it to recover gracefully.
Handling:Error:
Errors are typically not handled by the program but are left for the system or
environment to manage. They often result in program crashes or system failures.
Exception: Exceptions are meant to be handled by the program. Developers can
anticipate specific types of exceptions and implement error-handling mechanisms
to deal with them.
Examples of Exceptions:
NullPointerException: This occurs when a program attempts to access an object or
variable that doesn't exist (null reference). For example, trying to call a method on
a null object.
IOException: This exception is thrown when an input or output operation (e.g., file
reading/writing) encounters an issue, such as a file not being found or permission
denied.
ArithmeticException: This exception occurs when an arithmetic operation is
performed with inappropriate operands, such as dividing by zero.
3. public class EvenNumberCounter {
public static int countEvenNumbers(int[] arr) {
int count = 0; // Initialize a counter to keep track of even numbers

for (int num : arr) {


if (num % 2 == 0) {
count++; // Increment the counter if the number is even
}
}

return count; // Return the count of even numbers


}

public static void main(String[] args) {


int[] numbers = {6, 5, 6, 7, 5, 8, 3, 9, 3};
int evenCount = countEvenNumbers(numbers);
System.out.println("Number of even numbers: " + evenCount);
}
}
4. import java.util.*;

public class DuplicateNumberFinder {


public static List<Integer> findDuplicates(int[] arr) {
List<Integer> duplicates = new ArrayList<>();
Set<Integer> seenNumbers = new HashSet<>();

for (int num : arr) {


if (seenNumbers.contains(num)) {
duplicates.add(num); // Add the duplicate number to the list
} else {
seenNumbers.add(num); // Add the number to the set if it's not seen
before
}
}

return duplicates;
}

public static void main(String[] args) {


int[] numbers = {6, 5, 6, 7, 5, 8, 3, 9, 3};
List<Integer> duplicateNumbers = findDuplicates(numbers);

if (!duplicateNumbers.isEmpty()) {
System.out.println("Duplicate numbers in the array: " + duplicateNumbers);
} else {
System.out.println("No duplicate numbers found in the array.");
}
}
}
5. Method Signature Must Match.
Access Modifier Rules.
No Increase in Exception Types.
Method in Subclass Must Be Marked as @Override (Optional).
The Overriding Method Should Not Reduce Visibility.
Return Type Covariance (For Java 5 and later).
Static Methods Cannot Be Overridden.
Constructors and Private Methods Cannot Be Overridden.
6.
public class ArrayValueReplacement {
public static void main(String[] args) {
int[] arr = {6, 7, 8, 4, 9, 8, 5, 7, 5};
int valueToReplace = 9;
int replacementValue = 17;

// Find the index of the value 9 in the array


int indexToReplace = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == valueToReplace) {
indexToReplace = i;
break;
}
}
// Check if the value was found before replacing
if (indexToReplace != -1) {
// Print the index of the value 9
System.out.println("Index of value " + valueToReplace + ": " +
indexToReplace);

// Replace the value 9 with 17


arr[indexToReplace] = replacementValue;

// Print the updated array


System.out.print("Updated Array: ");
for (int num : arr) {
System.out.print(num + " ");
}
} else {
System.out.println("Value " + valueToReplace + " not found in the array.");
}
}
}
8. An interface in programming is a blueprint for a class that defines a set of
abstract methods. It's defined by declaring these methods without
implementations, specifying a contract that classes implementing the
interface must follow. In Java, for example, you define an interface using the
interface keyword.
9. Size:
Array: Fixed size, cannot change after creation.ArrayList: Dynamically resizes
as elements are added or removed.Type:Array: Can store primitive data
types and objects.ArrayList: Can only store objects, not primitive types.
Efficiency:
Array: Generally more memory-efficient and faster for simple element
access.
ArrayList: Less memory-efficient and can be slower due to dynamic resizing.
Flexibility:
Array: Limited flexibility for size changes and manipulation.
ArrayList: Offers flexibility with dynamic resizing and built-in methods.
Syntax and Methods:
Array: Uses square brackets for element access, lacks built-in methods for
manipulation.
ArrayList: Uses methods like add(), remove(), and get() for easier element
manipulation.
Compatibility with Generics:
Array: Not ideal for generics and type safety.
ArrayList: Works well with generics for type safety.

You might also like