JAVA LAB PROGRAMS
1. Implement an application that utilizes enumeration to represent days of
the week and performs operations such as checking for a weekend day
and calculating the next working day.
CODE:
public class prg1 {
// Define enum for days of the week
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
// Check if it's a weekend
public boolean isWeekend() {
return this == SATURDAY || this == SUNDAY;
}
// Get the next working day
public Day nextWorkingDay() {
switch (this) {
case FRIDAY:
case SATURDAY:
case SUNDAY:
return MONDAY;
default:
return [Link]()[[Link]() + 1];
}
}
}
// Main method
public static void main(String[] args) {
Day today = [Link];
[Link]("Today is: " + today);
if ([Link]()) {
[Link]("It's a weekend!");
} else {
Day nextWorkingDay = [Link]();
[Link]("The next working day is: " + nextWorkingDay);
}
}
}
2. a. Implement a program that showcases different data structures from
the Java collections framework, such as ArrayList, LinkedList, HashSet,
and TreeMap, to perform common operations like insertion, deletion,
and retrieval.
CODE:
import [Link].*;
public class prg2 {
public static void main(String[] args) {
//ArrayList
[Link]("=== ArrayList ===");
ArrayList<String> arrayList = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Banana");
[Link]("ArrayList: " + arrayList);
[Link]("Element at index 1: " + [Link](1 - 1)); //
index pointing to 0th index
//LinkedList
[Link]("\n=== LinkedList ===");
LinkedList<String> linkedList = new LinkedList<>();
[Link]("Dog");
[Link]("Elephant");
[Link]("Cat");
[Link]();
[Link]("LinkedList: " + linkedList);
[Link]("First Element: " + [Link]());
//HashSet
[Link]("\n=== HashSet ===");
HashSet<Integer> hashSet = new HashSet<>();
[Link](10);
[Link](20);
[Link](10); // Duplicate will not be added
[Link](20);
[Link]("HashSet: " + hashSet);
[Link]("Contains 10? " + [Link](10));
// TreeMap
[Link]("\n=== TreeMap ===");
TreeMap<Integer, String> treeMap = new TreeMap<>();
[Link](3, "Three");
[Link](1, "One");
[Link](2, "Two");
[Link](2);
[Link]("TreeMap: " + treeMap);
[Link]("Value for key 1: " + [Link](1));
}
}