0% found this document useful (0 votes)
29 views5 pages

20BCS4919 - Sahul Java 2.3

This document describes a Java program that performs basic operations on a list of String objects. The program allows the user to insert, search, delete, and display items in the list using a menu-driven interface. It implements methods to search for an item in the list and return its index. The main method uses a switch statement to call the appropriate method based on the user's choice. Key data structures used include ArrayList to store the list and Scanner for user input.

Uploaded by

Sahul Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

20BCS4919 - Sahul Java 2.3

This document describes a Java program that performs basic operations on a list of String objects. The program allows the user to insert, search, delete, and display items in the list using a menu-driven interface. It implements methods to search for an item in the list and return its index. The main method uses a switch statement to call the appropriate method based on the user's choice. Key data structures used include ArrayList to store the list and Scanner for user input.

Uploaded by

Sahul Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment Title 2.

3
Student Name: Sahul Kumar Parida UID: 20BCS4919
Branch: CSE Section/Group: WM-904/B
Semester: 5th
Subject Name: Project Based Learning using Java
Subject Code: 20CSP-321

1. Aim/Overview of the practical:

Write a Program to perform the basic operations like insert, delete, display and
search in list. List contains
String object items where these operations are to be performed.
2. Software/Hardware Requirements:

Windows
BlueJ (Java IDE)
JRE (Java Runtime Environment)

3. Steps for experiment/practical/Code:

import java.util.*;
class Worksheet
{
public static int search(ArrayList<String> items, String name)
{
for (int i = 0; i < items.size(); i++)
{
if (items.get(i) == name)
{
return i;
}
}
return -1;
}

public static void Main(String[] args)


{
Scanner sc=new Scanner(System.in);
String name;
int index;
int ch = -1;
ArrayList<String> items = new ArrayList<>();
while (ch != 5)
{
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Display");
System.out.println("5. Exit");
System.out.println("Enter your choice: ");
ch = sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter the item to be inserted: ");
name = sc.nextLine();
items.add(name);
System.out.println("Inserted successfully");
break;
case 2:
System.out.println("Enter the item to search: ");
name = sc.nextLine();
index = search(items, name);
if (index == -1)
{
System.out.println("Item not found in the list.");
}
else
{
System.out.println("Item found in the list...");
}
break;
case 3:
System.out.println("Enter the item to delete: ");
name = sc.nextLine();
index = search(items, name);
if (index == -1)
{
System.out.println("Item not found in the list.");
}
else
{
items.remove(index);
System.out.println("Deleted successfully.");
}
break;
case 4:
System.out.println("The Items in the list are: ");
for (int i = 0; i < items.size(); i++)
{
System.out.println(items.get(i));
}
break;
case 5:
//exit
break;
}
}
}
}

4. Result/Output/Writing Summary:

Learning outcomes (What I have learnt):


1. Java Classes
2. Java Switch Case Statements
3. Arrays, Strings and ArrayLists
4. Java If-Else Statements

You might also like