b23cse067 Java
b23cse067 Java
1) import myPackage.Person;
import myPackage.Address;
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();
System.out.println("Addition: " + calc.add(10, 5));
System.out.println("Subtraction: " + calc.subtract(10, 5));
System.out.println("Multiplication: " + calc.multiply(10, 5));
System.out.println("Division: " + calc.divide(10, 5));
}
}
3) class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("MyThread: " + i);
}
}
}
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("MyRunnable: " + i);
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
Thread t2 = new Thread(new MyRunnable());
t1.start();
t2.start();
}
}
4) class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(getName() + ": " + i);
}
}
}
public class ThreadPriorityDemo {
public static void main(String[] args) {
NumberThread t1 = new NumberThread();
NumberThread t2 = new NumberThread();
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
5) class ItemQueue {
private int item;
private boolean produced = false;
public synchronized void produce(int item) throws InterruptedException {
while (produced) {
wait();
}
this.item = item;
produced = true;
System.out.println("Produced: " + item);
notify();
}
public synchronized int consume() throws InterruptedException {
while (!produced) {
wait();
}
produced = false;
notify();
System.out.println("Consumed: " + item);
return item;
}
}
class Producer extends Thread {
ItemQueue queue;
public Producer(ItemQueue queue) {
this.queue = queue;
}
public void run() {
for (int i = 1; i <= 5; i++) {
try {
queue.produce(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer extends Thread {
ItemQueue queue;
public Consumer(ItemQueue queue) {
this.queue = queue;
}
public void run() {
for (int i = 1; i <= 5; i++) {
try {
queue.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ProducerConsumer {
public static void main(String[] args) {
ItemQueue queue = new ItemQueue();
new Producer(queue).start();
new Consumer(queue).start();
}
}
6) import java.io.*;
public class FileCopy {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt")) {
int byteData;
while ((byteData = fis.read()) != -1) {
fos.write(byteData);
}
System.out.println("File copied successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT
7) import java.util.*;
public class CollectionDemo {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Orange");
HashSet<String> hashSet = new HashSet<>();
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Orange");
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Apple");
treeSet.add("Banana");
treeSet.add("Orange");
System.out.println("ArrayList: " + arrayList);
System.out.println("HashSet: " + hashSet);
System.out.println("TreeSet: " + treeSet);
}
}
8) import java.util.*;
public class ListPerformance {
public static void main(String[] args) {
List<Integer> arrayList = new ArrayList<>();
List<Integer> linkedList = new LinkedList<>();
long start = System.nanoTime();
for (int i = 0; i < 10000; i++) {
arrayList.add(i);
}
long end = System.nanoTime();
System.out.println("ArrayList add: " + (end - start) + " ");
start = System.nanoTime();
for (int i = 0; i < 10000; i++) {
linkedList.add(i);
}
end = System.nanoTime();
System.out.println("LinkedList add: " + (end - start) + " " );
start = System.nanoTime();
arrayList.remove(5000);
end = System.nanoTime();
System.out.println("ArrayList remove: " + (end - start) + " ");
start = System.nanoTime();
linkedList.remove(5000);
end = System.nanoTime();
System.out.println("LinkedList remove: " + (end - start) + " ");
}
}
9) import java.util.HashMap;
import java.util.TreeMap;
public class MapDemo {
public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "One");
hashMap.put(2, "Two");
hashMap.put(3, "Three");
System.out.println("HashMap: " + hashMap);
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(1, "One");
treeMap.put(2, "Two");
treeMap.put(3, "Three");
System.out.println("TreeMap: " + treeMap);
}
}
10) import java.util.HashMap;
import java.util.TreeMap;
public class HashMapVsTreeMap {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("apple", 1);
hashMap.put("banana", 2);
hashMap.put("cherry", 3);
System.out.println("HashMap Output (No particular order):");
for (String key : hashMap.keySet()) {
System.out.println(key + ": " + hashMap.get(key));
}
TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("apple", 1);
treeMap.put("banana", 2);
treeMap.put("cherry", 3);
System.out.println("\nTreeMap Output (Sorted by keys):");
for (String key : treeMap.keySet()) {
System.out.println(key + ": " + treeMap.get(key));
}
hashMap.put("date", 4);
treeMap.put("date", 4);
System.out.println("\nHashMap after inserting 'date': " + hashMap);
System.out.println("TreeMap after inserting 'date': " + treeMap);
}
}
OUTPUT