Oopsjavaexpirements 1
Oopsjavaexpirements 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);
}
}
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"));
}
}