OOPD Soved QP
OOPD Soved QP
>> Explanation:
1. Logical OR (||):
o a || b evaluates to true because at least one operand (a) is true.
o Output: a||b = true
2. Logical AND (&&):
o a && b evaluates to false because both operands must be true for the result to
be true, and b is false.
o Output: a&&b = false
3. Logical NOT (!):
o !a evaluates to false because a is true, and the NOT operator negates it.
o Output: a! = false
Overall Output:
css
Copy code
a||b = true
a&&b = false
a! = false
-------------------------------------------------------------------------------------------------------
class BankAccount {
private double balance;
try {
System.out.println("Current Balance: " + account.getBalance());
// Deposit money
account.deposit(500.0);
System.out.println("Balance after deposit: " + account.getBalance());
// Withdraw money
account.withdraw(300.0);
System.out.println("Balance after withdrawal: " + account.getBalance());
>>import java.io.*;
String line;
// Read each line from input file and write to output file
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine(); // Add a new line after each line copied
}
Explanation
1. File Handling:
o The program uses File, BufferedReader, and BufferedWriter to manage
reading from and writing to the files.
2. Try-with-Resources:
o This feature automatically closes the BufferedReader and BufferedWriter at
the end of the block, ensuring resources are released properly.
3. Reading and Writing:
o The program reads each line from input.txt using readLine() and writes it to
output.txt. After writing each line, it adds a new line using newLine().
4. Exception Handling:
o FileNotFoundException: This exception is caught if input.txt does not exist in
the specified directory.
o IOException: This catches any input/output errors that may occur while
reading from or writing to the files.
----------------------------------------------------------------------------------------------------------------
6. Write a JAVA program to create your own exception (User Exception) that displays
the error message “ Invalid Input” to find the area of the circle when the radius is given
as either 0 or a negative value
>> // Custom Exception Class
class InvalidInputException extends Exception {
public InvalidInputException(String message) {
super(message);
}
}
try {
double area = calculateArea(radius);
System.out.printf("The area of the circle with radius %.2f is: %.2f%n", radius, area);
} catch (InvalidInputException e) {
System.err.println(e.getMessage());
}
}
}
Explanation
XXXXXXX