java _merged
java _merged
Practical File
Submitted To Submitted By
Mrs. Lalita Name - Akash
Ma'am Section -B
Signature College Roll No. – 98
University Roll No. –
……………………… 221280400092
Session 2024-25
1
INDEX
s.no content Date Signature
1.
write a program on constructor overloading.
2.
Write a program on inheritance.
3.
write a program on interface.
4.
Write a program on polymorphism.
5.
Write a Program on exceptional handling.
7.
Write a Program to print the Fibonacci series
up to n terms.
8.
Write a Program to reverse a number.
9.
Write a Program to check if a number is a
palindrome.
10. Write a Program to find the sum of digits of a
number.
11.
Write a program to find the largest of three
numbers.
// ConstructorOverloadingExample.java
class Student {
String name;
int age;
// Constructor 1: No parameters
Student() {
name = "Unknown";
age = 0;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Output :
void sleep() {
System.out.println("This animal sleeps.");
}
}
// Main class
public class InheritanceExample {
public static void main(String[] args) {
Dog d = new Dog();
Output :
Explanation :
* Inheritance allows a class to use the properties and methods of another class.
* Dog class extends Animal, so it inherits the eat() and sleep() methods.
// Interface
interface Shape {
void draw(); // abstract method
double getArea(); // abstract method
}
// Implementing class
class Circle implements Shape {
double radius;
Circle(double r) {
radius = r;
}
// Main class
public class InterfaceExample {
public static void main(String[] args) {
Circle c = new Circle(5.0);
c.draw();
System.out.println("Area of Circle: " + c.getArea());
}
}
Output :
Drawing a Circle
Area of Circle: 78.5
Explanation :
* An interface in Java is like a blueprint—it only has abstract methods (no body).
* The Circle class implements the Shape interface and provides body for both methods.
* In main(), we create a Circle object, call draw(), and print the area.
Practical - 4
// Base class
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Derived class 1
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
// Derived class 2
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
// Main class
public class PolymorphismExample {
public static void main(String[] args) {
Animal a; // reference of base class
Output :
Dog barks
Cat meows
Expanation :
child objects (Dog, Cat), and the correct method is called based on the
import java.util.Scanner;
try {
System.out.print("Enter first number: ");
int a = sc.nextInt();
int result = a / b;
System.out.println("Result: " + result);
}
catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
}
catch (Exception e) {
System.out.println("Error: Invalid input.");
}
finally {
System.out.println("Program ended.");
}
sc.close();
}
}
Output :1
import java.util.Scanner;
sc.close();
}
}
Output :
Enter a number: 5
Factorial of 5 is: 120
Explanation :
* The program takes a number from the user and calculates its factorial using a for loop.
* It uses a long variable to store the factorial because the value can get large.
* It also handles the case where the number is negative by showing a proper message.
Practical - 7
import java.util.Scanner;
sc.close();
}
}
Output :
Explanation :
* The program asks for the number of terms (n) to display in the Fibonacci series.
* It then uses a for loop to calculate and print the terms in the series up to n.
Practical -8
import java.util.Scanner;
sc.close();
}
}
Output :
Explanation :
* The program takes a number from the user and reverses it using a while loop.
* In each iteration, the last digit of the number is obtained using the modulus
operator (%), and then the reversed number is built by appending this digit.
* The number is reduced by removing its last digit (num /= 10) in each loop iteration.
import java.util.Scanner;
sc.close();
}
}
Output :
Explanation :
* A palindrome number is a number that reads the same forward and backward, like 121 or 12321.
* The program first stores the original number (originalNum) to compare it later.
* Then, it reverses the number using the same approach as the reverse number program.
* Finally, it checks if the original number is equal to the reversed number. If they
import java.util.Scanner;
sc.close();
}
}
Output :
Explanation :
import java.util.Scanner;
sc.close();
}
}
Output :
Explanation :
* It then compares the second and third numbers with the current largest number and
import java.util.Scanner;
if (num <= 1) {
isPrime = false; // 0 and 1 are not prime
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false; // Divisible by i, not prime
break;
}
}
}
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
sc.close();
}
}
Output :
Enter a number: 17
17 is a prime number.
Explanation :
* A prime number is a number greater than 1 that has no divisors other than 1 and itself.
* The program handles edge cases such as 0 and 1 (which are not prime).