Name : Dhwani Yadav
Roll no. : B23CSE067
JAVA TASK-7
1) import [Link];
import [Link];
public class MainApp {
public static void main(String[] args) {
// Creating an Address object
Address address = new Address("123 Main St", "Springfield");
// Creating a Person object and associating it with the address
Person person = new Person("John Doe", 30, address);
// Displaying the person's information
[Link]();
}
}
package myPackage;
public class Person {
private String name;
private int age;
private Address address;
public Person(String name, int age, Address address) {
[Link] = name;
[Link] = age;
[Link] = address;
}
public void displayPersonInfo() {
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]();
}
}
package myPackage;
public class Address {
private String street;
private String city;
public Address(String street, String city) {
[Link] = street;
[Link] = city;
}
public void displayAddressInfo() {
[Link]("Street: " + street);
[Link]("City: " + city);
}
}
OUTPUT
Name: Nakul Verma
Age: 30
Street: 124 shivaji road
City: Mumbai
2) interface Calculator {
public int add(int a, int b);
public int subtract(int a, int b);
public int multiply(int a, int b);
public double divide(int a, int b);
}
class BasicCalculator implements Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public double divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return (double) a / b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new BasicCalculator();
[Link]("Addition: " + [Link](10, 5));
[Link]("Subtraction: " + [Link](10, 5));
[Link]("Multiplication: " + [Link](10, 5));
[Link]("Division: " + [Link](10, 5));
}
}
3) class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("MyThread: " + i);
}
}
}
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("MyRunnable: " + i);
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
Thread t2 = new Thread(new MyRunnable());
[Link]();
[Link]();
}
}
4) class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
[Link](getName() + ": " + i);
}
}
}
public class ThreadPriorityDemo {
public static void main(String[] args) {
NumberThread t1 = new NumberThread();
NumberThread t2 = new NumberThread();
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}
}
5) class ItemQueue {
private int item;
private boolean produced = false;
public synchronized void produce(int item) throws InterruptedException {
while (produced) {
wait();
}
[Link] = item;
produced = true;
[Link]("Produced: " + item);
notify();
}
public synchronized int consume() throws InterruptedException {
while (!produced) {
wait();
}
produced = false;
notify();
[Link]("Consumed: " + item);
return item;
}
}
class Producer extends Thread {
ItemQueue queue;
public Producer(ItemQueue queue) {
[Link] = queue;
}
public void run() {
for (int i = 1; i <= 5; i++) {
try {
[Link](i);
} catch (InterruptedException e) {
[Link]();
}
}
}
}
class Consumer extends Thread {
ItemQueue queue;
public Consumer(ItemQueue queue) {
[Link] = queue;
}
public void run() {
for (int i = 1; i <= 5; i++) {
try {
[Link]();
} catch (InterruptedException e) {
[Link]();
}
}
}
}
public class ProducerConsumer {
public static void main(String[] args) {
ItemQueue queue = new ItemQueue();
new Producer(queue).start();
new Consumer(queue).start();
}
}
6) import [Link].*;
public class FileCopy {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("[Link]");
FileOutputStream fos = new FileOutputStream("[Link]")) {
int byteData;
while ((byteData = [Link]()) != -1) {
[Link](byteData);
}
[Link]("File copied successfully.");
} catch (IOException e) {
[Link]();
}
}
}
OUTPUT
7) import [Link].*;
public class CollectionDemo {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
HashSet<String> hashSet = new HashSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
TreeSet<String> treeSet = new TreeSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
[Link]("ArrayList: " + arrayList);
[Link]("HashSet: " + hashSet);
[Link]("TreeSet: " + treeSet);
}
}
8) import [Link].*;
public class ListPerformance {
public static void main(String[] args) {
List<Integer> arrayList = new ArrayList<>();
List<Integer> linkedList = new LinkedList<>();
long start = [Link]();
for (int i = 0; i < 10000; i++) {
[Link](i);
}
long end = [Link]();
[Link]("ArrayList add: " + (end - start) + " ");
start = [Link]();
for (int i = 0; i < 10000; i++) {
[Link](i);
}
end = [Link]();
[Link]("LinkedList add: " + (end - start) + " " );
start = [Link]();
[Link](5000);
end = [Link]();
[Link]("ArrayList remove: " + (end - start) + " ");
start = [Link]();
[Link](5000);
end = [Link]();
[Link]("LinkedList remove: " + (end - start) + " ");
}
}
9) import [Link];
import [Link];
public class MapDemo {
public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<>();
[Link](1, "One");
[Link](2, "Two");
[Link](3, "Three");
[Link]("HashMap: " + hashMap);
TreeMap<Integer, String> treeMap = new TreeMap<>();
[Link](1, "One");
[Link](2, "Two");
[Link](3, "Three");
[Link]("TreeMap: " + treeMap);
}
}
10) import [Link];
import [Link];
public class HashMapVsTreeMap {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
[Link]("apple", 1);
[Link]("banana", 2);
[Link]("cherry", 3);
[Link]("HashMap Output (No particular order):");
for (String key : [Link]()) {
[Link](key + ": " + [Link](key));
}
TreeMap<String, Integer> treeMap = new TreeMap<>();
[Link]("apple", 1);
[Link]("banana", 2);
[Link]("cherry", 3);
[Link]("\nTreeMap Output (Sorted by keys):");
for (String key : [Link]()) {
[Link](key + ": " + [Link](key));
}
[Link]("date", 4);
[Link]("date", 4);
[Link]("\nHashMap after inserting 'date': " + hashMap);
[Link]("TreeMap after inserting 'date': " + treeMap);
}
}
OUTPUT