0% found this document useful (0 votes)
15 views

Java LAB 6th Nov

The document contains 8 code snippets involving ArrayLists and other Java collection classes. The snippets demonstrate various operations on lists such as adding/removing elements, searching, filtering, and more. Overall the snippets show how to perform common list operations in Java.
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)
15 views

Java LAB 6th Nov

The document contains 8 code snippets involving ArrayLists and other Java collection classes. The snippets demonstrate various operations on lists such as adding/removing elements, searching, filtering, and more. Overall the snippets show how to perform common list operations in Java.
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/ 20

Q1)

import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] s1 = s.split(" ");
Map<String, Integer> freqMap = new HashMap<>();
for (int i = 0; i < s1.length; i++) {
if (freqMap.containsKey(s1[i])) {
Integer count = freqMap.get(s1[i]);
freqMap.put(s1[i], count + 1);
} else {
freqMap.put(s1[i], 1);
}
}
System.out.println(freqMap.toString());
}
}
Q2)

import java.util.*;

public class Main {


static Scanner console = new Scanner(System.in);

static void createList(List list) {


console.nextLine();
System.out.println("Enter numbers in separate lines for the list.
Press enter on finishing entry");
String s = console.nextLine();
while (s.length() > 0) {
list.add(Integer.parseInt(s));
s = console.nextLine();
}
}

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();

int option;
do {
System.out.println("Menu ");
System.out.println("1. Create List");
System.out.println("2. Display List");
System.out.println("3. Search an Item in the List");
System.out.println("4. Sort the List");
System.out.println("5. Remove an element from the list");
System.out.println("6. Exit");
System.out.print("Select an Option : ");
option = console.nextInt();

switch (option) {
case 1:
createList(list);
break;
case 2:
System.out.println(list.toString());
break;
case 3:
System.out.print("Enter the number you want to search: ");
int item = console.nextInt();
int index = list.indexOf(item);
if (index == -1) {
System.out.println("Item not found");
} else {
System.out.println("Item found at position " + (index
+ 1));
}
break;
case 4:
System.out.println("Sorted List :");
Collections.sort(list);
System.out.println(list.toString());
break;
case 5:
System.out.println(list.toString());
System.out.println("Enter the element to remove");
item = console.nextInt();
index = list.indexOf(item);
list.remove(index);

}
} while (option != 6);
}
}
Q3)

import java.util.*;

public class Main {


public static void main(String[] args) {
ArrayList<String> c1 = new ArrayList<String>();
Scanner sc = new Scanner(System.in);

System.out.println("Enter number students studying python");


int ele = sc.nextInt();
System.out.println("enter names of" + ele + "students");
for (int i = 0; i < ele; i++) {
c1.add(sc.next());
}

ArrayList<String> c2 = new ArrayList<String>();


System.out.println("Enter number students studying java");
int ele1 = sc.nextInt();
System.out.println("enter names of " + ele1 + "students");
for (int i = 0; i < ele1; i++) {
c2.add(sc.next());
}

ArrayList<String> c3 = new ArrayList<String>();


System.out.println("List of students studying both languages\n");
for (String e : c1)
c3.add(c2.contains(e) ? "Yes" : "No");
System.out.println(c3);

}
}
Q4)

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.Scanner;

public class Main {


public static <T> List<T> filterList(List<T> originalList, Predicate<T>
predicate) {
List<T> filteredList = new ArrayList<>();
for (T element : originalList) {
if (predicate.test(element)) {
filteredList.add(element);
}
}
return filteredList;
}

public static void main(String[] args) {

List<Integer> years = new ArrayList<>();


Scanner sc = new Scanner(System.in);

System.out.println("number of years to enter");


int ele = sc.nextInt();
System.out.println("enter any" + ele + "years");
for (int i = 0; i < ele; i++) {
years.add(sc.nextInt());
}

System.out.println("Original list of years: " + years);

List<Integer> leap = filterList(years, n -> n % 4 == 0);


System.out.println("Leap years: " + leap);

List<Integer> nonleap = filterList(years, n -> n % 4 != 0);


System.out.println("Non Leap years: " + nonleap);
System.out.println("enter number of states to enter");
List<String> states = new ArrayList<>();

int str = sc.nextInt();


System.out.println("enter any" + str + " states in india");
for (int i = 0; i < str; i++) {
states.add(sc.next());
}
System.out.println("\nOriginal list of states: " + states);
List<String> mystate = filterList(states, state ->
state.startsWith("t"));
System.out.println("States starting with 't': " + mystate);

}
}
Q5)

import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<String> color = new ArrayList<String>();
color.add("Red");
color.add("Green");
System.out.println("Original array list: " + color);
String new_color = "White";
color.set(1, new_color);
int num = color.size();
System.out.println("Replace second element with 'White'.");
for (int i = 0; i < num; i++)
System.out.println(color.get(i));
}
}
Q6)

import java.util.List;

public class Main {


public static <T> int findIndexOfElement(List<T> list, T target) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(target)) {
return i;
}
}
return -1;
}

public static void main(String[] args) {


List<Integer> numbers = List.of(1, 2, 3, 4, 5);
List<String> colors = List.of("Red", "Green", "Blue", "Orange",
"White");
System.out.println("Original list elements:");
System.out.println("Nums: " + numbers);
System.out.println("Colors: " + colors);
int index1 = findIndexOfElement(numbers, 3);
System.out.println("\nIndex of 3 in numbers: " + index1); // Output: 2
int index2 = findIndexOfElement(numbers, 6);
System.out.println("Index of 6 in numbers: " + index2); // Output: -1
int index3 = findIndexOfElement(colors, "Green");
System.out.println("Index of \"Green\" in colors: " + index3); //
Output: 1
int index4 = findIndexOfElement(colors, "Black");
System.out.println("Index of \"Black\" in colors: " + index4); //
Output: -1
}
}
Q7)

import java.io.*;
import java.util.*;

class Book {
private int id;
private String name;
private String author;
private String publisher;
private int quantity;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getAuthor() {


return author;
}

public void setAuthor(String author) {


this.author = author;
}

public String getPublisher() {


return publisher;
}

public void setPublisher(String publisher) {


this.publisher = publisher;
}

public int getQuantity() {


return quantity;
}

public void setQuantity(int quantity) {


this.quantity = quantity;
}

public Book() {
this.id = 0;
this.name = null;
this.author = null;
this.publisher = null;
this.quantity = 0;
}

public Book(int id, String name, String author, String publisher, int
quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}

class Main {
public static void main(String[] args) {
int i, n, flag = 0;
Scanner sc = new Scanner(System.in);
n = Integer.parseInt(sc.nextLine());
Book[] b = new Book[n];
LinkedHashSet<Book> bb = new LinkedHashSet<Book>(n);
for (i = 0; i < n; i++) {
b[i] = new Book();
b[i].setId(Integer.parseInt(sc.nextLine()));
b[i].setName(sc.nextLine());
b[i].setAuthor(sc.nextLine());
b[i].setPublisher(sc.nextLine());
b[i].setQuantity(Integer.parseInt(sc.nextLine()));
bb.add(b[i]);
}
String searchBook = sc.nextLine();
for (Book b1 : bb) {
System.out.println(b1.getId() + " " + b1.getName() + " " +
b1.getAuthor() + " " + b1.getPublisher() + " "
+ b1.getQuantity());
}
for (Book b2 : bb) {
if (b2.getName().contains(searchBook)) {
flag = 1;
break;
}
}
if (flag == 1) {
System.out.println(searchBook + " is present in the set");
} else {
System.out.println(searchBook + " is not present in the set");
}
}
}
Q8)

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

class ItemType {
private String name;
private Double deposit;
private Double costPerDay;

@Override
public String toString() {
return String.format("%-20s%-20s%-20s", name, deposit, costPerDay);
}

public ItemType(String name, Double deposit, Double costPerDay) {


super();
this.name = name;
this.deposit = deposit;
this.costPerDay = costPerDay;
}

class Main {
public static void main(String args[]) {
List<ItemType> items = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
String name = sc.nextLine();
Double deposit = Double.parseDouble(sc.nextLine());
Double costPerDay = Double.parseDouble(sc.nextLine());
items.add(new ItemType(name, deposit, costPerDay));
}
Iterator it = items.iterator();
System.out.format("%-20s%-20s%-20s", "Name", "Deposit", "Cost Per
Day");
System.out.println();
while (it.hasNext()) {
System.out.println(it.next());
}

}
Q9)

import java.io.*;
import java.util.*;

class Hall {
private String name;
private String contactNumber;
private double costPerDay;
private String ownerName;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getContactNumber() {


return contactNumber;
}

public void setContactNumber(String contactNumber) {


this.contactNumber = contactNumber;
}

public double getCostPerDay() {


return costPerDay;
}

public void setCostPerDay(double costPerDay) {


this.costPerDay = costPerDay;
}

public String getOwnerName() {


return ownerName;
}

public void setOwnerName(String ownerName) {


this.ownerName = ownerName;
}

public Hall() {
this.name = null;
this.contactNumber = null;
this.costPerDay = 0;
this.ownerName = null;
}
public Hall(String name, String contactNumber, Double costPerDay, String
ownerName) {
this.name = name;
this.contactNumber = contactNumber;
this.costPerDay = costPerDay;
this.ownerName = ownerName;
}

public void display() {


System.out.printf("%-20s%-20s%-20s%-20s\n", name, contactNumber,
costPerDay, ownerName);
}
}

class Main {
public static void main(String[] args) {
int i, n;
Scanner sc = new Scanner(System.in);
n = Integer.parseInt(sc.nextLine());
ArrayList<Hall> h1 = new ArrayList<>(n);
Hall[] h = new Hall[n];
for (i = 0; i < n; i++) {
h[i] = new Hall();
h[i].setName(sc.nextLine());
h[i].setContactNumber(sc.nextLine());
h[i].setCostPerDay(Double.parseDouble(sc.nextLine()));
h[i].setOwnerName(sc.nextLine());
h1.add(h[i]);
}
int index = Integer.parseInt(sc.nextLine());
if (h1.isEmpty()) {
System.out.println("The list is empty");
} else {
h1.remove(index);
System.out.printf("%-20s%-20s%-20s%-20s\n", "Name", "Contact
Number", "CostperDay", "Owner Name");
}

for (i = 0; i < n - 1; i++) {


h1.get(i).display();
}
}
}
Q10)

import java.util.*;
import java.lang.*;
import java.io.*;

class Q03Complex_List {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Input number of hands:
int numHands = input.nextInt();
// Input number of cards per hand
int cardsPerHand = input.nextInt();

if (numHands < 1 || cardsPerHand < 1)


return;

String[] suit = new String[] {


"Spades", "Hearts", "Diamonds", "Clubs"
};
String[] rank = new String[] {
"Ace", "2", "3", "4",
"5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"
};
List<String> deck = new ArrayList<String>();
for (int i = 0; i < suit.length; i++)
for (int j = 0; j < rank.length; j++)
deck.add(rank[j] + " of " + suit[i]);

Collections.shuffle(deck, new Random(0));

if (numHands * cardsPerHand > deck.size()) {


System.out.println("Not enough cards.");
return;
}

for (int i = 0; i < numHands; i++)


System.out.println(dealHand(deck, cardsPerHand));
}

public static List<String> dealHand(List<String> deck, int n) {


int deckSize = deck.size();
List<String> handView = deck.subList(deckSize - n, deckSize);
List<String> hand = new ArrayList<String>(handView);
handView.clear();
return hand;
}
}

You might also like