0% found this document useful (0 votes)
20 views

Simran Vishisht Java Assignment 2

The document contains an assignment submission for a programming in Java course. It includes answers to 4 programming questions involving stacks, complex numbers, inheritance, and polymorphism. Dynamic and fixed length stacks are implemented using interfaces. Complex number arithmetic and toString methods are defined. Inheritance is demonstrated between 2D and 3D coordinates classes. Polymorphism is shown using an abstract shape class and rectangle/circle subclasses.

Uploaded by

Simi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Simran Vishisht Java Assignment 2

The document contains an assignment submission for a programming in Java course. It includes answers to 4 programming questions involving stacks, complex numbers, inheritance, and polymorphism. Dynamic and fixed length stacks are implemented using interfaces. Complex number arithmetic and toString methods are defined. Inheritance is demonstrated between 2D and 3D coordinates classes. Polymorphism is shown using an abstract shape class and rectangle/circle subclasses.

Uploaded by

Simi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

INDRAPRASTHA COLLEGE FOR WOMEN

COMPUTER SCIENCE DEPARTMENT

ASSIGNMENT: 2

SUBMITTED BY: SIMRAN VISHISHT


SUBMITTED TO: Dr. SHIKHA AGARWAL
ROLL NO: 21/CS/48
SEMESTER: IInd
PAPER: PROGRAMMING IN JAVA
1. Write program to implement dynamic and fixed length stack making use of interfaces in java. (For
Run Time Polymorphism).
Answer:
interface Stack
{
void push(int item);
int pop();
}

class Fixedstack implements Stack


{
private int stck[];
private int tos;
Fixedstack (int size)
{
stck=new int [size];
tos=-1;
}

public void push(int item)


{
if(tos==stck.length-1)
System.out.println("stack is full");
else
stck[++tos]=item;
}

public int pop()


{
if(tos<0)
{
System.out.println("Stack underflow");
return 0;
}
else
return stck[tos--];
}
}

class Dynstack implements Stack


{
private int stck[];
private int tos;
Dynstack(int size)
{
stck=new int [size];
tos=-1;
}

public void push(int item)


{
if(tos==stck.length-1)
{
int temp[]=new int[stck.length*2];
for(int i=0;i<stck.length;i++) temp[i]=stck[i];
stck=temp;
stck[++tos]=item;
}
else
stck[++tos]=item;
}
public int pop()
{
if(tos<0)
{
System.out.println("Stack underflow");
return 0;
}

else
return stck[tos--];
}
}

public class dynamic_fixed_stack


{
// private int stck[];
public static void main(String args[])
{
Stack mystack;
Dynstack ds=new Dynstack(5);
Fixedstack fs=new Fixedstack(8);
mystack=ds;
for(int i=0;i<12;i++) mystack.push(i);
mystack=fs;
for(int i=0;i<8;i++) mystack.push(i);
mystack=ds;
System.out.println("elements of dynamic stack:");
for(int i=0;i<12;i++)
System.out.println(mystack.pop());
mystack=fs;
System.out.println("elements of fixed stack:");
for(int i=0;i<8;i++)
System.out.println(mystack.pop());
}
}
2. Design a class Complex having a real part (x) and an imaginary part (y). Provide methods to perform
the following on complex numbers:
a) Add two complex numbers.
b) Multiply two complex numbers.
c) toString() method to display complex numbers in the form: x + i y
Answer:
public class complex
{
int real;
int imaginary;
complex()
{
real=0;
imaginary=0;
}
complex(int a,int b)
{
real=a;
imaginary=b;
}
void Add(complex b)
{
complex c=new complex();
c.real=this.real+b.real;
c.imaginary=this.imaginary+b.imaginary;
System.out.println("Complex number is "+c.real+ " + i"+ c.imaginary);
}
void Multiply(complex b)
{
complex c=new complex();
c.real=this.real*b.real - this.imaginary*b.imaginary;
c.imaginary=this.real*b.imaginary+ this.imaginary*b.real;
System.out.println("Complex number is "+c.real+ " + i"+ c.imaginary);
}
public String toString()
{
return "Complex number is "+real+ " + i"+ imaginary;
}

public static void main(String args[]) {


complex test=new complex(6,2);
complex testB=new complex(4,6);
System.out.println(test.toString());
test.Add(testB);
test.Multiply(testB);
}
}

OUTPUT:

3. Create a class TwoDim which contains private members as x and y coordinates in package P1. Define
the default constructor, a parameterized constructor and override toString() method to display the co-
ordinates. Now reuse this class and in package P2 create another class ThreeDim, adding a new
dimension as z as its private member. Define the constructors for the subclass and override toString()
method in the subclass also. Write appropriate methods to show dynamic method dispatch. The main()
function should be in a package P.
Answer:
TwoDim.java :
package P1;
public class TwoDim {
private int x,y;
public TwoDim()
{
x=0;
y=0;
}
public TwoDim(int a,int b)
{
x=a;
y=b;
}
public String toString()
{
return "Coordinates of x and y are "+x+ " and "+y+"\n";
}
}

TwoDim.java :
package P1;
public class TwoDim {
private int x,y;
public TwoDim()
{
x=0;
y=0;
}
public TwoDim(int a,int b)
{
x=a;
y=b;
}
public String toString()
{
return "Coordinates of x and y are "+x+ " and "+y+"\n";
}
}

Demo.java:
package P;
import P1.*;
import P2.*;
public class DEmo
{
public static void main(String args[]){
TwoDim obj;
obj=new TwoDim(9,4);
System.out.println(obj.toString());
obj=new ThreeDim(6);
System.out.println(obj.toString());
}
}
4. Define an abstract class Shape in package P1. Inherit two more classes: Rectangle in package P2 and
Circle in package P3. Write a program to ask the user for the type of shape and then using the concept
of dynamic method dispatch, display the area of the appropriate subclass. Also write appropriate
methods to read the data. The main() function should not be in any package.
Answer:
Shape.java:
package P1;
abstract public class Shape
{
abstract public void area();
}

Rectangle.java:
package P2;
public class Rectangle extends P1.Shape
{
double length,breadth;
public Rectangle(double a,double b)
{
length=a;
breadth=b;
}
public void area()
{
System.out.print("Area of Rectangle is "+length*breadth+"\n");
}
}

Circle.java:
package P3;
public class Circle extends P1.Shape
{
double radius;
public Circle(double a)
{
radius=a;
}
public void area()
{
System.out.print("Area of circle is "+(3.14*radius*radius));
}
}

DemoShape:
import P1.Shape;
import P2.Rectangle;
import P3.Circle;
public class DemoShape
{
public static void main(String args[])
{
Shape obj;
obj = new Rectangle(5, 7.4);
obj.area();
obj = new Circle(8);
obj.area();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy