WORLD COLLEGE OF TECHNOLOGY &
MANAGEMENT, GURUGRAM
JAVA Practical File
Session: 2023-2027
Submitted By: Submitted to:
Name: SHANU RAJ Mr. Anil Kumar
Course: [Link] (Assistant Professor)
Branch: AI&ML WCTM
Semester: 5th
INDEX
[Link] Title Remarks
1. Write a program to print “Hello World!”
2. Write a program to add two integers
3. Write a Program to multiply two floating numbers
4. Write a program to find ASCII values of characters
5. Write a program to swap two numbers
6. Write a program to check whether a number is even or odd
7. Write a program to check whether a letter is vowel or consonant
8. Write a program to check whether the year is a leap year or not
9. Write a program to create a simple calculator program to perform addition,
subtraction, division and multiplication operations
10. Write a program to check whether a person is eligible to vote or not
11. Write a program to find the greatest among three numbers
12. Write a program to find percentage of five subjects
13. Write a program to print table of any given number
14. Write a program to find the factorial of a number
15. Write a program to display Fibonacci series
16. Write a program to perform multiplication of matrices
17. Write a program to find the area of rectangle using classes and objects
18. Write a program to show the use of “this” keyword
19. Write a program to implement Single Inheritance
20. Write a program to implement Multilevel Inheritance
21. Write a program to implement abstract class
22. Write a program to implement Method Overriding
23. Write a program to implement Method Overloading
24. Write a program to implement Polymorphism
25. Write a program to implement Interfaces
26. Write a program to implement multiple inheritance using Interface
27. Write a program to implement Exception Handling
28. Write a program to reverse a string
29. Write a program to concatenate two strings
30. Write a program to check if a string is a palindrome or not
1. WAP to print Hello world?
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, world!");
}
}
Output:
2. WAP to add two integers?
public class AddTwoNumbers {
public static void main(String[] args)
{ int num1 = 10; int num2 =
20; int sum = num1 + num2;
[Link]("The sum is: " + sum);
}
}
Output:
3. WAP to multiply two floating point numbers?
public class MultiplyFloats { public static void
main(String[] args) { double num1 = 5.5;
double num2 = 10.2; double product = num1 *
num2;
[Link]("The product is: " +
product); }
}
Output:
4. WAP to find ascii value of character?
public class FindAscii { public static void
main(String[] args) {
char ch = 'A'; int
asciiValue = ch; int
castAscii = (int) ch;
[Link]("The ASCII value of " + ch + " is: "
+ asciiValue);
[Link]("The ASCII value of " + ch + "
(using casting) is: " + castAscii);
}
}
Output:
5. WAP to swap two numbers?
public class SwapNumbers { public static
void main(String[] args) { int num1
= 10; int num2 = 20;
[Link]("Before swap: num1 = " + num1 + ", num2 = "
+ num2);
int temp = num1; num1 = num2; num2 = temp;
[Link]("After swap: num1 = " + num1 + ", num2 = "
+ num2);
}
}
Output:
6. WAP to check whether a number is odd or even?
public class OddOrEven { public static void
main(String[] args) { int num = 29;
[Link]("The number is: " + num);
if (num % 2 == 0) {
[Link](num + " is an even number.");
}
else {
[Link](num + " is an odd number.");
}
}
}
Output:
7. WAP to check whether an alphabet is a vowel or consonant?
public class VowelConsonant { public
static void main(String[] args)
{ char ch = 'E';
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
[Link](ch + " is a
vowel."); break; default:
[Link](ch + " is a consonant.");
}
}
}
Output:
8. WAP to check a leap year? public class LeapYearCheck {
public static void main(String[] args) { int year =
2024;
[Link]("Checking year: " + year); if ((year %
4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
[Link](year + " is a leap year.");
} else {
[Link](year + " is not a leap year.");
Output:
9. WAP to simple calculator program to perform addition,
subtraction, multiplication and multiplication?
public class SimpleCalculator { public
static void main(String[] args) {
double num1 = 100.0;
double num2 = 25.0;
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double quotient = num1 / num2;
[Link](num1 + " + " + num2 + " = " + sum);
[Link](num1 + " - " + num2 + " = " + difference);
[Link](num1 + " * " + num2 + " = " + product);
[Link](num1 + " / " + num2 + " = " + quotient);
}
}
Output:
10. WAP to check a person is eligible for voting or not?
public class VoteEligibility { public
static void main(String[] args) { int
age = 21;
[Link]("Person's age is: " + age);
if (age >= 18) {
[Link]("This person is eligible to vote.");
} else {
[Link]("This person is not eligible to vote.");
}
}
}
Output:
11. WAP to check greater between three number?
public class FindLargest { public
static void main(String[] args) {
double n1 = -4.5;
double n2 = 10.2;
double n3 = 3.0;
[Link]("The numbers are: " + n1 + ", " + n2 + ", " + n3);
if (n1 >= n2) {
if (n1 >= n3) {
[Link](n1 + " is the largest number.");
} else {
[Link](n3 + " is the largest number.");
}
} else {
if (n2 >= n3) {
[Link](n2 + " is the largest number.");
} else {
[Link](n3 + " is the largest number.");
}
}
}
}
Output:
12. WAP to find percentage of student marks?
public class CalculatePercentage {
public static void main(String[] args) {
double marksObtained = 450.0;
double totalMarks = 500.0;
double percentage = (marksObtained / totalMarks) * 100;
[Link]("Marks Obtained: " + marksObtained);
[Link]("Total Marks: " + totalMarks);
[Link]("Percentage: " + percentage + "%");
Output:
13. WAP to print a table of any given number?
public class MultiplicationTable
{ public static void main(String[]
args) {
int num = 8;
[Link]("Multiplication table of " + num + ":");
for (int i = 1; i <= 10; i++) {
[Link](num + " x " + i + " = " + (num * i));
}
}
}
Output:
14. WAP to find factorial of a number?
public class MultiplicationTable
{ public static void main(String[]
args) { int num = 8;
[Link]("Multiplication table of " + num + ":");
for (int i = 1; i <= 10; i++) {
[Link](num + " x " + i + " = " + (num * i));
Output:
15. WAP to display Fibonacci series?
public class FibonacciSeries { public
static void main(String[] args) { int
n = 10; int firstTerm = 0; int
secondTerm = 1;
[Link]("Fibonacci Series up to " + n + " terms:");
for (int i = 1; i <= n; i++)
{ [Link](firstTerm + " ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm; secondTerm =
nextTerm;
}
[Link]();
}
}
Output:
16. WAP to perform multiplication of a matrix?
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] matrixA = { {1, 2, 3}, {4, 5, 6} };
int[][] matrixB = { {7, 8}, {9, 10}, {11, 12} };
int rowsA = [Link];
int colsA = matrixA[0].length;
int rowsB = [Link];
int colsB = matrixB[0].length;
if (colsA != rowsB) {
[Link]("Matrices cannot be multiplied.");
return;
}
int[][] resultMatrix = new int[rowsA][colsB];
for (int i = 0; i < rowsA; i++) { for (int j = 0; j <
colsB; j++) { for (int k = 0; k < colsA; k++) {
resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
[Link]("Resulting
Matrix:"); for (int i = 0; i < rowsA; i++)
{ for (int j = 0; j < colsB; j++) {
[Link](resultMatrix[i][j] + " ");
}
[Link]();
}
}
}
OutPut:
17. WAP to calculate area of rectangle using class and objects?
class Rectangle
{ double length;
double width;
double calculateArea()
{ return length * width;
}
}
public class AreaCalculator { public
static void main(String[] args)
{ Rectangle rect = new Rectangle();
[Link] = 15.0; [Link] =
10.0;
double area = [Link]();
[Link]("The area of the rectangle is: " + area);
}
}
Output:
18. WAP to show the use of "this" keyword?
class Person {
String name;
int age;
Person(String name, int age)
{ [Link] = name;
[Link] = age;
}
void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
public class TestThis { public static void
main(String[] args) { Person p1 = new
Person("Aayush", 20); [Link]();
}
}
Output:
19. WAP to implement Single inheritance?
class Animal
{ void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal
{ void bark() {
[Link]("The dog barks.");
}
}
public class SingleInheritanceTest
{ public static void main(String[] args)
{
Dog myDog = new Dog();
[Link]();
[Link]();
}
}
Output:
20. WAP to implement multi-level inheritance?
class Animal
{ void eat() {
[Link]("eating...");
}
}
class Dog extends Animal
{ void bark() {
[Link]("barking...");
}
}
class Puppy extends Dog
{ void weep() {
[Link]("weeping...");
}
}
public class MultilevelInheritanceTest {
public static void main(String[] args) {
Puppy p = new Puppy(); [Link]();
[Link]();
[Link]();
}
}
Output:
21. WAP to implement abstract class?
abstract class Shape {
abstract double getArea();
void display() {
[Link]("This is a shape.");
class Circle extends Shape {
double radius;
Circle(double r) {
[Link] = r;
@Override
double getArea() {
return [Link] * radius * radius;
}
public class AbstractTest {
public static void main(String[] args)
{ Shape myCircle = new Circle(5.0);
[Link]();
[Link]("The area of the circle is: " + [Link]());
Output:
22. WAP to implement method overriding?
class Vehicle
{ void run() {
[Link]("Vehicle is moving");
}
}
class Bike extends Vehicle {
@Override void run() {
[Link]("Bike is running safely");
}
}
public class MethodOverridingTest
{ public static void main(String[] args) {
Bike myBike = new Bike();
[Link]();
}
}
Output:
23. WAP to implement method overloading?
class DisplayOverload
{ void display(int a) {
[Link]("Displaying integer: " + a);
}
void display(String s) {
[Link]("Displaying string: " + s);
}
void display(int a, double b) {
[Link]("Displaying int and double: " + a + " and " + b);
}
}
public class MethodOverloadingTest
{ public static void main(String[] args) {
DisplayOverload obj = new DisplayOverload();
[Link](100);
[Link]("Hello Java");
[Link](200, 10.5);
}
}
Output:
24. WAP to implement polymorphism?
class Animal { void
makeSound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override void
makeSound() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
@Override void
makeSound() {
[Link]("Cat meows");
}
}
public class PolymorphismTest {
public static void main(String[] args) {
Animal myAnimal;
myAnimal = new Dog();
[Link]();
myAnimal = new Cat();
[Link]();
}
}
Output:
25. WAP to implement interface?
interface Drawable
{ void draw();
}
class Rectangle implements Drawable
{ public void draw() {
[Link]("drawing rectangle");
}
}
class Circle implements Drawable
{ public void draw() {
[Link]("drawing circle");
}
}
public class InterfaceTest { public
static void main(String[] args) {
Drawable d1 = new Rectangle();
[Link]();
Drawable d2 = new Circle();
[Link]();
}
}
Output:
26. WAP to implement multiple inheritance using interface?
interface FirstInterface
{ void methodOne();
}
interface SecondInterface
{ void methodTwo();
}
class DemoClass implements FirstInterface, SecondInterface {
public void methodOne() {
[Link]("Executing methodOne from FirstInterface");
}
public void methodTwo() {
[Link]("Executing methodTwo from SecondInterface");
}
}
public class MultipleInheritanceTest {
public static void main(String[] args) {
DemoClass obj = new DemoClass();
[Link]();
[Link]();
}
}
Output:
27. WAP to implement exception handling?
public class ExceptionHandlingTest {
public static void main(String[] args) {
try {
int result = 100 / 0;
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("An exception occurred: Cannot divide by
zero.");
[Link]("Rest of the code continues...");
Output:
28. WAP to reverse a string in java?
public class ReverseString { public
static void main(String[] args) {
String original = "Hello Java";
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--)
{ reversed = reversed + [Link](i);
[Link]("Original string: " + original);
[Link]("Reversed string: " + reversed);
Output:
29. WAP to concatenate two strings?
public class ConcatenateWithMethod
{ public static void main(String[] args) {
String str1 = "Java is";
String str2 = " awesome!";
String str3 = [Link](str2);
[Link]("--- Using concat() method ---");
[Link](str3);
Output:
30. WAP to check a string is palindrome?
public class PalindromeCheck { public
static void main(String[] args) {
String original = "madam";
String reversed = ""; int
length = [Link]();
for (int i = length - 1; i >= 0; i--)
{ reversed = reversed +
[Link](i);
}
[Link]("Original string: " + original);
if ([Link](reversed)) {
[Link](original + " is a palindrome.");
} else {
[Link](original + " is not a palindrome.");
}
}
}
Output: