Oop
Oop
@) RTTI
@) Nested try
@) Valid Invalid
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);
}
import java.io.*;
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
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
System.out.print("LinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println();
}
// **************DELETION BY KEY**************
//
// CASE 1:
// If head node itself holds the key to be deleted
//
// CASE 2:
// If the key is somewhere other than at head
//
//
// CASE 3: The key is not present
//
// **************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******
//
//
// ******DELETION BY KEY******
//
@) 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);
}
}
}
}
@) Runnable
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