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

Java Lab VV

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)
6 views

Java Lab VV

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/ 11

Experiment No.

8
Aim - WAP in java to illustrate multithreading, inheritance, interface.

Code :
interface RunnableTask {

void runTask();

class Animal {

void makeSound() {

System.out.println("Some generic animal sound");

class Dog extends Animal {

void makeSound() {

System.out.println("Bark");

class TaskRunner implements Runnable, RunnableTask {

private String taskName;

private Animal animal;

public TaskRunner(String taskName, Animal animal) {

this.taskName = taskName;
this.animal = animal;

public void runTask() {

System.out.println("Running task: " + taskName);

animal.makeSound();

public void run() {

runTask();

public class Main {

public static void main(String[] args) {

Animal myDog = new Dog();

TaskRunner task = new TaskRunner("DogSoundTask", myDog);

Thread thread = new Thread(task);

thread.start();

System.out.println("Main thread is running...");

}
Output Window:
Experiment No. 9
Aim - WAP to create a file. Read and write from the file using byte stream
class.

Code :
import java.io.*;

public class Main {

public static void main(String[] args) {

String filename = "story.txt";

String content = "This is astory book";

try (FileOutputStream f = new FileOutputStream(filename)) {

byte[] contentBytes = content.getBytes();

f.write(contentBytes);

System.out.println(" written successfully");

} catch (IOException e) {

System.out.println("An error encountered while writing to the file");

e.printStackTrace();

try (FileInputStream f = new FileInputStream(filename)) {

int contentLength = f.available();

byte[] contentBytes = new byte[contentLength];

f.read(contentBytes);
String fileContent = new String(contentBytes);

System.out.println("File content: " + fileContent);

} catch (IOException e) {

System.out.println("An error encountered while reading from the


file.");

e.printStackTrace();

Output Window:
Experiment No. 10
Aim - WAP in java to illustrate list.

Code :
import java.util.*;

public class Main{

public static void main(String[] args) {

//ArrayList

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

AL.add(1);

AL.add(2);

AL.add(3);

AL.add(4);

AL.add(5);

System.out.println("ArrayList is : "+ AL);

System.out.println("Size of ArrayList is : "+ AL.size());

System.out.println("Element at index 4 is : "+ AL.get(4));

System.out.println("Does AL(ArrayList) contains 3 : "+AL.contains(3));

AL.remove(3);

System.out.println("Does AL(ArrayList) contains AL : "+AL.containsAll(AL));


AL.add(3,8);

System.out.println("Updated ArrayList is : "+ AL);

//LinkedList

LinkedList<Integer> LL=new LinkedList<>();

LL.add(9);

LL.add(10);

System.out.println("LinkedList is : "+ LL);

LL.addAll(AL);

LL.add(4,11);

LL.addFirst(12);

LL.addLast(13);

System.out.println("First Element of LinkedList is : " + LL.getFirst());

System.out.println("Last Element of LinkedList is : " + LL.getLast());

System.out.println("Updated LinkedList is : "+ LL);

//Vector

Vector<String> vector = new Vector<>();

vector.add("Apple");

vector.add("Banana");

vector.add("Cherry");

System.out.println("Initial Vector :"+vector);

System.out.println("Element at index 1: " + vector.get(1));

vector.set(1, "Blueberry");
System.out.println("Vector elements after modification:"+ vector);

vector.remove(2);

System.out.println("Vector elements after removal:"+vector);

System.out.println("Vector size: " + vector.size());

System.out.println("Vector capacity: " + vector.capacity());

Output Window:
Experiment No. 11
Aim - WAP in java to illustrate set.

Code :
import java.util.*;

public class Main {

public static void main(String[] args) {

//hashSet

Set<String> hashSet = new HashSet<>();

hashSet.add("Apple");

hashSet.add("Banana");

hashSet.add("Cherry");

hashSet.add("Apple");

System.out.println("HashSet elements (unordered, no


duplicates):"+hashSet);

System.out.println("size of hashset: "+ hashSet.size());

System.out.println("hashset is empty or not : "+ hashSet.isEmpty());

Set<String> retainSet = new HashSet<>();

retainSet.add("Apple");

retainSet.add("Banana");

hashSet.retainAll(retainSet);

System.out.println("HashSet after retainAll:"+hashSet);

//linkedHashSet

Set<String> linkedHashSet = new LinkedHashSet<>();


linkedHashSet.add("Dog");

linkedHashSet.add("Elephant");

linkedHashSet.add("Frog");

linkedHashSet.add("Dog");

System.out.println("LinkedHashSet elements :"+linkedHashSet);

linkedHashSet.addAll(hashSet);

System.out.println("LinkedHashSet after addAll:"+linkedHashSet);

System.out.println("Linkedhashset is empty or not : "+


linkedHashSet.isEmpty());

//treeSet

Set<String> treeSet = new TreeSet<>();

treeSet.add("Giraffe");

treeSet.add("Hippo");

treeSet.add("Iguana");

treeSet.add("Giraffe");

System.out.println("TreeSet elements :"+treeSet);

treeSet.addAll(linkedHashSet);

System.out.println("TreeSet after addAll:"+treeSet);

System.out.println("Size of treeSet: " + treeSet.size());

treeSet.retainAll(hashSet);

System.out.println("TreeSet after retainAll:"+treeSet);

System.out.println("treeset is empty or not : "+ treeSet.isEmpty());

}
}

Output Window:

You might also like