This program collects, stores,
and processes student marks.
It demonstrates basic
input/output, data storage using
ArrayList, and simple calculations
like the average.
Unlike arrays, ArrayList can grow
as needed.
No need to know the size in
advance (flexible and memory
efficient).
for loop used to:
Collect marks from the user.
Display each student's mark.
Demonstrates use of both
indexed for-loops and
enhanced for-loops.
Output:
How many students? 4
Enter mark for student 1: 85
Enter mark for student 2: 90
Enter mark for student 3: 78
Enter mark for student 4: 92
Student Marks:
Student 1: 85
Student 2: 90
Student 3: 78
Student 4: 92
Average Mark: 86.25
Demonstrates dynamic array usage via
ArrayList<String> books.
Benefits:
Automatically resizes.
Easy to add, remove, and access elements.
No need to predefine size like a regular
array.
Easy to add, remove, and access elements.
No need to predefine size like a regular array.
-Uses a for loop and [Link](i) to display the list of books with
numbering.
Output for 1.a)
How many students? 4
Enter mark for student 1: 85
Enter mark for student 2: 90
Enter mark for student 3: 78
Enter mark for student 4: 92
Student Marks:
Student 1: 85
Student 2: 90
Student 3: 78
Student 4: 92
Average Mark: 86.25
Output:
How many books to add? 3
Enter title of book 1: The Alchemist
Enter title of book 2: Harry Potter
Enter title of book 3: To Kill a Mockingbird
Books in Library:
1. The Alchemist
2. Harry Potter
3. To Kill a Mockingbird
Enter a book title to search: harry potter
Book found: Harry Potter
1.
A Purpose of the Program
simple program to manage and store employee
Anames
simple with
program
their to manage and store employee names
salaries.
with their salaries.
Allowsusers
Allows userstotoinput,
input, display,
display, and
and search
search employee
employee salary
salary data.
data.
HashMap<String, Integer> salaries = new HashMap<>();
Stores key-value pairs:
Key: Employee name (String)
Value: Salary (Integer)
Benefits:
Fast search using keys.
No duplicates in keys (employee names must be
unique).
Efficient for lookups and storage.
Advantages:
-Constant time complexity for insertion and searching.
-Simplifies employee data management.
Output:
How many employees? 3
Enter employee name: Alice
Enter salary: 50000
Enter employee name: Bob
Enter salary: 45000
Enter employee name: Charlie
Enter salary: 55000
Employee Salaries:
Alice - ₹50000
Bob - ₹45000
Charlie - ₹55000
Search employee name: Bob
Salary: ₹45000
A simple product catalog system that allows users to:
-Add product names
-Prevent duplicates
-Display all products
-Search for a product
HashSet<String> products = new HashSet<>();
Stores unique product names (no duplicates allowed).
Benefits:
-Automatically prevents duplicate entries
-Fast insertion and lookup (constant time on average)
-No specific order of elements
User enters product names.
[Link](product) returns false if the
product already exists.
Uses [Link](search) to check if the
product exists.
Efficient lookup with average time complexity
O(1).
Output:
How many products to add? 4
Enter product name: Keyboard
Product added.
Enter product name: Mouse
Product added.
Enter product name: Monitor
Product added.
Enter product name: Mouse
Duplicate! Product already exists.
Product Catalog:
- Keyboard
- Mouse
- Monitor
Enter product name to search: Mouse
Product found.
A simple program that takes a list of integers as input and
removes all odd numbers from it.
Demonstrates how to safely modify a collection while
iterating using an Iterator.
ArrayList<Integer> numbers = new ArrayList<>();
Used to store a dynamic list of integers.
Advantages:
-Resizable
-Indexed access
-Compatible with iterators
Why use Iterator?
Removing elements in a standard for loop can cause
ConcurrentModificationException.
[Link]() is the safe way to remove elements
during iteration.
Output:
How many numbers? 6
Enter 6 numbers:
11
42
37
28
9
60
List after removing odd numbers:
42 28 60
A basic program to create and display an attendance list for
students.
Collects student names and prints the full list.
ArrayList<String> attendance = new ArrayList<>();
Stores names in a dynamic, resizable list.
Advantages:
Easy to add names without needing to predefine size.
Maintains insertion order.
Uses Scanner to read:
-Number of students (n)
-Student names in a loop.
[Link]() is used to read full names and handle
newline issues.
Output:
How many students are in the class? 3
Enter student names:
Alice
Bob
Charlie
Attendance List:
Alice
Bob
Charlie
Demonstrates how to store, manage, and sort a list
of custom objects (in this case, people) by a specific
attribute (age).
Defines a class Person with:
-Attributes: name (String), age (int)
-Constructor to initialize values
-toString() method for readable output format
Uses ArrayList<Person> to store multiple person objects.
Uses [Link]() with an anonymous inner class
implementing Comparator<Person>.
Output:
People sorted by age:
Bob - Age: 20
John - Age: 25
Alice - Age: 30
Demonstrates autoboxing and unboxing between
Java primitive types and their wrapper classes.
Java provides wrapper classes for each primitive type:
-int Integer
-double Double
-Others: Byte, Short, Long, Float, Character, Boolean
Useful when working with collections, generics, or needing
object methods.
Definition: Automatic conversion of a primitive value to its
corresponding wrapper class.
Java provides wrapper classes for each
primitive type:
int Integer
Definition: Automatic conversion of a wrapper class
object to its corresponding primitive
double type.
Double
Others: Byte, Short, Long, Float,
Character, Boolean
Useful when working with collections,
generics, or needing object methods.
Output:
Autoboxing (int to Integer): 100
Unboxing (Integer to int): 200
Autoboxing (double to Double): 10.5
Unboxing (Double to double): 20.5
Demonstrates the use of generics in Java for:
-Creating a generic class
-Defining a generic method
-Applying generic logic (e.g., swapping elements)
Definition:
Generics allow a class or method to operate on objects
of various types while providing compile-time type safety.
-Example: Box<T> can store any type like Integer, String,
Double, etc.
Generic Swap Method:
-Works with any type of array: Integer[], String[], Double[],
etc.
-Swaps two elements at given indices.
-Promotes code reusability without rewriting for every
data type.
Output:
Box value: 100
Array before swap:
10 20 30 40 50
Array after swap:
10 40 30 20 50
To demonstrate the use of bounded type parameters in Java
generics.
To find the maximum of three values of any type that
implements Comparable.
T extends Comparable<T> means:
-Type T must implement the Comparable interface.
-This ensures we can call compareTo() on the values.
Output:
Maximum Integer: 20
Maximum Double: 20.5
Maximum String: Cherry
Demonstrates the use of:
-Generic interface
-Generic class
-Generic constructor
-Allows the creation of flexible Pair objects that hold different
data types.
Generic Interface:
Enables defining behavior for a key-value pair without
fixing the types in advance.
Generic Constructor:
It initializes the key and value fields.
Output:
Key: 1, Value: Apple
Key: Price, Value: 19.99
Key: Name, Value: John
-To demonstrate the use of Java's TreeMap collection.
-Store menu items and their prices.
-Allow searching for an item’s availability and price.
Why do we use Tree Map:
To demonstrate the use of Java's TreeMap
collection.
Store menu items and their prices.
Allow searching for an item’s availability and
price.
Iterating Entries:
Uses entrySet() to display all menu
items sorted alphabetically.
Searching:
Uses containsKey() method to check if
an item exists.
Output:
Welcome to the Canteen! Here is the menu:
Burger: ₹80.0
Coffee: ₹40.0
Juice: ₹30.0
Pasta: ₹120.0
Pizza: ₹150.0
Sandwich: ₹50.0
Enter the item you want to search for: Pasta
Pasta is available for ₹120.0
By:
PRAJNA TRS
24BCE7992