Java LAB 6th Nov
Java LAB 6th Nov
Java LAB 6th Nov
import java.util.*;
import java.util.*;
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.*;
}
}
Q4)
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.Scanner;
}
}
Q5)
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.*;
class Book {
private int id;
private String name;
private String author;
private String publisher;
private int 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);
}
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 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;
}
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");
}
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();