OOPS Assignment - Final Corrected Version
1(i) Java Program to Create a Class Box
Save as: [Link]
import [Link];
class Box {
double length, breadth, height;
void input() {
Scanner sc = new Scanner([Link]);
[Link]("Enter Length: ");
length = [Link]();
[Link]("Enter Breadth: ");
breadth = [Link]();
[Link]("Enter Height: ");
height = [Link]();
}
void display() {
[Link]("Length = " + length);
[Link]("Breadth = " + breadth);
[Link]("Height = " + height);
}
double volume() {
return length * breadth * height;
}
}
public class BoxDemo1 {
public static void main(String[] args) {
Box b = new Box();
[Link]();
[Link]("\nBox Details:");
[Link]();
[Link]("Volume = " + [Link]());
}
}
1(ii) Java Program using Constructor and Compare Two Box Objects
Save as: [Link]
import [Link];
class Box2 {
double length, breadth, height;
Box2(double l, double b, double h) {
length = l;
breadth = b;
height = h;
}
void display() {
[Link]("Length = " + length);
[Link]("Breadth = " + breadth);
[Link]("Height = " + height);
}
double volume() {
return length * breadth * height;
}
boolean compare(Box2 x) {
if(length == [Link] &&
breadth == [Link] &&
height == [Link])
return true;
else
return false;
}
}
public class BoxDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Details of Box 1");
[Link]("Enter Length: ");
double l1 = [Link]();
[Link]("Enter Breadth: ");
double b1 = [Link]();
[Link]("Enter Height: ");
double h1 = [Link]();
[Link]("\nEnter Details of Box 2");
[Link]("Enter Length: ");
double l2 = [Link]();
[Link]("Enter Breadth: ");
double b2 = [Link]();
[Link]("Enter Height: ");
double h2 = [Link]();
Box2 box1 = new Box2(l1, b1, h1);
Box2 box2 = new Box2(l2, b2, h2);
[Link]("\nDetails of Box 1:");
[Link]();
[Link]("Volume = " + [Link]());
if([Link](box2))
[Link]("\nBoth Boxes are Equal");
else
[Link]("\nBoth Boxes are Not Equal");
}
}
2. Java Program for Addition of Two Matrices
Save as: [Link]
import [Link];
public class MatrixAddition {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int row, col;
[Link]("Enter Number of Rows: ");
row = [Link]();
[Link]("Enter Number of Columns: ");
col = [Link]();
int[][] a = new int[row][col];
int[][] b = new int[row][col];
int[][] c = new int[row][col];
[Link]("Enter Elements of First Matrix");
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
a[i][j] = [Link]();
}
}
[Link]("Enter Elements of Second Matrix");
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
b[i][j] = [Link]();
}
}
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
[Link]("\nSum Matrix");
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
[Link](c[i][j] + " ");
}
[Link]();
}
}
}
3. C++ Program using Function Template to Compare Two Numbers
Save as: [Link]
#include <iostream>
using namespace std;
template <class T>
void compare(T a, T b)
{
if(a > b)
cout << a << " is Greater";
else if(b > a)
cout << b << " is Greater";
else
cout << "Both Numbers are Equal";
}
int main()
{
int x, y;
cout << "Enter First Number: ";
cin >> x;
cout << "Enter Second Number: ";
cin >> y;
compare(x, y);
return 0;
}
4. C++ Program to Implement Stack using Class Template with Dynamic
Memory Allocation
Save as: [Link]
#include <iostream>
using namespace std;
template <class T>
class Stack {
T *arr;
int top;
int size;
public:
Stack(int s) {
size = s;
top = -1;
arr = new T[size];
}
~Stack() {
delete[] arr;
}
void push(T value) {
if(top == size - 1)
cout << "Stack Overflow\n";
else {
top++;
arr[top] = value;
cout << "Element Inserted Successfully\n";
}
}
void pop() {
if(top == -1)
cout << "Stack Underflow\n";
else {
cout << "Deleted Element = " << arr[top] << endl;
top--;
}
}
void display() {
if(top == -1)
cout << "Stack is Empty\n";
else {
cout << "Stack Elements are:\n";
for(int i = top; i >= 0; i--) {
cout << arr[i] << endl;
}
}
}
};
int main() {
int n, choice;
int value;
cout << "Enter Size of Stack: ";
cin >> n;
Stack<int> s(n);
do {
cout << "\n1. PUSH";
cout << "\n2. POP";
cout << "\n3. DISPLAY";
cout << "\n4. EXIT";
cout << "\nEnter Your Choice: ";
cin >> choice;
switch(choice) {
case 1:
cout << "Enter Value: ";
cin >> value;
[Link](value);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
cout << "Program Terminated";
break;
default:
cout << "Invalid Choice";
}
} while(choice != 4);
return 0;
}