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

java exp 4

The document outlines an experiment for a Java program that collects and stores cards, allowing users to search for cards by symbol using the Collection interface. It includes an algorithm for the program's functionality, a sample code implementation, and expected learning outcomes related to applying concepts, analyzing information, creating original work, communicating effectively, and collaborating in group projects. The program demonstrates basic Java programming skills and user interaction through a console application.

Uploaded by

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

java exp 4

The document outlines an experiment for a Java program that collects and stores cards, allowing users to search for cards by symbol using the Collection interface. It includes an algorithm for the program's functionality, a sample code implementation, and expected learning outcomes related to applying concepts, analyzing information, creating original work, communicating effectively, and collaborating in group projects. The program demonstrates basic Java programming skills and user interaction through a console application.

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.4

Student Name: Sushmit Singh UID: 22BCS17177


Branch: CSE Section: IOT_630-B
Semester: 6th DOP: 13/01/25
Subject: Java Subject Code:22CSH-359

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.

Algorithm:

1. Start
2. Create a collection to store cards (e.g., List<Card>).
3. Add sample cards to the collection:
o Each card has a symbol (e.g., Hearts, Diamonds) and a value (e.g., Ace,
King, 10).
4. Display all the cards in the collection:
o Iterate through the collection and print each card.
5. Prompt the user to enter a symbol to search (e.g., Hearts, Diamonds, Clubs,
Spades).
6. Read the symbol input from the user.
7. Search the collection for cards with the given symbol:
o Iterate through the collection.
o For each card, check if its symbol matches the user's input (case-
insensitive).
o If a match is found, add the card to the result list or print it directly.
8. If no cards are found for the given symbol:
o Display a message: "No cards found with symbol '<symbol>'."
9. If cards are found:
o Display all the cards with the given symbol.
10.End
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Code:

import java.util.*;

class Card {
private String symbol;
private String value;

public Card(String symbol, String value) {


this.symbol = symbol;
this.value = value;
}

public String getSymbol() {


return symbol;
}

public String getValue() {


return value;
}

@Override
public String toString() {
return value + " of " + symbol;
}
}

public class CardCollection {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

List<Card> cards = new ArrayList<>();

cards.add(new Card("Hearts", "Ace"));


cards.add(new Card("Diamonds", "King"));
cards.add(new Card("Clubs", "Queen"));
cards.add(new Card("Spades", "Jack"));
cards.add(new Card("Hearts", "10"));
cards.add(new Card("Diamonds", "9"));
cards.add(new Card("Clubs", "8"));
cards.add(new Card("Spades", "7"));

System.out.println("All Cards in Collection:");


for (Card card : cards) {
System.out.println(card);
}

System.out.print("\nEnter a symbol to find all cards (e.g., Hearts, Diamonds, Clubs, Spades):
");
String symbol = scanner.nextLine().trim();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

System.out.println("\nCards with symbol '" + symbol + "':");


boolean found = false;
for (Card card : cards) {
if (card.getSymbol().equalsIgnoreCase(symbol)) {
System.out.println(card);
found = true;
}
}

if (!found) {
System.out.println("No cards found with symbol '" + symbol + "'.");
}

scanner.close();
}

Output:

Learning Outcomes:

1. Demonstrate: Apply key concepts to real-world scenarios to showcase understanding.


2. Analyze: Critically evaluate information, identify patterns, and draw meaningful
conclusions.
3. Create: Develop original work, including presentations, reports, or projects, to exhibit
comprehension and skills.
4. Communicate: Convey ideas and findings effectively through oral and written
communication.
5. Collaborate: Contribute to group projects and exhibit strong teamwork capabilities in a
collaborative environment.

You might also like