0% found this document useful (0 votes)
21 views7 pages

Oop Set 8

1. The document describes implementations of linked lists and priority queues in Java. It includes code to perform operations like insertion, deletion on singly linked lists and cloning a priority queue. 2. The code defines a MyPriorityQueue class that extends PriorityQueue and implements the Cloneable interface. It overrides the clone() method to clone the priority queue. 3. The last part asks to write a program to remove all occurrences of a specified value from an integer array and return the new length of the array.

Uploaded by

adada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Oop Set 8

1. The document describes implementations of linked lists and priority queues in Java. It includes code to perform operations like insertion, deletion on singly linked lists and cloning a priority queue. 2. The code defines a MyPriorityQueue class that extends PriorityQueue and implements the Cloneable interface. It overrides the clone() method to clone the priority queue. 3. The last part asks to write a program to remove all occurrences of a specified value from an integer array and return the new length of the array.

Uploaded by

adada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Enrollment No.

200170107069 Practical 8

Name: Patel Aditi Mukeshkumar


Enrollment No.: 200170107069
Subject: Object Orientated Programming

Practical : 8
Practical 8.1)
Implement singly linked list and its operations in java program.
Code:

import ptrjava.util.Scanner;
= ptr.getNext(); public
public boolean
class isEmpty(){ {
Practical8_01
class
} Node { public static void null;
return start == main(String[] args) {
protected
size++; int data; } Scanner scan = new Scanner(System.in);
} protected Node Next; public int getSize()
LinkedList { LinkedList();
list = new
publicvoid
public Node() {
deleteAtPos(int pos) { return size;
System.out.println("Singly Linked List Test");
if Next
(pos == = null;
1) { } char ch;
data
start==0;start.getNext(); public void insertAtStart(int val)
System.out.println("Singly {
Nexted List
} size--; Node nptr = new Node(val, null);
Operations");
public Node(int d) {
return; size++;
System.out.println("1. insert at begining");
} Next = null; ifSystem.out.println("2.
(start == null) { insert at end");
if data
(pos ===d;size) { start = nptr;
System.out.println("3. insert at position");
} Node s = start; end = start;
System.out.println("4. delete at position");
public
Node Node(int
t = start;d, Node n) { }System.out.println("5.
else { check empty");
data
while= (sd;!= end) { nptr.setNext(start); get size");
System.out.println("6.
Nextt ==s;n; dostart
{ = nptr;
} s = s.getNext(); } System.out.println("Enter your operation");
public
} void setNext(Node n) { } int choice = scan.nextInt();
Next
end == t;n; public void insertAtEnd(int
switch (choice) { val) {
} end.setNext(null); Nodecase nptr1:= new Node(val, null);
public void setData(int d) {
size--; size++; System.out.println("Enter integer
data
return;= d; if (start
element == null) {
to insert");
}} start list.insertAtStart(scan.nextInt());
= nptr;
public
Node ptr Node getNext() {
= start; end =break;
start;
return Next;
pos = pos - 1; } elsecase{ 2:
}for (int i = 1; i < size - 1; i++) { end.setNext(nptr);
System.out.println("Enter integer
public int pos)
if (i == getData()
{ { element end to=insert");
nptr;
return data;
Node tmp = ptr.getNext(); } } list.insertAtEnd(scan.nextInt());
} tmp = tmp.getNext(); public void insertAtPos(int val, int pos) {
break;
} ptr.setNext(tmp); Nodecase nptr3:= new Node(val, null);
class LinkedList
break; { Node ptr = start;
System.out.println("Enter integer
protected
} Node start; pos =topos
element - 1;
insert");
protected Node end;
ptr = ptr.getNext(); for (int inti = num
1; i < =size; i++) {
scan.nextInt();
public
} int size; if (i == pos) {
System.out.println("Enter position");
public
size--; LinkedList()
} { Nodeint postmp==scan.nextInt();
ptr.getNext();
start = null;
public void display() { ptr.setNext(nptr);
if (pos <= 1 || pos > list.getSize())
end = null;
System.out.print("Singly Nexted List = "); nptr.setNext(tmp);
System.out.println("Invalid position");
if size
(size=== 0; 0) { break;
else
} System.out.print("empty\n"); } list.insertAtPos(num, pos);
Object Orientated
return; } Programming break; 1|Page
if (start.getNext() == null) { case 4:
System.out.println(start.getData()); System.out.println("Enter position");
Enrollment No. 200170107069 Practical 8

case 6:
System.out.println("Size = " + list.getSize());
break;
default:
System.out.println("Wrong Entry");
break;
}
list.display();
System.out.println("Do you want to continue (Type y or n)");
ch = scan.next().charAt(0);
} while (ch == 'Y' || ch == 'y');
scan.close();
}
}

Output:

Object Orientated Programming 2|Page


Enrollment No. 200170107069 Practical 8

Practical 8.2)
Define MYPriorityQueue class that extends Priority Queue to implement the
Cloneable interface and implement the clone() method to clone a priority queue.
Code:

import java.util.PriorityQueue;
class MyPriorityQueue<E> extends PriorityQueue<E> implements Cloneable {
@Override
public Object clone() throws CloneNotSupportedException {
MyPriorityQueue<E> clone = new MyPriorityQueue<>();
this.forEach(clone::add);
return clone;
}
}
public class Practical8_02 {
public static void main(String[] args) {
MyPriorityQueue<String> queue = new MyPriorityQueue<>();
queue.add("1");
queue.add("2");
queue.add("3");
MyPriorityQueue<String> queue1 = null;
try {
queue1 = (MyPriorityQueue<String>) (queue.clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println("Cloned Priority Queue is : ");
System.out.println(queue1);
}
}

Output:

Object Orientated Programming 3|Page


Enrollment No. 200170107069 Practical 8

Practical 8.3)
Write a Java program to remove all occurrences of a specified value in a given array of
integers and return the new length of the array.
Code:

import java.util.ArrayList;
import java.util.Scanner;
public class Practical8_03 {
static int removeOccurrencesInRange(ArrayList<Integer> arr, int start, int end){
for (int i = arr.size()-1; i >= 0; i--) {
if(arr.get(i) >= start && arr.get(i) <= end){
arr.remove(i);
}
}
return arr.size();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
int n = sc.nextInt();
ArrayList<Integer> arr = new ArrayList<>();
System.out.println("Enter " + n + " elements of the array");
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
arr.add(temp);
}
System.out.println("Enter the starting and ending range");
int start = sc.nextInt();
int end = sc.nextInt();
int newSize = removeOccurrencesInRange(arr, start, end);
System.out.println("New size of array is : " + newSize);
sc.close();
}
}
Output:

Object Orientated Programming 4|Page


Enrollment No. 200170107069 Practical 8

Practical 8.4)
Write a Java program to find the longest word in a text file.
Code:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Practical8_04 {
static String findLongestWords() throws FileNotFoundException {
String longest_word = "";
String current;
Scanner sc = new Scanner(new File("./temp.txt"));
while (sc.hasNext()) {
current = sc.next();
if (current.length() > longest_word.length()) {
longest_word = current;
}
}
return longest_word;
}
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Longest word in the file is : ");
System.out.println(findLongestWords());
}
}

Output:

Object Orientated Programming 5|Page


Enrollment No. 200170107069 Practical 8

Practical 8.5)
Write a program to create a file name 123.txt, if it does not exist. Append a new data to
it if it already exist. Write 150 integers created randomly into the file using Text I/O.
Integers are separated by space.
Code:

import java.io.*;
import java.util.Scanner;
class Practical8_05 {
public static void main(String[] args) {
try (
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("123.txt"), true));
)
{
for (int i = 0; i < 150; i++){
pw.print((int)(Math.random()* 50) + " ");
}
System.out.println("Integers are added into 123.txt file.");
}
catch (FileNotFoundException notfound) {
System.out.println("Cannot create the file.");
notfound.printStackTrace();
}
}
}

Output:

Object Orientated Programming 6|Page


Enrollment No. 200170107069 Practical 8

Practical 8.6)
Write a program that reads words from a text file and displays all the no duplicate
words in descending order. The text file is passed as a command-line argument.
Code:

import java.io.*;
import java.util.*;
class Practical8_06 {
public static void main(String[] args) {
if(args.length==1){
String fileName = args[0];
TreeSet<String> set = new TreeSet<>();
File file = new File(fileName);
try {
Scanner s = new Scanner(file);
while (s.hasNext()){
set.add(s.next());
}
System.out.println(set);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else{
System.out.println("Please, Pass the File Name as Command Line Argument");
}
}
}
Output:

Object Orientated Programming 7|Page

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy