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

YugJava4

The document outlines an experiment for a computer science course where students create a program to collect and store card details based on symbols. The program uses a map for efficient data organization, allowing users to add cards, find them by symbol, and display all cards. Learning outcomes include understanding maps for data storage, grouping data, handling user input, and sorting/displaying structured data.

Uploaded by

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

YugJava4

The document outlines an experiment for a computer science course where students create a program to collect and store card details based on symbols. The program uses a map for efficient data organization, allowing users to add cards, find them by symbol, and display all cards. Learning outcomes include understanding maps for data storage, grouping data, handling user input, and sorting/displaying structured data.

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 4
Student Name: Yug UID: 22BCS14252
Branch: BE-CSE Section/Group: 639-A
Semester:6th Date of Performance:14/02/25
Subject Name: PBLJ Subject Code: 22CSH-359

1. Aim : Write a program to collect and store all the cards to assist the
users in finding all the cards in a given symbol.This cards game
consist of N number of cards. Get N number of cards details from
the user and store the values in Card object with the attributes
symbol and Number. Store all the cards in a map with symbols as
its key and list of cards as its value. Map is used here to easily
group all the cards based on their symbol. Once all the details are
captured print all the distinct symbols in alphabetical order from
the Map.

2. Objective : This program collects and stores N cards, grouping them


by symbol in a map for easy retrieval. It displays distinct symbols in
alphabetical order along with their associated cards, total count, and
sum of numbers, ensuring efficient organization and user-friendly
output.

3. Code
import java.util.*;

class Card {
String symbol;
String name;

Card(String symbol, String name) {


this.symbol = symbol;
this.name = name;
}

public String toString() {


return name + " (" + symbol + ")";
}
}

public class CardCollection {


static Collection<Card> cards = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public static void main(String[] args) {


while (true) {
System.out.println("1.Add 2.Find by Symbol 3.Show All 4.Exit");
int choice = sc.nextInt();
switch (choice) {
case 1 -> addCard();
case 2 -> findBySymbol();
case 3 -> cards.forEach(System.out::println);
case 4 -> { return; }
default -> System.out.println("Invalid");
}
}
}

static void addCard() {


System.out.print("Enter Symbol: ");
String symbol = sc.next();
sc.nextLine();
System.out.print("Enter Name: ");
String name = sc.nextLine();
cards.add(new Card(symbol, name));
}

static void findBySymbol() {


System.out.print("Enter Symbol: ");
String symbol = sc.next();
cards.stream().filter(c ->
c.symbol.equals(symbol)).forEach(System.out::println);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Code

5. Learning Outcomes

• Understand how to use maps (dictionaries) for efficient data storage and
retrieval.

• Learn to group and organize data based on a key attribute.

• Gain experience in handling user input and storing objects dynamically.


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Develop skills in sorting and displaying structured data in a meaningful

You might also like