0% found this document useful (0 votes)
14 views35 pages

Oopsjavaexpirements 1

The document contains multiple Java programming experiments demonstrating various concepts such as basic input/output, static and instance variables, destructors, constructors, quadratic equations, relational and arithmetic operators, matrix operations, and inheritance. Each experiment includes code snippets and explanations for implementing these concepts in Java. Additionally, it covers advanced topics like encapsulation and polymorphism.

Uploaded by

tejas.j.h8055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views35 pages

Oopsjavaexpirements 1

The document contains multiple Java programming experiments demonstrating various concepts such as basic input/output, static and instance variables, destructors, constructors, quadratic equations, relational and arithmetic operators, matrix operations, and inheritance. Each experiment includes code snippets and explanations for implementing these concepts in Java. Additionally, it covers advanced topics like encapsulation and polymorphism.

Uploaded by

tejas.j.h8055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

EXPERIMENT 1

BASIC PROGRAM 1
public class Demo11 {
public static void main (String args[]){
System.out.println("My Name is Bhuvan");
}
}
BASIC PROGRAM 2
import java.util.*;
public class b {
public static void main(String args[]) {
int num1, num2, sum;
Scanner c = new Scanner(System.in);
System.out.print("Enter the first number:");
num1=c.nextInt();
System.out.print("Enter second number:");
num2=c.nextInt();
sum=num1 +num2;
System.out.print("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;
System.out.println("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;
System.out.println("sum1="+sum1);
System.out.println("sum2="+sum2);
}
}
EXPERIMENT 3
WRITE A JAVA PROGRAM TO ILLUSTRATE DESTRUCTOR.
public class Destructor {
public static void main(String[] args) {
Destructor de = new Destructor();
de.finalize();
System.gc();
System.out.println("Inside the main method");
}
@Override
protected void finalize() {
System.out.println("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){
this.name = name;
this.breed = breed;
this.age = age;
this.color = 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");
System.out.println(tuffy.toString());}
}
EXPERIMENT 4
WRITE A JAVA PROGRAM TO FIND THE QUADRATIC EQUATIONS
ROOTS.
import java.util.Scanner;
public class QuadraticEquationSolver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the coefficients of the quadratic equation (a, b,
c):");
System.out.print(" a = ");
double a = scanner.nextDouble();
System.out.print(" b = ");
double b = scanner.nextDouble();
System.out.print(" c = ");
double c = scanner.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Roots are real and different.");
System.out.println("Root 1 = " + root1);
System.out.println("Root 2 = " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("Roots are real and the same.");
System.out.println("Root = " + root);
} else {
System.out.println("Roots are complex and different.");
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(Math.abs(discriminant)) / (2 * a);
System.out.println("Root 1 = " + realPart + " + " + imaginaryPart + "i");
System.out.println("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;
System.out.println(a < b);
System.out.println(a > b);
System.out.println(a <= b);
System.out.println(a >= b);
System.out.println(a == b);
System.out.println(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;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}

WRITE A JAVA PROGRAM TO ILLUSTRATE UNARY OPERATORS.


public class OperatorExample {
public static void main(String args[]) {
int x = 11;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--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;
System.out.println(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
System.out.println(a);
}
}
EXPERIMENT 7
WRITE A JAVA PROGRAM TO CHECK WETHER THE GIVEN NUMBER
IS PRIME OR NOT .
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
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) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}
WRITE A JAVA PROGRAM TO CHECK WETHER THE GIVEN NUMBER
IS EVEN OR ODD.
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is an odd number.");
}
scanner.close();
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE SWITCH CASE CONCEPT
import java.util.Scanner;
public class MonthDays {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the month number (1-12): ");
int monthNumber = scanner.nextInt();
switch (monthNumber) {
case 1:
System.out.println("January (31 days)");
break;
case 2:
System.out.println("February (28 or 29 days, depending on the year)");
break;
case 3:
System.out.println("March (31 days)");
break;
case 4:
System.out.println("April (30 days)");
break;
case 5:
System.out.println("May (31 days)");
break;
case 6:
System.out.println("June (30 days)");
break;
case 7:
System.out.println("July (31 days)");
break;
case 8:
System.out.println("August (31 days)");
break;
case 9:
System.out.println("September (30 days)");
break;
case 10:
System.out.println("October (31 days)");
break;
case 11:
System.out.println("November (30 days)");
break;
case 12:
System.out.println("December (31 days)");
break;
default:
System.out.println("Invalid month number. Please enter a number
between 1 and 12.");
}
scanner.close();
}
}
EXPERIMENT 8
WRITE A JAVA PROGRAM TO PERFORM ADDITON OF 2 MATRIXS.
import java.util.Scanner;
public class matrix_practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = sc.nextInt();
System.out.println("Enter the number of columns");
int columns = sc.nextInt();
int[][] a = new int[rows][columns];
int[][] b = new int[rows][columns];
int[][] result = new int[rows][columns];
System.out.println("Enter the element of first matrix ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j] = sc.nextInt();}
}
System.out.println("Enter the element of second matrix ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
b[i][j] = sc.nextInt();}
}
//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];}
}
System.out.println("Results ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(result[i][j]+" ");
}System.out.println();}
}
}

WRITE A JAVA PROGRAM TO PERFORM MULTIPLICATION ON 2


MATRIXS.
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows for first matrix:");
int rows1 = scanner.nextInt();
System.out.println("Enter the number of columns for first matrix:");
int cols1 = scanner.nextInt();
System.out.println("Enter the number of rows for second matrix:");
int rows2 = scanner.nextInt();
System.out.println("Enter the number of columns for second matrix:");
int cols2 = scanner.nextInt();

if (cols1 != rows2) {
System.out.println("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];
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix1[i][j] = scanner.nextInt();
}
}
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
matrix2[i][j] = scanner.nextInt();
}
}
// 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
System.out.println("Result of matrix multiplication:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}
EXPERIMENT 9
WRITE A JAVA PROGRAM TO PERFORM SIMPLE CALCULATOR
PROGRAM.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter the operator (+, -, *, /): ");
char operator = sc.next().charAt(0);
System.out.print("Enter the second number: ");
double num2 = sc.nextDouble();
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 {
System.out.println("Cannot divide by zero.");
return;
}
break;
default:
System.out.println("Invalid operator. Please enter +, -, *, or /.");
return;
}
System.out.println("Result: " + result);
sc.close();
}
}
EXPERIMENT 10
WRITE A JAVA PROGRAM TO ILLUSTRATE SINGLE INHERITANCE .
class Animal {
void eat() {
System.out.println("Animal is eating");
}
void sleep() {
System.out.println("Animal is sleeping");
}
}
class Dogs extends Animal {
void bark() {
System.out.println("Dog is barking");
}
void stand() {
System.out.println("Dog is standing");
}
}
public class Main11 {
public static void main(String[] args) {
Dogs myDog = new Dogs();
myDog.eat();
myDog.sleep();
myDog.bark();
myDog.stand();
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE MULTILEVEL
INHERITANCE.
class Animall {
void eat() {
System.out.println("Eating...");
}
}
class Dogg extends Animall {
void bark() {
System.out.println("Barking...");
}
}
class GermanShepherd extends Dogg {
void guard() {
System.out.println("Guarding...");
}
}
public class MultilevelInheritance1 {
public static void main(String[] args) {
GermanShepherd germanShepherd = new GermanShepherd();
germanShepherd.eat();
germanShepherd.bark();
germanShepherd.guard();
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE HIERARCHICAL
INHERITANCE .
class Animal7 {
void eat() {
System.out.println("Animal is eating");
}
void sleep() {
System.out.println("Animal is sleeping");
}
}
class Dog7 extends Animal {
void bark() {
System.out.println("Dog is barking");
}
void playFetch() {
System.out.println("Dog is playing fetch");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
void scratch() {
System.out.println("Cat is scratching");
}
}
public class Hierarchical123 {
public static void main(String[] args) {
Dog7 myDog = new Dog7();
Cat myCat = new Cat();
myDog.eat();
myDog.sleep();
myCat.eat();
myCat.sleep();
myDog.bark();
myDog.playFetch();
myCat.meow();
myCat.scratch();
}
}
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) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public float getAmount() {
return amount; }
public void setAmount(float amount)
{
this.amount = amount;
}
}
public class GFG {
public static void main(String[] args)
{
Account acc = new Account();
acc.setAcc_no(90482098491L);
acc.setName("ABC");
acc.setEmail("[email protected]");
acc.setAmount(100000f);
System.out.println(acc.getAcc_no() + " " + acc.getName() + " "
+ acc.getEmail() + " " + acc.getAmount());
}
}
WRITE A JAVA PROGRAM TO ILLUSTRATE ABSTRACTION .
abstract class Shape {
public abstract double area();
public void display() {
System.out.println("This is a shape.");
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double area() {
return length * width;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
Circle circle = new Circle(4);
System.out.println("Area of rectangle: " + rectangle.area());
rectangle.display();
System.out.println("Area of circle: " + circle.area());
circle.display();
}
}
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();
System.out.println("Sum of 2 and 3 is: " + e.add(2, 3));
System.out.println("Sum of 2.5 and 3.5 is: " + e.add(2.5, 3.5));
System.out.println("Sum of 2, 3, and 4 is: " + e.add(2, 3, 4));
System.out.println("Sum of 2.5 and 3.5 and 1.0 is: " + e.add(2.5, 3.5,1.0));
System.out.println("Concatenation of 'Hello' and 'World' is: " + e.add("Hello",
"World"));
}
}

WRITE A JAVA PROGRAM TO ILLUSTRATE RUN TIME


POLYMORPHISM .
class Vehicle {
void drive() {
System.out.println("Vehicle is being driven");}
}
class Car extends Vehicle {
@Override
void drive() {
System.out.println("Car is being driven");}
}
class Bike extends Vehicle {
@Override
void drive() {
System.out.println("Bike is being driven");}
}
class Aeroplane extends Vehicle {
@Override
void drive() {
System.out.println("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();
myCar.drive();
myBike.drive();
aero.drive();
}
}
EXPERIMENT 13
WRITE A JAVA PROGRAM TO ILLUSTRATE EXCEPTION HANDLING
import java.util.Scanner;
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter two numbers:");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
try {
// Division operation that may throw an ArithmeticException
int result = divideNumbers(num1, num2);
System.out.println("Result of division: " + result);
} catch (ArithmeticException e) {
// Catching the ArithmeticException and handling it
System.out.println("Error: Division by zero is not allowed.");
}
scanner.close();
}
// 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 java.io.*;
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\\input.txt");
out = new FileOutputStream("C:\\Users\\PESSTUDENT\\IdeaProjects
\\myfirstjava\\output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
WRITE A JAVA PROGRAM TO PERFORM DATABASE CONNECTION
import java.util.Scanner;
import java.sql.*;
public class database_insertDB {
public static void main(String[] args) {
int id;
String name, email, password, ph_number;
Scanner s = new Scanner(System.in);
System.out.println("enter id");
id = s.nextInt();
System.out.println("enter name");
name = s.next();
System.out.println("enter email");
email = s.next();
System.out.println("enter password");
password = s.next();
System.out.println("phone number");
ph_number = s.next();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/javadb", "root", "");
PreparedStatement ps = con.prepareStatement("insert into signup
values(?,?,?,?,?)");
ps.setInt(1, id);
ps.setString(2, name);
ps.setString(3, email);
ps.setString(4, password);
ps.setString(5, ph_number);
int i = ps.executeUpdate();
if(i>0){
System.out.println("Record Saved");
}
else{
System.out.println("Error");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

You might also like