0% found this document useful (0 votes)
35 views5 pages

CORE - V Practical

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

CORE - V Practical

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

1.

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;

public class NumberConversion {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();

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);

System.out.println("Octal: " + octal);


System.out.println("Binary: " + binary);
System.out.println("Hexadecimal: " + hexadecimal);
}

scanner.close();
}
}

2.Write a program in java to find prime numbers between 1 to 1000.


public class PrimeNumbers {
public static void main(String[] args) {
int limit = 1000;

System.out.println("Prime numbers between 1 and " + limit + " are:");

for (int i = 2; i <= limit; i++) {


if (isPrime(i)) {
System.out.print(i + " ");
}
}
}

// Function to check if a number is prime


public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}

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();
}

// Extended interface with additional methods


interface ExtendedShape extends Shape {
void calculateArea();
void calculatePerimeter();
}

// Implementation of the ExtendedShape interface


class Circle implements ExtendedShape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}

@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");
}
}

public class InterfaceExtensionExample {


public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
circle.calculateArea();
circle.calculatePerimeter();

Rectangle rectangle = new Rectangle();


rectangle.draw();
rectangle.calculateArea();
rectangle.calculatePerimeter();
}
}

4.Write a program to demonstrate autoboxing and unboxing.

public class AutoBoxingUnboxingExample {


public static void main(String[] args) {
// Autoboxing: converting primitive types to their corresponding wrapper classes
int primitiveInt = 10;
Integer wrapperInt = primitiveInt; // Autoboxing: int to Integer

double primitiveDouble = 3.14;


Double wrapperDouble = primitiveDouble; // Autoboxing: double to Double
// Unboxing: converting wrapper classes to their corresponding primitive types
Integer wrapperInt2 = 20;
int primitiveInt2 = wrapperInt2; // Unboxing: Integer to int

Double wrapperDouble2 = 6.28;


double primitiveDouble2 = wrapperDouble2; // Unboxing: Double to double

// 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;

public class FibonacciSeries {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the limit for the Fibonacci series: ");


int limit = scanner.nextInt();

System.out.println("Fibonacci series up to " + limit + " terms:");

int firstTerm = 0, secondTerm = 1;

for (int i = 1; i <= limit; ++i) {


System.out.print(firstTerm + " ");

int nextTerm = firstTerm + secondTerm;


firstTerm = secondTerm;
secondTerm = nextTerm;
}

scanner.close();
}
}
6.Write a program to demonstrate exception handling of arithmetic type exceptions with
multiple catch blocks

import java.util.Scanner;

public class ArithmeticExceptionHandling {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter two numbers:");


int numerator = scanner.nextInt();
int denominator = scanner.nextInt();

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();
}
}

You might also like