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

Assigment-02 Generics in Java.docx

The document outlines an assignment for a generic class 'TextEditor<T>' in Java, designed to manage objects of any type using an array-based approach. It includes methods for adding, displaying, and undoing actions on the stored data, along with implementation requirements such as supporting various data types and providing a menu-driven interface. An example scenario demonstrates the expected input and output for user interactions with the text editor.

Uploaded by

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

Assigment-02 Generics in Java.docx

The document outlines an assignment for a generic class 'TextEditor<T>' in Java, designed to manage objects of any type using an array-based approach. It includes methods for adding, displaying, and undoing actions on the stored data, along with implementation requirements such as supporting various data types and providing a menu-driven interface. An example scenario demonstrates the expected input and output for user interactions with the text editor.

Uploaded by

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

North South University

Department of Electrical and Computer Engineering


CSE 215 Lab: Programming Language II Lab

Assignment: Generics in Java

<<generic>> TextEditor<T>

- data: T[] // Array to store data


- capacity: int // Maximum number of items
- size: int // Current number of items

+ TextEditor(capacity: int)
+ add(obj: T): void
+ display(): void
+ undo(): void

Description: Generic Class: TextEditor<T>

○ This is a generic class that allows storing and managing objects of any type
(T) in a text editor.

○ Fields:

■ data: A generic array (T[]) to store the objects.


■ capacity: Maximum number of objects the editor can hold.
■ size: Tracks the number of objects currently in the editor.

○ Methods:

■ TextEditor(capacity: int): Constructor to initialize the


array and capacity.
■ add(obj: T): void: Adds an object to the editor. If full, it
prints "Editor is full".
■ display(): void: Displays all the objects in the editor. If
empty, it prints "Editor is empty".

■ undo(): void: Removes the last added object. If empty, it prints


"Nothing to undo".
Implementation Requirements:

1. Add Data: Allow users to add data of any type (Integer, String, Float,
Double) into the editor.
2. Display Data: Display all objects stored in the editor.
3. Undo Last Action: Remove the most recently added object (undo functionality).
4. Use Generics:
○ The class should work with any data type.
5. Array-Based Design:
○ No external libraries or data structures (e.g., ArrayList, Stack). Use a
normal array.
6. Menu-Driven Program:
○ Provide options to add data, display all objects, undo the last action, and
exit.

Example Scenario: Create the generic class stated above. Write a


Test Class program, and test the following Example Scenario

Sample Input Expected Output

Welcome to the Generic Text Editor!


1. Add Data
2. Display Data
3. Undo Last Action
4. Exit

Enter your choice: 1


Select data type (1. Integer, 2. String, 3.
Float, 4. Double): 2
Enter data: Hello, World! Data added successfully!

Enter your choice: 1


Select data type (1. Integer, 2. String, 3.
Float, 4. Double): 1
Enter data: 42 Data added successfully!
Enter your choice: 1
Select data type (1. Integer, 2. String, 3.
Float, 4. Double): 4
Enter data: 3.14 Data added successfully!

Enter your choice: 2 Stored Data: [Hello, World!, 42, 3.14]

Enter your choice: 3


Last action undone!

Enter your choice: 2 Stored Data: [Hello, World!, 42]

Enter your choice: 1


Select data type (1. Integer, 2. String, 3.
Float, 4. Double): 3
Enter data: 2.71 Data added successfully!

Enter your choice: 2 Stored Data: [Hello, World!, 42, 2.71]

Enter your choice: 4 Exiting the Text Editor. Goodbye!

You might also like