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

Java 2.1 - Yuvi.2.3

Uploaded by

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

Java 2.1 - Yuvi.2.3

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.1

Student Name: Yuvraj Singh UID: 21BCS10849


Branch: CSE Section/Group: FL-604_A
Semester: 6th Date of Performance:09/02/24
Subject Name: Java with Lab Subject Code: 21CSH-319

1. Aim: Create a program to collect and store all the cards to assist the users in finding
all the cards in a given symbol using Collection interface

2. Objective: Develop a program that utilizes the Collection interface to


efficiently collect and store all cards, enabling users to easily find and
retrieve all cards associated with a given symbol.

3. Input/Apparatus Used:
 Hardware Requirements: - Minimum 384MB RAM, 100 GB hard Disk, processor with 2.1
MHz
 Software Requirements: - Eclipse, NetBeans, IntelliJ, etc

4. Procedure: To create a program that collects and stores all the cards to assist users
in finding all the cards in a given symbol using a Collection interface, you can
follow these steps:
I. Define the Card class: Create a class to represent a single card. Each card
should have attributes like a symbol and a value.
II. Implement the Collection interface: Define an interface with methods for
adding cards, removing cards, and finding cards by symbol.
III. Create a concrete implementation of the Collection interface: Implement the
methods defined in the interface using appropriate data structures like lists or
dictionaries to store the cards.
IV. Provide a user interface: Create a user interface to interact with the program.
Users should be able to add cards, remove cards, and search for cards by symbol.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Code:
import java.util.*;
class Card {
String symbol;
int number;
public Card(String symbol, int number) {
this.symbol = symbol;
this.number = number;
}
@Override
public String toString() {
return symbol + " " + number;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of Cards:");
int numCards = scanner.nextInt();
scanner.nextLine();
Map<String, List<Card>> cardsMap = new TreeMap<>();
for (int i = 0; i < numCards; i++) {
System.out.println("Enter card " + (i + 1) + ":");
String symbol = scanner.nextLine();
int number = scanner.nextInt();
scanner.nextLine
Card card = new Card(symbol, number);
cardsMap.computeIfAbsent(symbol, k -> new ArrayList<>()).add(card);
}
System.out.println("Distinct Symbols are :");
for (String symbol : cardsMap.keySet()) {
System.out.print(symbol + " ");
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

System.out.println();
for (Map.Entry<String, List<Card>> entry : cardsMap.entrySet()) {
System.out.println("Cards in " + entry.getKey() + " Symbol");
List<Card> cards = entry.getValue();
int sum = 0;
for (Card card : cards) {
System.out.println(card);
sum += card.number;
}
System.out.println("Number of cards : " + cards.size());
System.out.println("Sum of Numbers : " + sum);
}
}
}
6. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

7. Learning Outcomes:
 Understanding of the Collection interface
 Data structure selection: Students should be able to select an appropriate data
structure (e.g., ArrayList, HashSet) to store the cards efficiently.
 Symbol-based retrieval: Students should be able to implement functionality to
find all cards in the collection with a given symbol.
 Implementation of card collection: Students should be able to implement the
logic for collecting and storing cards in the chosen data structure.

You might also like