Professional Documents
Culture Documents
Harikuamar Ex 07
Harikuamar Ex 07
2023
Ex. No: 07
Generic Types
Question No.:01
Write a Java program that defines a generic type
LinkedList. The nodes in the linked list should be capable of
holding data of any of the following types: Integer, Double,
Character, String. You should define functions to achieve the
functionality specified below:
a. Add new nodes: If a position is specified, then the
new node should get added immediately after the specified po-
sition. If no position is specified, then the node should get
added as the last node in the list.
b. Remove nodes: If a position is specified, then the
node at that position should get deleted. If a data value is
given, then the first node holding that data should get de-
leted.
c. Get data: The data at a given position should be re-
turned.
Class Diagram:
LinkedList<T>
+data: T
+next: LinkedList<T>
+LinkedList()
+insert(T x): void
+display(): void
+delete(int x): void
+delete(T x): void
+add(T ele, int x): void
+getData(int x): T
Code:
import java.util.*;
class LinkedList<T>
{
T data;
LinkedList<T> next;
LinkedList()
{
next = null;
}
void insert(T x)
{
LinkedList<T> temp = new LinkedList<T>();
temp.next = next;
next = temp;
temp.data = x;
}
void display()
{
LinkedList<T> ptr = next;
while(ptr != null)
{
System.out.print(ptr.data+" ");
ptr = ptr.next;
}
System.out.println();
}
void delete(int x)
{
LinkedList<T> prev = next;
LinkedList<T> ptr = prev.next;
int c = 2;
if(x==1)
{
next = ptr;
prev = next;
ptr = prev.next;
}
while(ptr != null)
{
if(c == x)
{
prev.next = ptr.next;
break;
}
prev = prev.next;
ptr = prev.next;
c++;
}
}
void deletep(T x)
{
LinkedList<T> prev = next;
LinkedList<T> ptr = prev.next;
if(next.data == x)
{
next = ptr;
prev = next;
ptr = prev.next;
}
else
{
while(ptr != null)
{
if(ptr.data==x)
{
prev.next = ptr.next;
}
prev = prev.next;
ptr = prev.next;
//c++;
}
}
}
ptr.next = temp;
break;
}
ptr = ptr.next;
c++;
}
}
T getData(int x)
{
int c = 1;
LinkedList<T> l = next;
while(l!=null)
{
if(c==x)
{
T data = l.data;
return (data);
}
l = l.next;
c++;
}
return null;
}
}
class Main
{
public static void main(String []a)
{
LinkedList<Integer> l = new LinkedList<Integer>();
Scanner sc = new Scanner(System.in);
while(true)
{
int x,c;
System.out.print("Enter 1 to insert\nEnter 2 to delete
by Value\nEnter 3 to delete by position\nEnter 4 to display\nEnter
Choice: ");
c = sc.nextInt();
if(c==1)
{
System.out.print("Enter element to insert: ");
x = sc.nextInt();
l.insert(x);
}
else if(c==2)
{
System.out.print("Enter element to delete: ");
x = sc.nextInt();
l.deletep(x);
}
else if(c==3)
{
System.out.print("Enter element to delete: ");
x = sc.nextInt();
l.delete(x);
}
else if(c==4)
{
l.display();
}
else
{
System.out.println("Exiting.......");
break;
}
}
}
}
Output:
Question No.:02
Write a Java program that defines a generic type
ShapeBox that is capable of holding different types of shapes.
The ShapeBox class should allow you to add shapes of different
types (e.g., Circle, Square, Triangle) and provide a method to
calculate the total area of all shapes in the box.
a. Shape - An abstract class representing a shape with an
abstract method double getArea() to calculate the area of the
shape.
b. Circle - A class representing a circle, which is a
subclass of Shape. It should have a constructor that takes the
radius and implements the getArea method to calculate the area
of the circle.
c. Rectangle - A class representing a square, which is a
subclass of Shape. It should have a constructor that takes the
side length and implements the getArea method to calculate the
area of the square.
d. ShapeBox<T> - A generic class that can hold shapes of
any type T that extends the Shape class. It should have meth-
ods to add shapes to the box and calculate the total area of
all shapes in the box.
Your program should output the total area of the shapes in the
box.
Class Diagram:
<<abstract>>
Shape
+getArea(): Double
Rectangle
Circle
+lenght: Double
+radius: Double +breadth: Double
+Circle +Rectangle
+getArea(): Double +getArea(): Double
ShapeBox<T>
+s[]: T
+c: Integer
+ShapeBox
+addShape: void
+calArea(): Double
Code:
import java.util.*;
import java.lang.*;
Circle(double x)
{
radius = x;
}
double getArea()
{
return (Math.PI)*(Math.pow(radius,2));
}
}
Rectangle(double l, double b)
{
length = l;
breadth = b;
}
double getArea()
{
return length*breadth;
}
}
ShapeBox(T []a)
{
s = a;
}
void addShape(T x)
{
s[c++] = x;
}
double calArea()
{
double d = 0;
for(int i =0;i<c;i++)
{
d += s[i].getArea();
}
return d;
}
}
class Main2
{
public static void main(String[]args)
{
else if(c==2)
{
double l,b;
System.out.print("Enter Length: ");
l = sc.nextDouble();
System.out.print("Enter Breadth: ");
b = sc.nextDouble();
s.addShape(new Rectangle(l,b));
}
else if(c==3)
{
System.out.println("Total Area: " + s.calArea());
}
else
{
System.out.println("Exit");
break;
}
}
}
}
Output:
Question No.:03
Write a Java program to perform a sorting operation
on various types of elements using a generic method.
Class Diagram:
Main3
+intArray[]: Integer
+strArray[]: String
-sortArray(T[] a): void
+printArray(T[] a): void
Code:
import java.util.*;
T temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
Output:
Learning Outcomes:
From this exercise, I have learnt to create generic
class and methods and Use the generic class to store and
manipulate elements of different data types.