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

Experiment 12 22z433 2

This document contains the details of 9 experiments conducted on Object Oriented Programming Laboratory. The experiments cover topics like reading input from the console, handling console output using PrintWriter class, reading and writing files, copying files, using Automatic Resource Management to close files, working with ArrayList and LinkedList in Java collections. For each experiment, the code with input/output and purpose is documented.

Uploaded by

T.K. Santhosh
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)
15 views

Experiment 12 22z433 2

This document contains the details of 9 experiments conducted on Object Oriented Programming Laboratory. The experiments cover topics like reading input from the console, handling console output using PrintWriter class, reading and writing files, copying files, using Automatic Resource Management to close files, working with ArrayList and LinkedList in Java collections. For each experiment, the code with input/output and purpose is documented.

Uploaded by

T.K. Santhosh
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/ 12

Name : T K Santhosh

Roll No : 22Z433
Date : 06/12/2023

Experiment 12

Object Oriented Programming Laboratory

1. Java Program to read a character from the input console.


Aim : To write a java program to read a character from the input console.

// Program to read a character input from the console.

package EXP_12;

import java.util.Scanner;

public class ReadingCharacters{


public static void main(String[] args){

char character;

// Creating a new scanner instance.


Scanner scan = new Scanner(System.in);
System.out.print("Enter a character : ");
character = scan.next().charAt(0);

System.out.println("The entered character is : " +


character);
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter a character : s
The entered character is : s

Page 1
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
2.
Aim : To write a java program to read a string from the console using
BufferedReader.

// Program to read string from the console.

package EXP_12;
import java.io.*;

public class ReadingString {


public static void main(String[] args) throws Exception{

String name;

BufferedReader string_reader = new BufferedReader(new


InputStreamReader(System.in));
System.out.print("Enter a string: ");

name = string_reader.readLine();

System.out.println("You entered " + name + " string.");

}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter a string: santhosh
You entered santhosh string.

3.
Aim : To write a java program to handle console output using printwriter
class.

// Program to handle console output using PrintWriter class.

Page 2
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
package EXP_12;

import java.io.PrintWriter;

public class PrintWriterDemo {

public static void main(String[] args){


PrintWriter print_write = new PrintWriter(System.out);

print_write.write("Using the PrintWriter to print the


output..\n");
print_write.println("Hello, World!");

// Finally flushing and closing the PrintWriter instance.


print_write.flush();
print_write.close();
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Using the PrintWriter to print the output..
Hello, World!

4.
Aim : To write a java program to print console output using write( )
// Program to print console output using write()

package EXP_12;

import java.io.PrintStream;

public class ConsoleOutputWrite {


public static void main(String[] args) throws Exception{
PrintStream out = System.out;

out.write("Hello, World\n".getBytes());
out.write("This is how we use the write() method to print

Page 3
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
console output..\n".getBytes());
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Hello, World
This is how we use the write() method to print console output..

5.
Aim : To write a java program to read a file using read( ).

package EXP_12;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadFile {

public static void main(String[] args) throws IOException


{

File file = new


File("/home/santhoshtk/Desktop/PSG-TECH/3rd-sem/OPP-LAB/EXP_12/f
ile.txt");
FileInputStream inputStream = new FileInputStream(file);

int byteRead;
while ((byteRead = inputStream.read()) != -1) {
char character = (char) byteRead;
System.out.print(character);
}

inputStream.close();

Page 4
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
5 1 2 3 4 6 7

6.
Aim : To write a java program to copy a file from one place to another place.

package EXP_12;

import java.nio.channels.FileChannel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class CopyFile{


public static void main(String[] args) throws Exception{

// Creating two channels.


FileChannel inputStream = null;
FileChannel outputStream = null;

// Creating the files.


File source = new
File("/home/santhoshtk/Desktop/PSG-TECH/3rd-sem/OPP-LAB/EXP_12/f
ile.txt");
File destination = new
File("/home/santhoshtk/Desktop/PSG-TECH/3rd-sem/OPP-LAB/EXP_12/n
ew_file.txt");

try{
inputStream = new
FileInputStream(source).getChannel();
outputStream = new
FileOutputStream(destination).getChannel();

Page 5
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
outputStream.transferFrom(inputStream, 0,
inputStream.size());
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
// Closing the two file channels.
inputStream.close();
outputStream.close();
}

}
}

7.
Aim : To write a java program to use the Automatic Resource Management
(ARM) to close the file.

Page 6
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023

// Program to use the Automatic Resource Management (ARM) to


close the file.
// Try block with resource.

package EXP_12;

import java.io.FileInputStream;
import java.util.Scanner;
import java.io.*;

public class AutomaticResourceManagement {


public static void main(String[] args){

File data = new


File("/home/santhoshtk/Desktop/PSG-TECH/3rd-sem/OPP-LAB/EXP_12/n
ew_file.txt");

try(FileInputStream input = new FileInputStream(data)){


Scanner scan = new Scanner(input);

while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}

scan.close();
}
catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Hello, I am Santhosh here.

Page 7
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023

8.
Aim : To write a java program to work with the arraylist in java collection.

import java.util.ArrayList;
import java.util.Collections;

public class ArrayListDemo {


public static void main(String[] args){
ArrayList <String> colors = new ArrayList<String>();

// Adding some colors to the arraylist


colors.add("blue");
colors.add("green");
colors.add("black");

// Inserting a color at the first position.


colors.add(0, "red");

// Deleting the third element.


colors.remove(2);

// Search for the element red.


System.out.println("Red found : " +
colors.contains("Red".toLowerCase()));

// Sorting the array list.


Collections.sort(colors);

// updating the fourth element with yellow.


colors.add(3, "Yellow");

// Printing the final output.


for(String color: colors){
System.out.println(color);
}

Page 8
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Red found at : true
black
blue
red
Yellow

9.
Aim : To write a java program to work with LinkedList in java using the
collection framework.

import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Iterator;

public class LinkedListDemo{


public static void main(String[] args){
ArrayList <String> animals = new ArrayList<String>();
animals.add("cow");
animals.add("lion");
animals.add("cheetah");

// Creating a new linked list.


LinkedList <String> animals_list = new
LinkedList<String>();
animals_list.addAll(animals);

// Inserting cat at the third position.


animals_list.add(2, "cat");

// Removing the first and the last element.


animals_list.remove(0);

Page 9
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
animals_list.remove(animals_list.size() - 1);

// Search for the dog.


System.out.println("Is dog exist : " +
animals_list.contains("dog"));

// Replace second element with elephant.


animals_list.add(1, "elephant");

// Creating a Iterater instance to print the elements.


Iterator <String> iter = animals_list.iterator();

while (iter.hasNext()) {
System.out.println(iter.next());
}

}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Is dog exist : false
lion
elephant
cat

10.
Aim : To write a java program to work with priority queues in java using the
collection framework.

import java.util.PriorityQueue;
import java.util.*;

public class PriorityQueueDemo {

Page 10
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
public static void main(String[] args){
PriorityQueue <String> subjects = new
PriorityQueue<String>();
subjects.add("Data Structures and Algorithms");
subjects.add("Object Oriented Programming");
subjects.add("Design Patterns");
subjects.add("Distributed Computing");

// Adding maths to the priority queue.


subjects.add("Mathematics");

// Iterating through all the elements.


Iterator <String> new_iterator = subjects.iterator();

while (new_iterator.hasNext()) {
System.out.println(new_iterator.next());
}

// Count the no of element in the priority queue.


System.out.println("No of elements in the priority queue :
" + subjects.size());

// Retrieving the first element in the priority queue.


System.out.println("First element in the priority queue is
: " + subjects.peek());

// Adding the elements of the priority queue to another


queue.
PriorityQueue <String> new_subjects = new
PriorityQueue<String>();

Iterator <String> new_Iterator2 = subjects.iterator();

while(new_Iterator2.hasNext()){
new_subjects.add(new_Iterator2.next());
}

// Printing the elements of the second priority queue.

Page 11
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
Iterator <String> new_Iterator3 = new_subjects.iterator();

while(new_Iterator3.hasNext()){
System.out.println(new_Iterator3.next());
}
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Data Structures and Algorithms
Distributed Computing
Design Patterns
Object Oriented Programming
Mathematics
No of elements in the priority queue : 5
First element in the priority queue is : Data Structures and
Algorithms
Data Structures and Algorithms
Distributed Computing
Design Patterns
Object Oriented Programming
Mathematics

Result
Hence the given programs were executed successfully.

Page 12

You might also like