0% found this document useful (0 votes)
66 views9 pages

Oop

The document contains code snippets demonstrating different Java concepts like sorting names, exception handling, data input/output, linked lists, threads, and inheritance. Specifically, it includes code for sorting an array of names, using runtime type identification to check data types, nested try-catch blocks, validating number inputs, writing data to a file, implementing a linked list with insertion and deletion methods, using synchronized methods for thread-safe execution, implementing Runnable interface to create threads, and extending the Thread class.

Uploaded by

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

Oop

The document contains code snippets demonstrating different Java concepts like sorting names, exception handling, data input/output, linked lists, threads, and inheritance. Specifically, it includes code for sorting an array of names, using runtime type identification to check data types, nested try-catch blocks, validating number inputs, writing data to a file, implementing a linked list with insertion and deletion methods, using synchronized methods for thread-safe execution, implementing Runnable interface to create threads, and extending the Thread class.

Uploaded by

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

@) Sortinh names.

public class SortingNames


{
public static void main(String[] args)
{
int k=0;
int ln = args.length;
for (int i = 0; i < ln-1; i++)
{
for (int j = i + 1; j < ln; j++)
{
if (args[i].compareTo(args[j]) > 0)
{
String temp = args[i];
args[i] = args[j];
args[j] = temp;
}
}
}
// for printing the elements of the string.
for( int i = 0 ; i< ln ; i++)
{
System.out.print(args[i]);
System.out.print(" ");
}
}
}

@) RTTI

public class RTTI


{
public static void main(String[] args)
{
try
{
try
{
int a = Integer.parseInt(args[0]);
System.out.println("It is a Integer.");
}
catch (Exception e1)
{
double a = Double.parseDouble(args[0]);
System.out.println("It is a double value.");
}
}
catch (Exception e2)
{
String b = args[0];
System.out.println("It is a character value.");
}
}
}

@) Nested try

public class Nested_try{


public static void main(String[] args)
{
int a[]={10,20};
int b=10;
try
{
int d=a[0]/(b-a[0]);
try
{
int c=a[2]/b-a[1];
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index error");
}
catch(ArrayStoreException e)
{
System.out.println("Array wrong data type");
}
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
int y=a[1]/a[0];
System.out.println("y= "+y); // only to check if the program ran
completely
}
}

@) Valid Invalid

public class Exceptionsss {


public static void main(String args[])
{
int invalid=0;
int number,count=0;

for(int i=0;i<args.length;i=i+1)
{
try
{
number=Integer.parseInt(args[i]);
System.out.println("Valid number:"+number);
}
catch(NumberFormatException e)
{
invalid=invalid+1;
System.out.println("Inavalid number: "+args[i]);
continue;
}
count=count+1;
}
System.out.println("Valid number count:"+count);
System.out.println("Invalid number count:"+invalid);
}

@) Data input Stream

import java.io.*;

public class Inputing_data_to_a_file {


public static void main(String[] args) throws IOException
{
DataInputStream dis=new DataInputStream(System.in);

System.out.println("Enter name:");
String name=dis.readLine();
System.out.println("The name is: "+name);

System.out.println("Enter age:");
String s=dis.readLine();
int age=Integer.parseInt(s);
System.out.println("The age is:"+age);

System.out.println("Enter salary:");
String sa=dis.readLine();
Double sal=Double.parseDouble(sa);
System.out.println("The Salary is:"+sal);
// Transferring above details in a file using DataOutputStream.
FileOutputStream fos =new FileOutputStream("abc.txt");
DataOutputStream dos=new DataOutputStream(fos);

dos.writeInt(age);
dos.writeBytes(name);
dos.writeDouble(sal);

dis.close();
}
}

@) Linked List

// Java program to implement


// a Singly Linked List
public class LinkedList {

Node head; // head of list

// Linked list Node.


// Node is a static nested class
// so main() can access it
static class Node {

int data;
Node next;

// Constructor
Node(int d)
{
data = d;
next = null;
}
}

// Method to insert a new node


public static LinkedList insert(LinkedList list,
int data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;

// If the Linked List is empty,


// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}

// Insert the new_node at last node


last.next = new_node;
}

// Return the list by head


return list;
}

// Method to print the LinkedList.


public static void printList(LinkedList list)
{
Node currNode = list.head;

System.out.print("LinkedList: ");

// Traverse through the LinkedList


while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");

// Go to next node
currNode = currNode.next;
}

System.out.println();
}

// **************DELETION BY KEY**************

// Method to delete a node in the LinkedList by KEY


public static LinkedList deleteByKey(LinkedList list,
int key)
{
// Store head node
Node currNode = list.head, prev = null;

//
// CASE 1:
// If head node itself holds the key to be deleted

if (currNode != null && currNode.data == key) {


list.head = currNode.next; // Changed head

// Display the message

// Return the updated List


return list;
}

//
// CASE 2:
// If the key is somewhere other than at head
//

// Search for the key to be deleted,


// keep track of the previous node
// as it is needed to change currNode.next
while (currNode != null && currNode.data != key) {
// If currNode does not hold key
// continue to next node
prev = currNode;
currNode = currNode.next;
}

// If the key was present, it should be at currNode


// Therefore the currNode shall not be null
if (currNode != null) {
// Since the key is at currNode
// Unlink currNode from linked list
prev.next = currNode.next;

// Display the message


System.out.println(key + " found and deleted");
}

//
// CASE 3: The key is not present
//

// If key was not present in linked list


// currNode should be null
if (currNode == null) {
// Display the message
System.out.println(key + " not found");
}

// return the List


return list;
}

// **************MAIN METHOD**************
// method to create a Singly linked list with n nodes
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

// Insert the values


list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);

// Print the LinkedList


printList(list);

//
// ******DELETION BY KEY******
//

// Delete node with value 1


// In this case the key is ***at head***
deleteByKey(list, 1);

// Print the LinkedList


printList(list);

// Delete node with value 4


// In this case the key is present ***in the
// middle***
deleteByKey(list, 4);

// Print the LinkedList


printList(list);

// Delete node with value 10


// In this case the key is ***not present***
deleteByKey(list, 10);

// Print the LinkedList


printList(list);
}
}

@) Synchronized

class Tablee
{
synchronized void printTable(int n) //method synchronized
{
for(int i=1;i<=5;i=i+1)
System.out.println(n*i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class MyThread11 extends Thread
{
Tablee t;
MyThread11(Tablee t)
{
this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread22 extends Thread
{
Tablee t;
MyThread22(Tablee t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}

public class Synchronization {


public static void main(String[] args)
{
Tablee obj=new Tablee();
MyThread11 t1=new MyThread11(obj);
MyThread22 t2=new MyThread22(obj);
t1.start();
t2.start();

}
}

@) Runnable

class SumofaSeries implements Runnable


{
Thread t;
int num;
SumofaSeries(int n)
{
t=new Thread(this);
t.start();
num=n;
}
public void run()
{
int sum=0;
try
{
for(int i=1;i<=num;i=i+1)
{

if(i==1)
{
sum=1;
System.out.print(i);
}
else if(i>1&&i%2==0)
{
sum=sum+i;
System.out.print("+"+i);
}
else if(i>1&&i%2!=0)
{
sum=sum-i;
System.out.print("-"+i);
}
}
System.out.println("\nSum:"+sum);
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
public static void main(String[] args)
{
SumofaSeries s=new SumofaSeries(6);
}
}

@) Extending thread

class ExtendingThread extends Thread


{
ExtendingThread(String nm)
{
super(nm);
start();
}
public void run()
{
try
{
for(int i=0;i<=15;i=i+1)
{
System.out.println("Value of counter:"+i);
sleep(1000);
}
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
public static void main (String[] args)
{
ExtendingThread s1=new ExtendingThread("Sample");
try
{
Thread.sleep(2000);
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
System.out.println("Main thread existing");
}

You might also like