1. Implement a Java program to demonstrate multilevel inheritance.
class A
{
public void show1()
{
[Link]("Good");
} OUTPUT:-
}
class B extends A Good
{ Morning
public void show2() Student
{
[Link]("Morning");
}
}
class C extends B
{
public void show3()
{
[Link]("Student");
}
}
public class Multilevel
{
public static void main(String[] args)
{
C obj = new C();
obj.show1();
obj.show2();
obj.show3();
}
}
2. Write a Program to print GCD of two numbers .
public class GCD
{
public static void main(String[] args)
{
int num1 = 4; OUTPUT:-
int num2 = 2;
int gcd = 1; GCD of 4 and 2 is: 2
for (int i = 1; i <= num1 && i <= num2; i++)
{
if (num1 % i == 0 && num2 % i == 0)
{
gcd = i;
}
}
[Link]("GCD of " + num1 + " and " + num2 + " is: " + gcd);
}
}
3. Write a Java program to print numbers from 1 to 10 using a do-while loop.
public class Print_Number OUTPUT:-
{
public static void main(String[] args) 1
{ 2
int i = 1; 3
do 4
{ 5
[Link](i); 6
i++; 7
}
8
while (i <= 10);
9
}
} 10
4. Write a Java program to handle a divide-by-zero exception.
public class DivideByZero
{
public static void main(String[] args)
{
int a = 10; OUTPUT:-
int b = 0;
int result; Error: Cannot divide by zero.
try
Program continues after exception handling.
{
result = a / b;
[Link]("Result: " + result);
}
catch (Exception e)
{
[Link]("Error: Cannot divide by zero.");
}
[Link]("Program continues after exception handling.");
}
}
5. Write a program to print Fibonacci Series.
public class Fibonacci
{
public static void main(String[] args)
{
int n = 5;
int first = 0, second = 1;
[Link]("Fibonacci Series up to " + n + " terms:");
for (int i = 1; i <= n; i++)
{
[Link](first + " ");
int next = first + second;
first = second;
second = next;
}
}
}
6. Write a Java program to demonstrate type casting.
public class TypeCasting
{
public static void main(String[] args) OUTPUT:-
{
int x = 10; Widening: 10 -> 10.0
double y = x; Narrowing: 9.7 -> 9
[Link]("Widening: " + x + " -> " + y);
double a = 9.7;
int b = (int) a;
[Link]("Narrowing: " + a + " -> " + b);
}
}
7. Write a program to print factorial of a number.
public class Factorial
{
OUTPUT:-
public static void main(String[] args)
{ Factorial of 5 is: 120
int num = 5;
long factorial = 1;
for (int i = 1; i <= num; i++)
{
factorial *= i;
}
[Link]("Factorial of " + num + " is " + factorial);
}
}
8. Write a Java program to create a parameterized constructor.
class Student
{
String name;
int age;
Student(String n, int a) OUTPUT:-
{
name = n; Name: Ram
age = a; Age: 20
}
void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
public class Constructor
{
public static void main(String[] args)
{
Student s1 = new Student("Ram", 20);
[Link]();
}
}
9. Write a Java program to demonstrate method overriding.
class A
{
void show()
{
[Link]("Hello"); OUTPUT:-
}
} World
class B extends A
{
void show()
{
[Link]("World");
}
}
class Inherit
{
public static void main(String args[])
{
B obj = new B();
[Link]();
}
}
10. Write a Java program to demonstrate constructor overloading using a Student class.
class Student
{
Student()
{
[Link]("No info given");
} OUTPUT:-
Student(String name)
{ No info given
[Link]("Name: " + name); Name: Ram
} Name: Amit
Roll: 101
Student(String name, int roll)
{
[Link]("Name: " + name);
[Link]("Roll: " + roll);
}
}
class Test
{
public static void main(String args[])
{
Student s1 = new Student();
Student s2 = new Student("Ram");
Student s3 = new Student("Amit", 101);
}
}