Nothing Special   »   [go: up one dir, main page]

Java Internal1 Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

1a) Write a JAVA program to display default value of all primitive

data type of JAVA

Source code:

class defaultdemo

static byte b;

static short s;

static int i;

static long l;

static float f;

static double d;

static char c;

static boolean bl;

public static void main(String[] args)

System.out.println("The default values of primitive data types are:");

System.out.println("Byte :"+b);

System.out.println("Short :"+s);

System.out.println("Int :"+i);

System.out.println("Long :"+l);

System.out.println("Float :"+f);

System.out.println("Double :"+d);

System.out.println("Char :"+c);

System.out.println("Boolean :"+bl);

}
OUTPUT:

The default values of primitive data types are:

Byte :0

Short :0

Int :0

Long :0

Float :0.0

Double :0.0

Char :.

Boolean :false

3a) Write a JAVA program to implement class mechanism. Create a class,


methods and invokethem inside main method.

SOURCE-CODE:

1.no return type and without parameter-list:

class A

int l=10,b=20;

void display()

System.out.println(l);

System.out.println(b);

class methoddemo

{
public static void main(String args[])

A a1=new A();

a1.display();

OUT-PUT:

10

20

2.no return type and with parameter-list:

class A

void display(int l,int b)

System.out.println(l);

System.out.println(b);

class methoddemo

public static void main(String args[])

A a1=new A();

a1.display(10,20);

OUTPUT:
10

20

3. return type and without parameter-list :

class A

{ int l=10,b=20;

int area()

return l*b;

class methoddemo

public static void main(String args[])

A a1=new A();

int r=a1.area();

System.out.println("The area is: "+r);

OUT-PUT: The area is:200

4.return type and with parameter-list:

class A

int area(int l,int b)

return l*b;
}

class methoddemo {

public static void main(String args[])

A a1=new A();

int r=a1.area(10,20);

System.out.println(“The area is:”+r);

OUT-PUT: The area is:200

3b)

AIM: To write a JAVA program to implement constructor overloading

SOURCE-CODE:

class A

int l,b;

A()

l=10;b=20;

A(int u,int v)

l=u;b=v;

int area()

{
return l*b;

class overconstructdemo

public static void main(String args[])

A a1=new A();

int r1=a1.area();

System.out.println("The area is: "+r1);

A a2=new A(30,40);

int r2=a2.area();

System.out.println("The area is: "+r2);

OUT-PUT: The area is: 200

The area is: 1200

4a)

AIM: Write a JAVA program to implement Single Inheritance

SOURCE-CODE:

// Superclass
class Animal {
// Method in the superclass
void sound() {
System.out.println("Animals make sound");
}
}
// Subclass (Single inheritance: Dog inherits from Animal)
class Dog extends Animal {
// Method in the subclass
void bark() {
System.out.println("Dog barks");
}
}

public class SingleInheritanceExample {


public static void main(String[] args) {
// Create an object of the subclass
Dog dog = new Dog();

// Call methods from both the superclass and the


subclass
dog.sound(); // Inherited from the Animal class
dog.bark(); // Method from the Dog class
}
}

Output:

Animals make sound


Dog barks

4b)

AIM:Write a JAVA program to implement multi level Inheritance

SOURCE CODE:

// Base class
class Animal {
String name;

// Constructor
Animal(String name) {
this.name = name;
}
// Method to display the animal's name
void display() {
System.out.println("Name: " + name);
}
}

// Intermediate class (inherits from Animal)


class Mammal extends Animal {
// Constructor
Mammal(String name) {
super(name); // Call the Animal constructor
}

// Method to display mammal characteristics


void breathe() {
System.out.println(name + " breathes air.");
}
}

// Derived class (inherits from Mammal)


class Dog extends Mammal {
// Constructor
Dog(String name) {
super(name); // Call the Mammal constructor
}

// Method to display dog's behavior


void bark() {
System.out.println(name + " barks.");
}
}

public class SmallMultiLevelExample {


public static void main(String[] args) {
// Create a Dog object
Dog dog = new Dog("Buddy");

// Call methods from all levels of inheritance


dog.display(); // Inherited from Animal class
dog.breathe(); // Inherited from Mammal class
dog.bark(); // Method in Dog class
}
}

Output:

Name: Buddy
Buddy breathes air.
Buddy barks.

5a)
AIM: Write a JAVA program give example for “super” keyword
SOURCE CODE:
class Vehicle {
int maxSpeed = 120;
} // Subclass Car extending Vehicle class Car extends Vehicle
{
int maxSpeed = 180;
void display()
{
// Print maxSpeed of base class (Vehicle) using super keyword
System.out.println("Maximum Speed of Vehicle: " +
super.maxSpeed);
}
} // Driver Program
class Test {
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}

OUTPUT:
Maximum Speed of Vehicle: 120

5b)
AIM:Write a JAVA program to implement Interface. What kind of
Inheritance can be achieved?
SOURCE CODE:
// Define an interface
interface Animal {
// Abstract method (does not have a body)
void makeSound();

// Default method (has a body)


default void eat() {
System.out.println("This animal is eating.");
}
}

// Implement the interface in a class


class Dog implements Animal {
// Provide implementation for the abstract method
public void makeSound() {
System.out.println("The dog barks.");
}

// You can also override the default method if needed


public void eat() {
System.out.println("The dog is eating dog food.");
}
}

public class InterfaceExample {


public static void main(String[] args) {
// Create an instance of Dog
Dog dog = new Dog();

// Call methods from the Animal interface


dog.makeSound(); // Implemented in Dog class
dog.eat(); // Overridden in Dog class
}
}
Output:

The dog barks.


The dog is eating dog food.

6a)
AIM: Write a JAVA program that describes exception handling
mechanism
SOURCE CODE:
public class ExceptionHandlingExample {

// Method that may throw an exception


static void divideNumbers(int a, int b) {
try {
// Code that may throw an exception
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
// Handle the exception
System.out.println("Error: Division by zero is not
allowed.");
} finally {
// This block will always execute
System.out.println("Execution of finally block.");
}
}

public static void main(String[] args) {


// Call method with valid inputs
divideNumbers(10, 2);

// Call method with invalid input to demonstrate


exception handling
divideNumbers(10, 0);
}
}
Output:

Result: 5
Execution of finally block.
Error: Division by zero is not allowed.
Execution of finally block.

6b)
AIM: Write a JAVA program Illustrating Multiple catch clauses
SOURCE CODE:
public class MultipleCatchExample {

public static void main(String[] args) {


try {
// This block of code may throw different types of
exceptions
int[] numbers = {1, 2, 3};
int result = numbers[3]; // This will cause
ArrayIndexOutOfBoundsException

// The following line will not execute due to the


exception above
int division = 10 / 0; // This will cause
ArithmeticException

} catch (ArrayIndexOutOfBoundsException e) {
// Handle ArrayIndexOutOfBoundsException
System.out.println("Error: Array index is out of
bounds.");
} catch (ArithmeticException e) {
// Handle ArithmeticException
System.out.println("Error: Division by zero is not
allowed.");
} catch (Exception e) {
// Handle any other exception
System.out.println("An unexpected error occurred: "
+ e.getMessage());
} finally {
// This block will always execute
System.out.println("Execution of finally block.");
}
}
}

Output:

Error: Array index is out of bounds.


Execution of finally block.

You might also like