Vivek Sharma (2309005370095)
Q1) Prepare Complex Class
class Complex:
Data Members :
int real;
int img;
Methods :
Complex()
Complex(int real, int img)
void incr(int, int) //increase the value real & img according to
parameters
Complex Sum(Complex)
int multiply(Complex)
void display() //display in format a + ib
Input:
public class Complex {
private int real;
private int img;
public Complex() {
[Link] = 0;
[Link] = 0;
}
public Complex(int real, int img) {
[Link] = real;
[Link] = img;
}
public void incr(int real, int img) {
[Link] += real;
[Link] += img;
}
public Complex sum(Complex c) {
return new Complex([Link] + [Link], [Link] + [Link]);
}
public Complex multiply(Complex c) {
int realPart = ([Link] * [Link]) - ([Link] * [Link]);
int imgPart = ([Link] * [Link]) + ([Link] * [Link]);
1 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
return new Complex(realPart, imgPart);
}
public void display() {
[Link]([Link] + " + " + [Link] + "i");
}
public static void main(String[] args) {
Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(1, 2);
[Link]("Complex Number 1: ");
[Link]();
[Link]("Complex Number 2: ");
[Link]();
Complex sum = [Link](c2);
[Link]("Sum: ");
[Link]();
Complex product = [Link](c2);
[Link]("Product: ");
[Link]();
}
}
Output:
Complex Number 1: 3 + 4i
Complex Number 2: 1 + 2i
Sum: 4 + 6i
Product: -5 + 10i
2 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
Q2) Write a program for Quadratic Equation ax^2 + bx + c
where a, b, c are integer values. Find the roots of the
equation.
class Qe:
Data member : int a, b, c
Methods :
Qe()
Qe(int, int, int)
void result() // Output : Roots are real : value 1, value 2
// : Roots are imaginary
: Roots are equal : value
Input:
public class Qe{
private int a, b, c;
public Qe() {
this.a = 0;
this.b = 0;
this.c = 0;
}
public Qe(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public void result() {
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: " + root1 + ", " +
root2);
} else if (discriminant == 0) {
double root = -b / (2.0 * a);
[Link]("Roots are equal: " + root);
} else {
[Link]("Roots are imaginary.");
3 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
}
}
public static void main(String[] args) {
Qe equation = new Qe(1, -3, 2);
[Link]();
}
}
Output:
Roots are real: 2.0, 1.0
4 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
Q3) Prepare stack using object oriented Approach :
class Stack:
Data member :
Char array size of 5
int top
Methods :
void push(char)
char pop()
(menu driven program using switch case)
Input:
import [Link];
public class Stack {
private char[] stackArray;
private int top;
public Stack() {
stackArray = new char[5];
top = -1;
}
public void push(char c) {
if (top == [Link] - 1) {
[Link]("Stack Overflow!");
} else {
stackArray[++top] = c;
[Link]("Pushed: " + c);
}
}
public char pop() {
if (top == -1) {
[Link]("Stack Underflow!");
return '\0';
} else {
char c = stackArray[top--];
[Link]("Popped: " + c);
return c;
}
}
5 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
public static void main(String[] args) {
Stack stack = new Stack();
Scanner scanner = new Scanner([Link]);
int choice;
char element;
do {
[Link]("\nMenu:\n1. Push\n2. Pop\n3. Exit\nEnter
your choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter a character to push: ");
element = [Link]().charAt(0);
[Link](element);
break;
case 2:
[Link]();
break;
case 3:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice. Try again.");
}
} while (choice != 3);
[Link]();
}
}
Output:
Menu:
1. Push
2. Pop
3. Exit
Enter your choice:
1
Enter a character to push: A
Pushed: A
Menu:
1. Push
6 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
2. Pop
3. Exit
Enter your choice:
2
Popped: A
Menu:
1. Push
2. Pop
3. Exit
Enter your choice:
3
Exiting...
7 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
Q4) Write a circle class with following details:
Class circle:
Data members :
Double radius
Method :
circle()
circle(double)
double circumference()
double area()
Input:
public class Circle {
private double radius;
public Circle() {
[Link] = 0;
}
public Circle(double radius) {
[Link] = radius;
}
public double circumference() {
return 2 * [Link] * radius;
}
public double area() {
return [Link] * radius * radius;
}
public static void main(String[] args) {
Circle circle = new Circle(5);
[Link]("Circumference: " + [Link]());
[Link]("Area: " + [Link]());
}
}
Output:
Circumference: 31.41592653589793
Area: 78.53981633974483
8 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
Q5) Write a cylinder class which extends all the feature of
circle class:
class Cylinder extends Circle:
Data member :
double height
Methods :
Cylinder()
Cylinder(double radius, double height)
double curvedSurfaceArea()
double volume()
void totalSurfaceArea()
Input:
// Circle class
public class Circle {
protected double radius;
public Circle() {
[Link] = 0;
}
public Circle(double radius) {
[Link] = radius;
}
public double circumference() {
return 2 * [Link] * radius;
}
public double area() {
return [Link] * radius * radius;
}
}
// Cylinder class extending Circle
public class Cylinder extends Circle {
private double height;
public Cylinder() {
super();
9 Object Oriented Programming With Java
Vivek Sharma (2309005370095)
[Link] = 0;
}
public Cylinder(double radius, double height) {
super(radius);
[Link] = height;
}
public double curvedSurfaceArea() {
return 2 * [Link] * radius * height;
}
public double volume() {
return [Link] * radius * radius * height;
}
public double totalSurfaceArea() {
return 2 * [Link] * radius * (radius + height);
}
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(5, 10);
[Link]("Curved Surface Area: " +
[Link]());
[Link]("Volume: " + [Link]());
[Link]("Total Surface Area: " +
[Link]());
}
}
Output:
Curved Surface Area: 314.1592653589793
Volume: 785.3981633974483
Total Surface Area: 471.23889803846896
10 Object Oriented Programming With Java