0% found this document useful (0 votes)
1 views3 pages

Java Lab Programs

The document contains two Java lab programs. The first program uses an enumeration to represent days of the week, allowing checks for weekends and calculating the next working day. The second program demonstrates various data structures from the Java collections framework, including ArrayList, LinkedList, HashSet, and TreeMap, showcasing operations like insertion, deletion, and retrieval.

Uploaded by

sujaltalekar2091
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Java Lab Programs

The document contains two Java lab programs. The first program uses an enumeration to represent days of the week, allowing checks for weekends and calculating the next working day. The second program demonstrates various data structures from the Java collections framework, including ArrayList, LinkedList, HashSet, and TreeMap, showcasing operations like insertion, deletion, and retrieval.

Uploaded by

sujaltalekar2091
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 Day.values()[this.ordinal() + 1];
}
}
}
// Main method
public static void main(String[] args) {
Day today = Day.THURSDAY;

System.out.println("Today is: " + today);

if (today.isWeekend()) {
System.out.println("It's a weekend!");
} else {
Day nextWorkingDay = today.nextWorkingDay();
System.out.println("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 java.util.*;

public class prg2 {


public static void main(String[] args) {

//ArrayList
System.out.println("=== ArrayList ===");
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Cherry");
arrayList.remove("Banana");
System.out.println("ArrayList: " + arrayList);
System.out.println("Element at index 1: " + arrayList.get(1 - 1)); //
index pointing to 0th index

//LinkedList
System.out.println("\n=== LinkedList ===");
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Dog");
linkedList.add("Elephant");
linkedList.addFirst("Cat");
linkedList.removeLast();
System.out.println("LinkedList: " + linkedList);
System.out.println("First Element: " + linkedList.getFirst());

//HashSet
System.out.println("\n=== HashSet ===");
HashSet<Integer> hashSet = new HashSet<>();
hashSet.add(10);
hashSet.add(20);
hashSet.add(10); // Duplicate will not be added
hashSet.remove(20);
System.out.println("HashSet: " + hashSet);
System.out.println("Contains 10? " + hashSet.contains(10));

// TreeMap
System.out.println("\n=== TreeMap ===");
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(3, "Three");
treeMap.put(1, "One");
treeMap.put(2, "Two");
treeMap.remove(2);
System.out.println("TreeMap: " + treeMap);
System.out.println("Value for key 1: " + treeMap.get(1));
}
}

You might also like