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

Questions On Generic Types in Java

The document describes an exercise on generic types in Java. It contains 3 questions asking to: 1) Create a generic linked list class that can hold different data types and define methods to add, remove and get data from nodes. 2) Create generic shape classes including a ShapeBox that can hold different shape types and calculate the total area. 3) Write a generic method to perform sorting on different element types.

Uploaded by

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

Questions On Generic Types in Java

The document describes an exercise on generic types in Java. It contains 3 questions asking to: 1) Create a generic linked list class that can hold different data types and define methods to add, remove and get data from nodes. 2) Create generic shape classes including a ShapeBox that can hold different shape types and calculate the total area. 3) Write a generic method to perform sorting on different element types.

Uploaded by

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

‭SSN College of Engineering‬

‭Department of Computer Science and Engineering‬


‭II Year CSE (III Semester)‬
‭Academic Year 2023-24‬
‭UCS2313 – Object Oriented Programming Lab‬

‭Exercise 7: Generic Types‬

‭Objective‬‭:‬
‭●‬ ‭To implement generic types – generic classes and methods‬

‭Learning Outcomes‬‭:‬
‭●‬ ‭Learning to create a generic class and method‬
‭●‬ ‭Use the generic class to store/manipulate elements of different data types‬

‭Best Practices:‬
‭●‬ ‭Class Diagrams‬
‭●‬ ‭Proper naming conventions‬
‭●‬ ‭Comments at proper places‬
‭●‬ ‭Prompt messages during reading input and displaying output‬
‭●‬ ‭Modularity‬
‭●‬ ‭Coverage of test cases‬

‭Questions:‬
‭1.‬ ‭Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭that‬ ‭defines‬ ‭a‬ ‭generic‬ ‭type‬ ‭MyLinkedList‬‭.‬ ‭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‬‭position.‬‭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 deleted.‬
‭c.‬ ‭Get data‬‭: The data at a given position should be returned.‬

‭2.‬ W
‭ rite‬ ‭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‬
s‭ hapes‬‭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.‬‭It‬‭should‬‭implement‬‭the‬
‭getArea‬‭method to calculate the area of the circle.‬
‭c.‬ ‭Rectangle‬‭-‬‭A‬‭class‬‭representing‬‭a‬‭rectangle,‬‭which‬‭is‬‭a‬‭subclass‬‭of‬‭Shape‬‭.‬
‭It‬ ‭should‬ ‭have‬ ‭a‬ ‭constructor‬ ‭that‬ ‭takes‬ ‭the‬ ‭length‬ ‭and‬ ‭width.‬ ‭It‬ ‭should‬
‭also implement the‬‭getArea‬‭method to calculate the area of the rectangle.‬
‭d.‬ ‭ShapeBox<T>‬ ‭-‬ ‭A‬ ‭generic‬ ‭class‬ ‭that‬ ‭can‬ ‭hold‬ ‭shapes‬ ‭of‬ ‭any‬ ‭type‬ ‭T‬ ‭that‬
‭extends‬‭the‬‭Shape‬‭class.‬‭It‬‭should‬‭have‬‭methods‬‭to‬‭add‬‭shapes‬‭to‬‭the‬‭box‬
‭and calculate the total area of all shapes in the box.‬

‭ rite a Java program that demonstrates the usage of these classes by creating‬
W
‭a‬‭ShapeBox‬‭, adding various shapes to it, and calculating the total area of all the‬
‭shapes in the box. Your program should output the total area of the shapes in‬
‭the box.‬

‭An example of how Class‬‭Main‬‭should look like is shown‬‭below:‬

‭hapeBox<Shape> shapeBox = new ShapeBox<>();‬


S
‭// get‬ ‭ radius‬‭as input‬
shapeBox.addShape(new Circle(radius));‬

‭// get‬ ‭length, width‬‭as input‬
shapeBox.addShape(new Rectangle(length,width));‬

‭// get‬ ‭
length, width‬‭as input‬
shapeBox.addShape(new Rectangle(length,width));‬

shapeBox.addShape(new Rectangle(16.0,22.0));‬

‭ouble totalArea = shapeBox.calculateTotalArea();‬


d
System.out.println("Total area of shapes in the box: " + totalArea);‬

‭3.‬ ‭Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭to‬ ‭perform‬ ‭a‬ ‭sorting‬ ‭operation‬ ‭on‬ ‭various‬ ‭types‬ ‭of‬
‭elements using a generic method.‬

You might also like