[go: up one dir, main page]

0% found this document useful (0 votes)
19 views6 pages

JAVA 2.1 Navi

Uploaded by

Harkaran Singh
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)
19 views6 pages

JAVA 2.1 Navi

Uploaded by

Harkaran Singh
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/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.1

Student Name: Aryan Vishwa UID:22BCS80031


Branch: CSE Section/Group:CC-634-A
Semester: 6th Date of Performance:09-02-24
Subject Name: Project Based Learning Subject Code: 21CSH-319
Using Java

• 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.

• Objective: This cards game consists 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 symbol 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. For each symbol print
all the card details, number of cards and their sum respectively.

• Input/Apparatus Used: PC, Visual Studio Code.

• Procedure/Algorithm/Pseudocode:
1. Import the Scanner class from the java.util package.
2. Define a custom exception class named InvalidValueException that
extends Exception.
3. Define an abstract class named Account with an abstract method
calculateInterest().
4. Define concrete classes SavingsAccount, FDAccount, and RDAccount
that extend the Account class.
5. Implement the calculateInterest() method in each concrete class to
calculate interest based on specific criteria.
6. Define a class named InterestCalculator.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

7. Inside the InterestCalculator class:


a. Create a main method.
b. Create an instance of the Scanner class to read user input.
c. Use a while loop to repeatedly prompt the user for options until they
choose to exit.
d. Display a menu of options for the user to choose from.
e. Use a switch statement to handle different user choices:
f. For option 1, prompt the user for the average amount in their savings
account, create a SavingsAccount object, calculate interest, and display
the result.
g. For option 2, prompt the user for FD amount, days, and age, create an
FDAccount object, calculate interest, and display the result.
h. For option 3, prompt the user for RD amount, days, and age, create an
RDAccount object, calculate interest, and display the result.
i. For option 4, close the Scanner and exit the program.
j. For invalid choices or input, display appropriate error messages.
8. Handle exceptions such as InvalidValueException and
InputMismatchException by catching them in try-catch blocks and
displaying error messages.
9. Properly close the Scanner object to prevent resource leaks.
10. Compile and run the program.

• Code:
import java.util.*;

class Card { private String symbol; private int number;

public Card(String symbol, int number) { this.symbol = symbol;


this.number = number;
}

public String getSymbol() { return symbol;


}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public int getNumber() {

return number;
}

@Override public String toString()


{ return symbol + "
" + number;
}
}

public class CardCollection { public static void main(String[]


args) { Scanner scanner = new
Scanner(System.in); System.out.println("Enter
Number of Cards: "); int numCards = scanner.nextInt();
Map<String, List<Card>> cardMap = new TreeMap<>();

for (int i = 0; i < numCards; i++) {


System.out.println("Enter card " + (i + 1) + ": "); String symbol =
scanner.next(); int number = scanner.nextInt(); Card card = new
Card(symbol, number);

if (cardMap.containsKey(symbol)) {
cardMap.get(symbol).add(card);
} else {
List<Card> cards = new ArrayList<>(); cards.add(card);
cardMap.put(symbol, cards);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

System.out.println("Distinct Symbols are :"); for (String symbol :


cardMap.keySet()) { System.out.print(symbol + " ");

}
System.out.println();

for (Map.Entry<String, List<Card>> entry : cardMap.entrySet()) {


System.out.println("Cards in " + entry.getKey() + " Symbol"); int sum
= 0; for (Card card : entry.getValue()) {
System.out.println(card); sum
+= card.getNumber();
}
System.out.println("Number of cards: " + entry.getValue().size());
System.out.println("Sum of Numbers: " + sum);
}

scanner.close();
}
}
Result/Output:
1. Number of Cards Entered and its Sum
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning Outcomes:
1. Demonstrates efficient use of data structures like Map for grouping data.
2. Illustrates dynamic input handling and processing.
3. Highlights importance of iterating over collections for data manipulation.
4. Shows sorting behavior with TreeMap for alphabetical order.
5. Encourages encapsulation and object-oriented design principles with
Card class.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like