Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Date: 17.10.

2023
Ex. No: 07

Exercise: 07 Name: HARIKUMAR G


Date: 17.11.2023 Reg No.: 3122225001033

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

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

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)
{

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

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++;
}
}
}

void add(T elt,int x)


{
LinkedList<T> ptr = next;
LinkedList<T> temp = new LinkedList<T>();
temp.data = elt;
int c = 1;
if(x==1)
{
insert(elt);
}
while(ptr != null)
{
if(c == x-1)
{
temp.next = ptr.next;

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

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);

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

}
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:

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

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.

Write a Java program that demonstrates the usage of these


classes by creating a ShapeBox, adding various shapes to it,
and calculating the total area of all the shapes in the box.

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

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.*;

abstract class Shape


{
abstract double getArea();
}

class Circle extends Shape


{
double radius;

Circle(double x)
{
radius = x;
}

double getArea()

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

{
return (Math.PI)*(Math.pow(radius,2));
}
}

class Rectangle extends Shape


{
double length;
double breadth;

Rectangle(double l, double b)
{
length = l;
breadth = b;
}

double getArea()
{
return length*breadth;
}
}

class ShapeBox<T extends Shape>


{
T []s;
int c = 0;

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;
}
}

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

class Main2
{
public static void main(String[]args)
{

ShapeBox<Shape> s = new ShapeBox<Shape>(new Shape[20]);


Scanner sc = new Scanner(System.in);
int c,t = 1;
while(true)
{
System.out.print("\nEnter 1 to add Circle\nEnter 2 to
add Rectangle\nEnter 3 print total Area of the Shapes\nEnter choice: ");
c = sc.nextInt();
if(c==1)
{
double a;
System.out.print("Enter radius: ");
a = sc.nextDouble();
s.addShape(new Circle(a));
}

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;
}

}
}
}

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

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

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

Code:

import java.util.*;

public class Main3 {

public static void main(String[] args) {

Integer[] intArray = {5, 2, 8, 1, 3};


System.out.println("Original Integer Array:");
printArray(intArray);
sortArray(intArray);
System.out.println("Sorted Integer Array:");
printArray(intArray);

String[] strArray = {"banana", "apple", "orange", "grape",


"kiwi"};
System.out.println("\nOriginal String Array:");
printArray(strArray);
sortArray(strArray);
System.out.println("Sorted String Array:");
printArray(strArray);
}

private static <T extends Comparable<T>> void sortArray(T[] a){


int n = a.length;

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {
if (a[j].compareTo(a[j + 1]) > 0) {

T temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

private static <T> void printArray(T[] a) {


for (T element : a) {
System.out.print(element + " ");
}
System.out.println();
}

Department of Computer Science and Engineering


Date: 17.10.2023
Ex. No: 07

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.

Department of Computer Science and Engineering

You might also like