0% found this document useful (0 votes)
2 views13 pages

Oop PR

The document outlines various programming experiments in C++ and Java, covering operations on complex numbers, quadratic equations, factorial calculations, prime number generation, bank account functionalities, sorting algorithms, matrix addition, inheritance, interfaces, and abstract classes. Each experiment includes code snippets demonstrating the implementation of the respective concepts. The document serves as a comprehensive guide for learning and practicing fundamental programming techniques.
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)
2 views13 pages

Oop PR

The document outlines various programming experiments in C++ and Java, covering operations on complex numbers, quadratic equations, factorial calculations, prime number generation, bank account functionalities, sorting algorithms, matrix addition, inheritance, interfaces, and abstract classes. Each experiment includes code snippets demonstrating the implementation of the respective concepts. The document serves as a comprehensive guide for learning and practicing fundamental programming techniques.
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

Experiment 01 return temp;

Write a program in C++ to perform following }


operations on complex numbers Add,
Subtract, Multiply, Divide, Complex // Multiplication
conjugate. Design the class for complex Complex multiply(Complex c)
number {
representation and the operations to be Complex temp;
performed [Link] = (real * [Link]) - (imag *
[Link]);
#include <iostream> [Link] = (real * [Link]) + (imag *
using namespace std; [Link]);
return temp;
class Complex }
{
float real, imag; // Division
Complex divide(Complex c)
public: {
// Function to input complex number Complex temp;
void input() float denominator = ([Link] * [Link]) +
{ ([Link] * [Link]);
cout << "Enter real part: ";
cin >> real; [Link] = ((real * [Link]) + (imag *
cout << "Enter imaginary part: "; [Link])) / denominator;
cin >> imag; [Link] = ((imag * [Link]) - (real *
} [Link])) / denominator;
return temp;
// Function to display complex number }
void display()
{ // Complex Conjugate
if (imag >= 0) Complex conjugate()
cout << real << " + " << imag << "i"; {
else Complex temp;
cout << real << " - " << -imag << "i"; [Link] = real;
} [Link] = -imag;
return temp;
// Addition }
Complex add(Complex c) };
{
Complex temp; int main()
[Link] = real + [Link]; {
[Link] = imag + [Link]; Complex c1, c2, result;
return temp;
} cout << "Enter first complex number:\n";
[Link]();
// Subtraction
Complex subtract(Complex c) cout << "\nEnter second complex
{ number:\n";
Complex temp; [Link]();
[Link] = real - [Link];
[Link] = imag - [Link]; cout << "\nAddition: ";
result = [Link](c2);
[Link]();

cout << "\nSubtraction: ";


result = [Link](c2);
[Link]();

cout << "\nMultiplication: ";


result = [Link](c2);
[Link]();

cout << "\nDivision: ";


result = [Link](c2);
[Link]();

cout << "\nConjugate of first complex


number: ";
result = [Link]();
[Link]();

return 0;
}
Experiment 02 else
Write a program in Java to find all the roots {
of a quadratic equation. // Complex roots
double realPart = -b / (2 * a);
import [Link]; double imagPart = [Link](-
discriminant) / (2 * a);
public class QuadraticRoots
{ [Link]("Roots are
public static void main(String[] args) complex:");
{ [Link]("Root 1 = " +
double a, b, c; realPart + " + " + imagPart + "i");
double discriminant, root1, root2; [Link]("Root 2 = " +
realPart + " - " + imagPart + "i");
Scanner sc = new Scanner([Link]); }

[Link]("Enter coefficient a: "); [Link]();


a = [Link](); }
}
[Link]("Enter coefficient b: ");
b = [Link]();

[Link]("Enter coefficient c: ");


c = [Link]();

discriminant = (b * b) - (4 * a * c);

if (discriminant > 0)
{
// Two distinct real roots
root1 = (-b + [Link](discriminant))
/ (2 * a);
root2 = (-b - [Link](discriminant)) /
(2 * a);

[Link]("Roots are real and


different:");
[Link]("Root 1 = " +
root1);
[Link]("Root 2 = " +
root2);
}
else if (discriminant == 0)
{
// Two equal real roots
root1 = -b / (2 * a);

[Link]("Roots are real and


equal:");
[Link]("Root = " + root1);
}
Experiment 03 return true;
Write a program in Java using methods, }
1) To find factorial of a given number.
public static void main(String[] args)
import [Link]; {
int count = 0;
public class FactorialMethod int number = 2;
{
// Method to calculate factorial [Link]("First 50 Prime
static long factorial(int n) Numbers:");
{
long fact = 1; while (count < 50)
for (int i = 1; i <= n; i++) {
fact = fact * i; if (isPrime(number))
return fact; {
} [Link](number + " ");
count++;
public static void main(String[] args) }
{ number++;
Scanner sc = new Scanner([Link]); }
}
[Link]("Enter a number: "); }
int num = [Link]();
3) To find sum and average of N numbers.
long result = factorial(num); import [Link];

[Link]("Factorial of " + num public class SumAverage


+ " = " + result); {
// Method to calculate sum and average
[Link](); static void sumAndAverage(int n)
} {
} Scanner sc = new Scanner([Link]);
int sum = 0;
int num;
2) To display first 50 prime numbers.
for (int i = 1; i <= n; i++)
public class First50Primes {
{ [Link]("Enter number " + i +
// Method to check whether a number is ": ");
prime num = [Link]();
static boolean isPrime(int num) sum = sum + num;
{ }
if (num <= 1)
return false; double average = (double) sum / n;

for (int i = 2; i <= num / 2; i++) [Link]("Sum = " + sum);


{ [Link]("Average = " +
if (num % i == 0) average);
return false; }
}
public static void main(String[] args) [Link]("First 50 Prime
{ Numbers:");
Scanner sc = new Scanner([Link]); while (count < 50)
{
[Link]("Enter value of N: "); if (isPrime(number))
int n = [Link](); {
[Link](number + " ");
sumAndAverage(n); count++;
}
[Link](); number++;
} }
} [Link]();
}

(All Three in one) // iii) Method to find sum and average of N


import [Link]; numbers
static void sumAndAverage(int n)
public class MethodOperations {
{ Scanner sc = new Scanner([Link]);
// i) Method to find factorial int sum = 0;
static long factorial(int n) int num;
{
long fact = 1; for (int i = 1; i <= n; i++)
for (int i = 1; i <= n; i++) {
fact = fact * i; [Link]("Enter number " + i +
return fact; ": ");
} num = [Link]();
sum = sum + num;
// Method to check prime number }
static boolean isPrime(int num)
{ double avg = (double) sum / n;
if (num <= 1)
return false; [Link]("Sum = " + sum);
[Link]("Average = " + avg);
for (int i = 2; i <= num / 2; i++) }
{
if (num % i == 0) public static void main(String[] args)
return false; {
} Scanner sc = new Scanner([Link]);
return true;
} // Factorial
[Link]("Enter a number to find
// ii) Method to display first 50 prime factorial: ");
numbers int num = [Link]();
static void displayPrimes() [Link]("Factorial = " +
{ factorial(num));
int count = 0;
int number = 2; // First 50 prime numbers
displayPrimes();
// Sum and average
[Link]("Enter how many
numbers: ");
int n = [Link]();
sumAndAverage(n);

[Link]();
}
}
Experiment 4) [Link]("Current Balance: " +
Constructor: balance);
Create a Bank Account class with deposit, }
withdraw, and balance check }
functionalities...
public class BankDemo
import [Link]; {
public static void main(String[] args)
class BankAccount {
{ Scanner sc = new Scanner([Link]);
int accountNumber;
String accountHolder; // Creating object using constructor
double balance; BankAccount acc = new
BankAccount(101, "Sachin Pawar", 5000);
// Constructor to initialize account details
BankAccount(int accNo, String name, [Link]();
double initialBalance)
{ [Link](2000);
accountNumber = accNo; [Link]();
accountHolder = name;
balance = initialBalance; [Link](3000);
} [Link]();

// Method to deposit amount [Link]();


void deposit(double amount) }
{ }
balance = balance + amount;
[Link]("Amount Deposited:
" + amount);
}

// Method to withdraw amount


void withdraw(double amount)
{
if (amount <= balance)
{
balance = balance - amount;
[Link]("Amount
Withdrawn: " + amount);
}
else
{
[Link]("Insufficient
Balance!");
}
}

// Method to display balance


void checkBalance()
{
Experiment 05)
Arrays & Strings: 2) List of Names
Write a program in Java to sort import [Link];
i) List of Integers
ii) List of Names public class SortNames
{
1) List of Integers public static void main(String[] args)
{
import [Link]; int n;
Scanner sc = new Scanner([Link]);
public class SortIntegers
{ [Link]("Enter number of
public static void main(String[] args) names: ");
{ n = [Link]();
int n; [Link](); // Clear buffer
Scanner sc = new Scanner([Link]);
String[] names = new String[n];
[Link]("Enter number of
elements: "); [Link]("Enter the names:");
n = [Link](); for (int i = 0; i < n; i++)
names[i] = [Link]();
int[] arr = new int[n];
// Sorting names using simple String
[Link]("Enter the integers:"); comparison
for (int i = 0; i < n; i++) for (int i = 0; i < n - 1; i++)
arr[i] = [Link](); {
for (int j = i + 1; j < n; j++)
// Sorting using simple Bubble Sort {
for (int i = 0; i < n - 1; i++) if
{ (names[i].compareToIgnoreCase(names[j]) >
for (int j = 0; j < n - 1 - i; j++) 0)
{ {
if (arr[j] > arr[j + 1]) String temp = names[i];
{ names[i] = names[j];
int temp = arr[j]; names[j] = temp;
arr[j] = arr[j + 1]; }
arr[j + 1] = temp; }
} }
}
} [Link]("Sorted List of
Names:");
[Link]("Sorted List:"); for (int i = 0; i < n; i++)
for (int i = 0; i < n; i++) [Link](names[i]);
[Link](arr[i] + " ");
[Link]();
[Link](); }
} }
}
Experiment 06) {
2 dimensional Arrays: for (int j = 0; j < c; j++)
Write a Program in Java to add two [Link](sum[i][j] + " ");
matrices... [Link]();
}
import [Link];
[Link]();
public class MatrixAddition }
{ }
public static void main(String[] args)
{
int r, c;
Scanner sc = new Scanner([Link]);

[Link]("Enter number of rows:


");
r = [Link]();

[Link]("Enter number of
columns: ");
c = [Link]();

int[][] A = new int[r][c];


int[][] B = new int[r][c];
int[][] sum = new int[r][c];

[Link]("Enter elements of
first matrix:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
A[i][j] = [Link]();

[Link]("Enter elements of
second matrix:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
B[i][j] = [Link]();

// Adding two matrices


for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
sum[i][j] = A[i][j] + B[i][j];
}
}

[Link]("Sum of the
matrices:");
for (int i = 0; i < r; i++)
Experiment 07) {
Inheritance: return basicSalary + (basicSalary * 0.20);
Create a base class Employee and derived // 20% allowance
classes Manager, Developer with }
overridden salary computation }

// Base class public class InheritanceDemo


class Employee {
{ public static void main(String[] args)
double basicSalary; {
Employee emp;
Employee(double basicSalary)
{ emp = new Manager(50000);
[Link] = basicSalary; [Link]("Manager Salary: " +
} [Link]());

// Method to calculate salary (to be emp = new Developer(40000);


overridden) [Link]("Developer Salary: " +
double calculateSalary() [Link]());
{ }
return basicSalary; }
}
}

// Derived class Manager


class Manager extends Employee
{
Manager(double basicSalary)
{
super(basicSalary);
}

// Overridden method
double calculateSalary()
{
return basicSalary + (basicSalary * 0.30);
// 30% allowance
}
}

// Derived class Developer


class Developer extends Employee
{
Developer(double basicSalary)
{
super(basicSalary);
}

// Overridden method
double calculateSalary()
Experiment 08)
Interface: Implement a program using v = new Car();
interfaces such as Vehicle with classes Car, [Link]();
Bike.. [Link]();

// Interface v = new Bike();


interface Vehicle [Link]();
{ [Link]();
void start(); }
void stop(); }
}

// Class Car implements Vehicle


class Car implements Vehicle
{
public void start()
{
[Link]("Car starts with a
key.");
}

public void stop()


{
[Link]("Car stops using
brakes.");
}
}

// Class Bike implements Vehicle


class Bike implements Vehicle
{
public void start()
{
[Link]("Bike starts with a
kick or button.");
}

public void stop()


{
[Link]("Bike stops using
brakes.");
}
}

// Main class
public class InterfaceDemo
{
public static void main(String[] args)
{
Vehicle v;
Experiment 09)
Abstract Class:
Demonstrate an example where both
abstract class and interface are used in a
payment
gateway context..

abstract class Payment


{
String transactionId;

Payment(String transactionId)
{
[Link] = transactionId;
}

// Concrete method
void showTransaction()
{
[Link]("Transaction ID: " +
transactionId);
}

// Abstract method
abstract void paymentType();
}
Experiment 10) Create a program to validate voter age using
Exception: user-defined exceptions.
Write a program in JAVA using try and catch
for exception handling. import [Link];

import [Link]; // User-defined Exception


class AgeException extends Exception
public class ExceptionDemo {
{ AgeException(String message)
public static void main(String[] args) {
{ super(message);
Scanner sc = new Scanner([Link]); }
}
try
{ // Main class
[Link]("Enter first number: public class VoterValidation
"); {
int a = [Link](); // Method to check voter age
static void checkVoterAge(int age) throws
[Link]("Enter second AgeException
number: "); {
int b = [Link](); if (age < 18)
throw new AgeException("Not eligible
int result = a / b; // May cause to vote! Age must be 18 or above.");
ArithmeticException else
[Link]("Eligible to vote!");
[Link]("Result = " + }
result);
} public static void main(String[] args)
catch (ArithmeticException e) {
{ Scanner sc = new Scanner([Link]);
[Link]("Error: Division by
zero is not allowed."); [Link]("Enter your age: ");
} int age = [Link]();
catch (Exception e)
{ try
[Link]("Error: Invalid {
input."); checkVoterAge(age); // Call method
} to validate age
}
[Link]("Program continues catch (AgeException e)
normally..."); {
[Link](); [Link]("Error: " +
} [Link]());
} }

[Link]("Program
continues...");
[Link]();
}}

You might also like