java exp 4
java exp 4
Experiment 1.4
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;
@Override
public String toString() {
return value + " of " + symbol;
}
}
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
if (!found) {
System.out.println("No cards found with symbol '" + symbol + "'.");
}
scanner.close();
}
Output:
Learning Outcomes: