CORE - V Practical
CORE - V Practical
CORE - V Practical
Write a program in java to accept an positive integer number from the user and convert
that number into octal, binary and hexadecimal numbers.
import java.util.Scanner;
if (number < 0) {
System.out.println("Please enter a positive integer.");
} else {
String octal = Integer.toOctalString(number);
String binary = Integer.toBinaryString(number);
String hexadecimal = Integer.toHexString(number);
scanner.close();
}
}
3.Write a program to extend an interface onto another interface and implement all the
methods as per your choice.
// Parent interface
interface Shape {
void draw();
}
@Override
public void calculateArea() {
// Implement circle area calculation here
System.out.println("Calculating Circle Area");
}
@Override
public void calculatePerimeter() {
// Implement circle perimeter calculation here
System.out.println("Calculating Circle Perimeter");
}
}
class Rectangle implements ExtendedShape {
@Override
public void draw() {
System.out.println("Drawing Rectangle");
}
@Override
public void calculateArea() {
// Implement rectangle area calculation here
System.out.println("Calculating Rectangle Area");
}
@Override
public void calculatePerimeter() {
// Implement rectangle perimeter calculation here
System.out.println("Calculating Rectangle Perimeter");
}
}
// Displaying values
System.out.println("Autoboxing: int to Integer: " + wrapperInt);
System.out.println("Autoboxing: double to Double: " + wrapperDouble);
System.out.println("Unboxing: Integer to int: " + primitiveInt2);
System.out.println("Unboxing: Double to double: " + primitiveDouble2);
}
}
5.Write a program to generate a fibonacci series of numbers, where the limit will be
received from the user.
import java.util.Scanner;
scanner.close();
}
}
6.Write a program to demonstrate exception handling of arithmetic type exceptions with
multiple catch blocks
import java.util.Scanner;
try {
int result = numerator / denominator;
System.out.println("Result of division: " + result);
} catch (ArithmeticException e) {
if (e instanceof ArithmeticException) {
System.out.println("ArithmeticException caught: Division by zero or other arithmetic
error.");
}
} catch (Exception e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("This block will always execute.");
}
scanner.close();
}
}