EXPERIMENT 1
BASIC PROGRAM 1
public class Demo11 {
public static void main (String args[]){
[Link]("My Name is Bhuvan");
}
}
BASIC PROGRAM 2
import [Link].*;
public class b {
public static void main(String args[]) {
int num1, num2, sum;
Scanner c = new Scanner([Link]);
[Link]("Enter the first number:");
num1=[Link]();
[Link]("Enter second number:");
num2=[Link]();
sum=num1 +num2;
[Link]("Sum of 2 number is :" + sum);
}
}
BASIC PROGRAM 3
public class SumOf2Nums {
public static void main(String args[]){
int num1=20,num2=33,sum;
sum=num1+num2;
[Link]("Sum of 2 numbers is "+sum);
}
}
EXPERIMENT 2
WRITE A JAVA PROGRAM TO ILLUSTRATE STATIC AND INSTANCE
VARIABLE.
public class Demo {
int x,y;
}
class variable {
public static void main(String args[]) {
int sum1, sum2;
Demo obj1=new Demo();
Demo obj2=new Demo();
obj1.x=10;
obj1.y=20;
obj2.x=30;
obj2.y=40;
sum1=obj1.x+obj1.y;
sum2=obj2.x+obj2.y;
[Link]("sum1="+sum1);
[Link]("sum2="+sum2);
}
}
EXPERIMENT 3
WRITE A JAVA PROGRAM TO ILLUSTRATE DESTRUCTOR.
public class Destructor {
public static void main(String[] args) {
Destructor de = new Destructor();
[Link]();
[Link]();
[Link]("Inside the main method");
}
@Override
protected void finalize() {
[Link]("Object is destroyed by the garbage collector");
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE CONSTRUCTOR.
public class Dog {
String name,breed,color;
int age;
public Dog(String name, String breed, int age, String color){
[Link] = name;
[Link] = breed;
[Link] = age;
[Link] = color;
}
public String get_name() {
return name;
}
public String get_breed() {
return breed;
}
public int get_age() {
return age;
}
public String get_color() {
return color;
}
public String toString() {
return "Hi, my name is " + this.get_name() + ". My breed, age, and color are "
+this.get_breed() + ", " + this.get_age() + ", " + this.get_color() + ".";
}
public static void main(String args[]) {
Dog tuffy = new Dog("Tuffy", "Poporion", 5, "White");
[Link]([Link]());}
}
EXPERIMENT 4
WRITE A JAVA PROGRAM TO FIND THE QUADRATIC EQUATIONS
ROOTS.
import [Link];
public class QuadraticEquationSolver {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the coefficients of the quadratic equation (a, b,
c):");
[Link](" a = ");
double a = [Link]();
[Link](" b = ");
double b = [Link]();
[Link](" c = ");
double c = [Link]();
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + [Link](discriminant)) / (2 * a);
double root2 = (-b - [Link](discriminant)) / (2 * a);
[Link]("Roots are real and different.");
[Link]("Root 1 = " + root1);
[Link]("Root 2 = " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
[Link]("Roots are real and the same.");
[Link]("Root = " + root);
} else {
[Link]("Roots are complex and different.");
double realPart = -b / (2 * a);
double imaginaryPart = [Link]([Link](discriminant)) / (2 * a);
[Link]("Root 1 = " + realPart + " + " + imaginaryPart + "i");
[Link]("Root 2 = " + realPart + " - " + imaginaryPart + "i");
}
}
}
EXPERIMENT 5
WRITE A JAVA PROGRAM TO ILLUSTRATE RELATIONAL
OPERATORS.
public class Relational {
public static void main(String args[]) {
int a = 10;
int b = 20;
[Link](a < b);
[Link](a > b);
[Link](a <= b);
[Link](a >= b);
[Link](a == b);
[Link](a != b);
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE AIRTHMETIC
OPERATORS
public class ArithmeticOperators {
public static void main(String args[]) {
int a = 10;
int b = 5;
[Link](a + b);
[Link](a - b);
[Link](a * b);
[Link](a / b);
[Link](a % b);
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE UNARY OPERATORS.
public class OperatorExample {
public static void main(String args[]) {
int x = 11;
[Link](x++);
[Link](++x);
[Link](x--);
[Link](--x);
}
}
EXPERIMENT 6
WRITE A JAVA PROGRAM TO ILLUSTRATE AUTOBOXING.
public class BoxingExample {
public static void main(String args[]) {
int a = 50;
Integer a2 = new Integer(a);
Integer a3 = 60;
[Link](a2 + " " + a3);
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE UNBOXING.
public class UnboxingExample {
public static void main(String args[]) {
Integer i = new Integer(50);
int a = i; // Unboxing
[Link](a);
}
}
EXPERIMENT 7
WRITE A JAVA PROGRAM TO CHECK WETHER THE GIVEN NUMBER
IS PRIME OR NOT .
import [Link];
public class PrimeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
int divisor = 2;
while (divisor <= number / 2) {
if (number % divisor == 0) {
isPrime = false;
break;
}
divisor++;
}
}
if (isPrime) {
[Link](number + " is a prime number.");
} else {
[Link](number + " is not a prime number.");
}
}
}
WRITE A JAVA PROGRAM TO CHECK WETHER THE GIVEN NUMBER
IS EVEN OR ODD.
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
if (number % 2 == 0) {
[Link](number + " is an even number.");
} else {
[Link](number + " is an odd number.");
}
[Link]();
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE SWITCH CASE CONCEPT
import [Link];
public class MonthDays {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the month number (1-12): ");
int monthNumber = [Link]();
switch (monthNumber) {
case 1:
[Link]("January (31 days)");
break;
case 2:
[Link]("February (28 or 29 days, depending on the year)");
break;
case 3:
[Link]("March (31 days)");
break;
case 4:
[Link]("April (30 days)");
break;
case 5:
[Link]("May (31 days)");
break;
case 6:
[Link]("June (30 days)");
break;
case 7:
[Link]("July (31 days)");
break;
case 8:
[Link]("August (31 days)");
break;
case 9:
[Link]("September (30 days)");
break;
case 10:
[Link]("October (31 days)");
break;
case 11:
[Link]("November (30 days)");
break;
case 12:
[Link]("December (31 days)");
break;
default:
[Link]("Invalid month number. Please enter a number
between 1 and 12.");
}
[Link]();
}
}
EXPERIMENT 8
WRITE A JAVA PROGRAM TO PERFORM ADDITON OF 2 MATRIXS.
import [Link];
public class matrix_practice {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows");
int rows = [Link]();
[Link]("Enter the number of columns");
int columns = [Link]();
int[][] a = new int[rows][columns];
int[][] b = new int[rows][columns];
int[][] result = new int[rows][columns];
[Link]("Enter the element of first matrix ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j] = [Link]();}
}
[Link]("Enter the element of second matrix ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
b[i][j] = [Link]();}
}
//FOR ADDING MATRIX
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = a[i][j]+b[i][j];}
}
[Link]("Results ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
[Link](result[i][j]+" ");
}[Link]();}
}
}
WRITE A JAVA PROGRAM TO PERFORM MULTIPLICATION ON 2
MATRIXS.
import [Link];
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of rows for first matrix:");
int rows1 = [Link]();
[Link]("Enter the number of columns for first matrix:");
int cols1 = [Link]();
[Link]("Enter the number of rows for second matrix:");
int rows2 = [Link]();
[Link]("Enter the number of columns for second matrix:");
int cols2 = [Link]();
if (cols1 != rows2) {
[Link]("Multiplication is not possible! Number of columns in
the first matrix should be equal to the number of rows in the second matrix.");
return;
}
int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[rows2][cols2];
int[][] resultMatrix = new int[rows1][cols2];
[Link]("Enter elements of first matrix:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix1[i][j] = [Link]();
}
}
[Link]("Enter elements of second matrix:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
matrix2[i][j] = [Link]();
}
}
// Multiplying matrices
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
resultMatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Displaying the result
[Link]("Result of matrix multiplication:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
[Link](resultMatrix[i][j] + " ");
}
[Link]();
}
[Link]();
}
}
EXPERIMENT 9
WRITE A JAVA PROGRAM TO PERFORM SIMPLE CALCULATOR
PROGRAM.
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the first number: ");
double num1 = [Link]();
[Link]("Enter the operator (+, -, *, /): ");
char operator = [Link]().charAt(0);
[Link]("Enter the second number: ");
double num2 = [Link]();
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
[Link]("Cannot divide by zero.");
return;
}
break;
default:
[Link]("Invalid operator. Please enter +, -, *, or /.");
return;
}
[Link]("Result: " + result);
[Link]();
}
}
EXPERIMENT 10
WRITE A JAVA PROGRAM TO ILLUSTRATE SINGLE INHERITANCE .
class Animal {
void eat() {
[Link]("Animal is eating");
}
void sleep() {
[Link]("Animal is sleeping");
}
}
class Dogs extends Animal {
void bark() {
[Link]("Dog is barking");
}
void stand() {
[Link]("Dog is standing");
}
}
public class Main11 {
public static void main(String[] args) {
Dogs myDog = new Dogs();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE MULTILEVEL
INHERITANCE.
class Animall {
void eat() {
[Link]("Eating...");
}
}
class Dogg extends Animall {
void bark() {
[Link]("Barking...");
}
}
class GermanShepherd extends Dogg {
void guard() {
[Link]("Guarding...");
}
}
public class MultilevelInheritance1 {
public static void main(String[] args) {
GermanShepherd germanShepherd = new GermanShepherd();
[Link]();
[Link]();
[Link]();
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE HIERARCHICAL
INHERITANCE .
class Animal7 {
void eat() {
[Link]("Animal is eating");
}
void sleep() {
[Link]("Animal is sleeping");
}
}
class Dog7 extends Animal {
void bark() {
[Link]("Dog is barking");
}
void playFetch() {
[Link]("Dog is playing fetch");
}
}
class Cat extends Animal {
void meow() {
[Link]("Cat is meowing");
}
void scratch() {
[Link]("Cat is scratching");
}
}
public class Hierarchical123 {
public static void main(String[] args) {
Dog7 myDog = new Dog7();
Cat myCat = new Cat();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
EXPERIMENT 11
WRITE A JAVA PROGRAM TO ILLUSTRATE ENCAPSULATION .
class Account {
private long acc_no;
private String name, email;
private float amount;
public long getAcc_no() {
return acc_no;
}
public void setAcc_no(long acc_no)
{
this.acc_no = acc_no;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email)
{
[Link] = email;
}
public float getAmount() {
return amount; }
public void setAmount(float amount)
{
[Link] = amount;
}
}
public class GFG {
public static void main(String[] args)
{
Account acc = new Account();
acc.setAcc_no(90482098491L);
[Link]("ABC");
[Link]("abc@[Link]");
[Link](100000f);
[Link](acc.getAcc_no() + " " + [Link]() + " "
+ [Link]() + " " + [Link]());
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE ABSTRACTION .
abstract class Shape {
public abstract double area();
public void display() {
[Link]("This is a shape.");
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override
public double area() {
return length * width;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double area() {
return [Link] * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
Circle circle = new Circle(4);
[Link]("Area of rectangle: " + [Link]());
[Link]();
[Link]("Area of circle: " + [Link]());
[Link]();
}
}
EXPERIMENT 12
WRITE A JAVA PROGRAM TO ILLUSTRATE COMPILE TIME
POLYMORPHISM .
public class Compiletime {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public double add(double a, double b) {
return a + b;
}
public double add(double a, double b,double c) {
return a + b +c;
}
public String add(String a, String b) {
return a + b;
}
public static void main(String[] args) {
Compiletime e = new Compiletime();
[Link]("Sum of 2 and 3 is: " + [Link](2, 3));
[Link]("Sum of 2.5 and 3.5 is: " + [Link](2.5, 3.5));
[Link]("Sum of 2, 3, and 4 is: " + [Link](2, 3, 4));
[Link]("Sum of 2.5 and 3.5 and 1.0 is: " + [Link](2.5, 3.5,1.0));
[Link]("Concatenation of 'Hello' and 'World' is: " + [Link]("Hello",
"World"));
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE RUN TIME
POLYMORPHISM .
class Vehicle {
void drive() {
[Link]("Vehicle is being driven");}
}
class Car extends Vehicle {
@Override
void drive() {
[Link]("Car is being driven");}
}
class Bike extends Vehicle {
@Override
void drive() {
[Link]("Bike is being driven");}
}
class Aeroplane extends Vehicle {
@Override
void drive() {
[Link]("Aeroplane is being driven");}
}
public class Runtime {
public static void main(String[] args) {
Vehicle myCar = new Car();
Vehicle myBike = new Bike();
Aeroplane aero = new Aeroplane();
[Link]();
[Link]();
[Link]();
}
}
EXPERIMENT 13
WRITE A JAVA PROGRAM TO ILLUSTRATE EXCEPTION HANDLING
import [Link];
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter two numbers:");
int num1 = [Link]();
int num2 = [Link]();
try {
// Division operation that may throw an ArithmeticException
int result = divideNumbers(num1, num2);
[Link]("Result of division: " + result);
} catch (ArithmeticException e) {
// Catching the ArithmeticException and handling it
[Link]("Error: Division by zero is not allowed.");
}
[Link]();
}
// Method to perform division
public static int divideNumbers(int dividend, int divisor) {
return dividend / divisor;
}
}
WRITE A JAVA PROGRAM TO READ DATA FROM THE FILE AND
WRITE DATA THE FILE.
import [Link].*;
public class InputOutput {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\Users\\PESSTUDENT \\IdeaProjects
\\ myfirstjava\\[Link]");
out = new FileOutputStream("C:\\Users\\PESSTUDENT\\IdeaProjects
\\myfirstjava\\[Link]");
int c;
while ((c = [Link]()) != -1) {
[Link](c);
}
} catch (IOException e) {
[Link]();
} finally {
if (in != null) {
[Link]();
}
if (out != null) {
[Link]();
}
}
}
}
WRITE A JAVA PROGRAM TO PERFORM DATABASE CONNECTION
import [Link];
import [Link].*;
public class database_insertDB {
public static void main(String[] args) {
int id;
String name, email, password, ph_number;
Scanner s = new Scanner([Link]);
[Link]("enter id");
id = [Link]();
[Link]("enter name");
name = [Link]();
[Link]("enter email");
email = [Link]();
[Link]("enter password");
password = [Link]();
[Link]("phone number");
ph_number = [Link]();
try {
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost/javadb", "root", "");
PreparedStatement ps = [Link]("insert into signup
values(?,?,?,?,?)");
[Link](1, id);
[Link](2, name);
[Link](3, email);
[Link](4, password);
[Link](5, ph_number);
int i = [Link]();
if(i>0){
[Link]("Record Saved");
}
else{
[Link]("Error");
}
} catch (Exception ex) {
[Link]();
}
}
}