Java Internal1 Programs
Java Internal1 Programs
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;
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:
Byte :0
Short :0
Int :0
Long :0
Float :0.0
Double :0.0
Char :.
Boolean :false
SOURCE-CODE:
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
class A
System.out.println(l);
System.out.println(b);
class methoddemo
A a1=new A();
a1.display(10,20);
OUTPUT:
10
20
class A
{ int l=10,b=20;
int area()
return l*b;
class methoddemo
A a1=new A();
int r=a1.area();
class A
return l*b;
}
class methoddemo {
A a1=new A();
int r=a1.area(10,20);
3b)
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
A a1=new A();
int r1=a1.area();
A a2=new A(30,40);
int r2=a2.area();
4a)
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");
}
}
Output:
4b)
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);
}
}
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();
6a)
AIM: Write a JAVA program that describes exception handling
mechanism
SOURCE CODE:
public class ExceptionHandlingExample {
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 {
} 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: