abstract class Shape
{
int x,y;
void getdata(int x1,int y1)
{
x=x1;
y=y1;
}
abstract void area();
}
class Rectangle extends Shape
{
void area()
{
int a=x*y;
System.out.println("Area of Rectangle having width"+x+"and Height="+y+"is"+a);
}
}
class Triangle extends Shape
{
void area()
{
int a=(x*y)/2;
System.out.println("Area of Triangle="+a);
}
}
class AbstractDemo
{
public static void main(String arg[])
{
Rectangle r=new Rectangle();
r.getdata(20,10);
r.area();
Triangle t=new Triangle();
t.getdata(30,40);
t.area();
}
}import javax.swing.JOptionPane;
public class activity11
{
public static void main(String args[])
{
int a,b,c,d,x;
a=Integer.parseInt(JOptionPane.showInputDialog("First Number"));
b=Integer.parseInt(JOptionPane.showInputDialog("Second Number"));
d=Integer.parseInt(JOptionPane.showInputDialog("Number Sequence"));
c=0;
System.out.print(a);
System.out.print(" "+b);
for (x=0;x<=d-2;x++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
}import java.io.*;
import java.lang.*;
class add
{
public static void main(String args[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
int a=0;
a = Integer.parseInt(in.readLine());
int d = a+a;
System.out.println(d);
}
}import java.io.*;
class animate
{
public static void main(String args[])
{
String msg="Java At RNEC by RVS..";
Thread t=Thread.currentThread();
try
{
for(;;)
{
msg=msg.substring(1,msg.length())+String.valueOf(msg.charAt(0));
System.out.println(msg);
t.sleep(100);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
/*
*/
public class appcal extends Applet implements ActionListener
{
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14;
TextField t1;
String str,str1,str3;
int n1,n2,n3;
String str2=0 + "";
public void init()
{
setLayout(null);
t1 = new TextField(20);
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("+");
b11 = new Button("-");
b12 = new Button("*");
b13 = new Button("/");
b14 = new Button("=");
add(t1);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
t1.setBounds(100,100,200,30);
b1.setBounds(100,150,50,50);
b2.setBounds(150,150,50,50);
b3.setBounds(200,150,50,50);
b4.setBounds(250,150,50,50);
b5.setBounds(100,200,50,50);
b6.setBounds(150,200,50,50);
b7.setBounds(200,200,50,50);
b8.setBounds(250,200,50,50);
b9.setBounds(100,250,50,50);
b0.setBounds(150,250,50,50);
b10.setBounds(200,250,50,50);
b11.setBounds(250,250,50,50);
b12.setBounds(100,300,50,50);
b13.setBounds(150,300,50,50);
b14.setBounds(200,300,50,50);
/*b2.setBounds(250,300,50,50);
b2.setBounds(150,150,50,50);
*/}
public void actionPerformed(ActionEvent e)
{
str1 = e.getActionCommand();
if(str1.equals("0"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("1"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("2"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("3"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("4"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("5"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("6"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("7"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("8"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("9"))
{
str2 = str2 + str1;
t1.setText(str2);
}
if(str1.equals("+"))
{
str = 0 +"";
str = t1.getText();
n1 = Integer.parseInt(str);
str3 = str1;
str2 = "";
}
if(str1.equals("-"))
{
n1 = Integer.parseInt(str2);
str3 = str1;
str2 = "";
}
if(str1.equals("*"))
{
n1 = Integer.parseInt(str2);
str3 = str1;
str2 = "";
}
if(str1.equals("/"))
{
n1 = Integer.parseInt(str2);
str3 = str1;
str2 = "";
}
if(str1.equals("="))
{
str2 = t1.getText();
n2 = Integer.parseInt(str2);
if(str3.equals("+"))
{
n3 = n1 + n2;
}
if(str3.equals("-"))
{
n3 = n1 + n2;
}
if(str3.equals("*"))
{
n3 = n1 + n2;
}
if(str3.equals("/"))
{
n3 = n1 + n2;
}
str3 = n3 + "";
t1.setText(str3);
}
}
}public class AppendAndInsertStringsDemo {
public static void main(String args[ ]) {
StringBuffer sb = new StringBuffer(30);
sb.append("Sofia Bulgaria.");
sb.insert(6, "is capital of ");
System.out.println(sb.toString());
String str = String.valueOf(Math.PI);
System.out.println("This is the string representation of " + str);
Float pi = Float.valueOf(str);
System.out.println("This is the number representation of " + pi);
}
}
import java.awt.*;
import java.applet.*;
public class applt extends java.applet.Applet
{
public void init()
{
resize(250,250);
}
public void paint(Graphics g)
{
Font myfont=new Font("Times new roman",Font.BOLD + Font.ITALIC,25);
g.setFont(myfont);
g.drawRect(100,100,300,450);
g.setColor(Color.orange);
g.fillRect(100,100,30,50);
g.setColor(Color.red);
g.drawString("hello world",120,120);
g.drawRect(100,100,300,450);
g.setColor(Color.green);
g.fillRect(150,150,30,50);
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class actlstn implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int ctr=1,no=0,fact=1;
Button bt;
bt=(Button)e.getSource();
if((bt.getLabel()).equals("compute"))
{
no=Integer.parseInt(appltfact.t1.getText());
while(ctr<=no)
{
fact*=ctr;
ctr++;
}
appltfact.t2.setText(String.valueOf(fact));
}
System.out.println("....");
}
}
public class appltfact extends Applet
{
static TextField t1,t2;
Label l1,l2;
Button b;
public void init()
{
l1=new Label("enter an integer.");
l2=new Label("fatorial val:");
t1=new TextField();
t2=new TextField(" ");
b=new Button("compute");
add(l1);
add(t1);
add(l2);
add(t2);
b.addActionListener(new actlstn());
add(b);
setSize(300,400);
setVisible(true);
}
public void paint(Graphics g)
{
showStatus("computing Factorial value...");
}
}
/*
*/
import java.applet.*;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.Panel;
class mmlstn implements MouseMotionListener
{
public void mouseDragged(MouseEvent m)
{
}
public void mouseMoved(MouseEvent m)
{
int x,y;
x=m.getX();
y=m.getY();
appltmouse.t.setText(String.valueOf(x)+" , "+String.valueOf(y));
}
}
public class appltmouse extends Applet
{
static TextField t;
public void init()
{
t=new TextField("........");
Panel p=new Panel();
p.add(t);
add(p);
//mouse event deligation
addMouseMotionListener(new mmlstn());
setSize(300,400);
setVisible(true);
}
public void paint(Graphics g)
{
g.drawRect(20,20,200,150);
}
}
/*
*/
import java.io.*;
class area
{
int x;
double y;
void cal(double pi,int ra)
{
y = pi*ra*ra;
System.out.println("Circle area is\t:"+y);
}
void cal(int len)
{
x = len*len;
System.out.println("Squre area is\t:"+x);
}
void cal(int len,int br)
{
x = len*br;
System.out.println("Rectangle area is\t:"+x);
}
}
class area1
{
public static void main(String args[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.print("choose any one \t 1.CIRCLE \t 2.SQURE \t 3.RECTANGLE\t:");
int a = Integer.parseInt(in.readLine());
area A = new area();
if(a==1)
{
System.out.print("Enter the radius\t:");
int b = Integer.parseInt(in.readLine());
A.cal(3.14,b);
}
else if(a==2)
{
System.out.print("Enter the length\t:");
int c = Integer.parseInt(in.readLine());
A.cal(c);
}
else
{
System.out.print("Enter the length\t:");
int d = Integer.parseInt(in.readLine());
System.out.print("Enter the breath\t:");
int e = Integer.parseInt(in.readLine());
A.cal(d,e);
}
}
}import java.io.*;
import java.lang.*;
class areae extends Exception
{
areae(String msg1)
{
super(msg1);
}
}
class area
{
int x;
double y;
void cal(double pi,int ra)
{
y = pi*ra*ra;
System.out.println("Circle area is\t:"+y);
}
void cal(int len)
{
x = len*len;
System.out.println("Squre area is\t:"+x);
}
void cal(int len,int br)
{
x = len*br;
System.out.println("Rectangle area is\t:"+x);
}
}
class area11
{
public static void main(String args[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.print("choose any one \t 1.CIRCLE \t 2.SQURE \t 3.RECTANGLE\t:");
try
{
int a = Integer.parseInt(in.readLine());
area A = new area();
if(a>=4 || a== 0)
throw new areae("Your Choice is Wrong");
if(a==1)
{
System.out.print("Enter the radius\t:");
int b = Integer.parseInt(in.readLine());
A.cal(3.14,b);
}
else if(a==2)
{
System.out.print("Enter the length\t:");
int c = Integer.parseInt(in.readLine());
if(c<=0)
throw new areae("Your length is worng");
A.cal(c);
}
else if(a==3)
{
System.out.print("Enter the length\t:");
int d = Integer.parseInt(in.readLine());
System.out.print("Enter the breath\t:");
int e = Integer.parseInt(in.readLine());
if(d
A.cal(d,e);
}
}
catch (areae x)
{
System.out.println(x.getMessage());
}
}
}class Array
{
public static void main(String arg[])
{
try
{
//int arr[]=new int[]{10,20,30,50,60};
int arr[]={10,20,30,40};
/*arr=new int[5];
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;*/
for(int i =0 ;i<=arr.length;i++)
System.out.print(arr[i]+"\t");
}
catch(Exception e)
{
System.out.println("error is ="+e);
}
}
}import java.io.*;
class Array1
{
public static void main(String arg[])
{
try
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(r);
int arr[];
arr=new int[5];
int total=0;
for(int j=0;j<=4;j++)
{
System.out.print("enter no"+j +"=");
arr[j]= Integer.parseInt(br.readLine());
}
for(int i =0 ;i
total=total+arr[i];
System.out.print(arr[i]+"\t");
}
int avg=total/5;
System.out.println("total = "+total +" avg="+avg);
}
catch(Exception e)
{
System.out.println("error is ="+e);
}
}
}import java.io.*;
import java.lang.*;
class arraye
{
public static void main(String args[])throws IOException
{
int a[] = new int [5];
int b;
DataInputStream in = new DataInputStream (System.in);
try
{
for(int i=0;i<5 i="" p="">{
System.out.print("choose the array location");
b = Integer.parseInt(in.readLine());
System.out.print("Enter the value");
a[b] = Integer.parseInt(in.readLine());
}
a[3] /= (a[1]+a[0])/(a[2]-a[4]);
System.out.println("C is :"+a[3]);
}
catch(ArithmeticException x)
{
System.out.println(" IT CAN'T DIVISIBLE BY ZERO");
}
catch(ArrayIndexOutOfBoundsException x)
{
System.out.println(" IT IS EXCEEDED THE LIMIT");
}
catch(ArrayStoreException x)
{
System.out.println(" THE VALUE CAN BE ENTER WRONG FORMAT");
}
}
}/*
- Arrays is a container object that holds fixed no. of values of the same datatype.
- Length of an array is defined when the array is created and remains fixed.
- Each item in the array is called as an element.
- Each element can be accessed by its index number. The index number starts from '0'.
*/
public class arrays
{
public static void main(String[] args)
{
int[] anArray;
//Declares an array of 10 integers
anArray = new int[5];
//Allocates the variable anArray with 4 integers
anArray[0] = 100;
anArray[1] = 200;
anArray[3] = 400;
anArray[2] = 300;
anArray[4] = 500;
//Defines the value at each index of the array
for(int i=0; i<=4; i++)
{
System.out.println("Element at index " + i + " = " + anArray[i]);
}
}
}/*
- 2D arrays are arrays where each element of the array is itself an array
- Visualize it as a matrix or a determinant
*/
import java.io.*;
class arrays2D
{
public static void main(String[] args)
{
String[][] names = {{"Dr.", "Mr."},
{"Anish", "Vivek"},
{"Shrinivasan", "Menon"}};
System.out.println("All possible combinations");
for(int i=0; i<=2; i++)
{
for(int y=0; y<=1; y++)
{
System.out.println("Row: " + i + "Column: " + y + " = " + names[i][y]);
}
}
}
}class assertDemo
{
void sqr(int num)
{
assert num>0:"-ve no. not allowed";
int s=num*num;
System.out.println("Sqr of "+num+"is" +s);
}
public static void main(String arg[])
{
assertDemo ad=new assertDemo();
ad.sqr(10);
ad.sqr(-8);
}
}class AssertTest
{
void sq(int n)
{
assert n>0:"Squiare is not allowed for (-)ve";
int s=n*n;
System.out.println("Square of"+n+" is "+s);
}
public static void main(String s[])
{
AssertTest a=new AssertTest();
a.sq(5);
a.sq(-2);
}
}import javax.swing.JOptionPane;
public class Average5Subjects
{
public static void main(String[] args)
{
double Average;
double sMath,sJava,sSociology,sHumanities,sPhysics;
sMath=Double.parseDouble(JOptionPane.showInputDialog("Input Grade in Math "));
sJava=Double.parseDouble(JOptionPane.showInputDialog("Input Grade in Java "));
sSociology=Double.parseDouble(JOptionPane.showInputDialog("Input Grade in Operating System "));
sHumanities=Double.parseDouble(JOptionPane.showInputDialog("Input Grade in Fil "));
sPhysics=Double.parseDouble(JOptionPane.showInputDialog("Input Grade in Physics "));
Average=((sMath + sJava + sSociology+sHumanities+sPhysics)/5);
JOptionPane.showMessageDialog(null,"Average is " + Average);
}
}
import java.io.*;
class bank
{
String name;
int accno;
String type;
int balance;
bank(int accno1,String name1,String type1)
{
accno = accno1;
name = name1;
type = type1;
}
void deposit(int accno1,int amt)
{
if(accno == accno1)
balance +=amt;
}
void withdraw(int accno1,int amt)
{
if(accno == accno1)
{
if(balance>=amt)
balance -=amt;
else
System.out.println("YOU CAN NOT WITH DRAW");
}
}
void display(int accno1)
{
if(accno == accno1)
System.out.println("Name :"+name+" balance :"+balance) ;
}
}
class banka
{
public static void main(String args[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
int accno1,i;
String name1,type1;
bank [] B = new bank [10];
for(i=0;i<3 i="" p="">{
accno1 = Integer.parseInt(in.readLine());
name1 = in.readLine();
type1 = in.readLine();
B[i] = new bank (accno1,name1,type1);
}
System.out.println("enter the acc no");
int an1 = Integer.parseInt(in.readLine());
int y=1;
do{
System.out.println("1.deposit 2.with draw 3.balance");
int cho = Integer.parseInt(in.readLine());
System.out.println("enter the amount");
int amt1 = Integer.parseInt(in.readLine());
for( i=0;i<3 i="" p="">{
if (cho == 1)
{
B[i].deposit(an1,amt1);
}
else if (cho == 2)
{
B[i].withdraw(an1,amt1);
}
else
{
B[i].display(an1);
}
}
System.out.println("transaction 1");
y = Integer.parseInt(in.readLine());
}while (y==1);
}
}import java.io.*;
class Bank1Exception extends Exception
{
int num=0;
Bank1Exception()
{
System.out.println("Default of myexception"+num);
}
Bank1Exception(int n)
{
num=n;
System.out.println("Entered amount is="+num);
}
public String toString()
{
return"Balance is not Enough"+num;
}
}
class BankException
{
int bal=1000;
void wid(int num)throws Bank1Exception
{
if(bal-num<500 p=""> {
throw new Bank1Exception(num);
}
else
{
bal=bal-num;
System.out.println("Balance is "+bal);
}
}
void dep(int num)
{
bal=bal+num;
System.out.println("Balance is "+bal);
}
int getbal()
{
return(bal);
}
public static void main(String arg[])
{
BankException ce=new BankException();
try{
char aa='y';
while(aa=='y' || aa=='Y')
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println(" Enter 1 for Deposit\n Enter 2 for Witdraw \n Enter 3 for Balance Enqu ");
String str1=br.readLine();
int ch=Integer.parseInt(str1);
System.out.println("Enter Amount ");
String str2=br.readLine();
int num=Integer.parseInt(str2);
switch(ch)
{
case 1:
ce.dep(num);
break;
case 2:
ce.wid(num);
break;
case 3:
int x=ce.getbal();
System.out.println(" Amount is "+x);
break;
default:System.out.println("Enter correct Choice ");
}
System.out.println("Want to Cont. enter y");
aa=(char)br.read();
}
}catch(Bank1Exception me)
{
System.out.println("Caught inside main"+me);
me.printStackTrace();
}
catch(Exception e)
{
System.out.println("Error is "+e);
}
}
}
import java.io.*;
class BankingDemo
{
int total_amount=500;
void deposit(int amount)
{
total_amount=total_amount+amount;
System.out.println("Total_amount is "+total_amount);
}
void withdrawn(int amount) throws BankingException
{
int am=total_amount-amount;
if(am<500 p=""> {
throw new BankingException(amount);
}
else
{
System.out.println(amount+" is withdrawn from "+total_amount);
total_amount=total_amount-amount;
}
}
int getbalance()
{
return total_amount;
}
public static void main(String s[]) throws BankingException
{
BankingDemo obj=new BankingDemo();
char c;
try
{
do
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("1. For Deposit\n2.For WithDrawn \n3. For Balance");
System.out.println("Enter Your Choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the amount");
int amt=Integer.parseInt(br.readLine());
obj.deposit(amt);
break;
case 2:
System.out.println("Enter the to be WithDrawn");
int wh=Integer.parseInt(br.readLine());
obj.withdrawn(wh);
break;
case 3:
int balance=obj.getbalance();
System.out.println("Total amount balance is "+balance);
break;
default :
System.out.println("Your choice is wrong");
}
System.out.println("Do u want to continue(y/n)");
c=(char)br.read();
}while(c=='y' || c=='Y');
}catch(Exception e)
{
System.out.println("Invalid exception"+e);
}
}
}
class BankingException extends Exception
{
int n;
BankingException()
{
n=0;
System.out.println("Default constructor");
}
BankingException(int n)
{
this.n=n;
//System.out.println("Parametarize constructor");
}
public String toString()
{
return "U can't withdrawn"+n;
}
}class C1
{
public static void main(String args[])
{
System.out.println("main() in C1...");
}
}
class C2
{
public static void main(String args[])
{
System.out.println("main() in C2...");
}
}
/* Note: This code will compile successfully & generate C1.class & C2.class.
But it won't execute. Runtime Error occurs. - ClassDefinitionNotFound.
Class C1C2 not found
*/
// A simple command-line calculator
/* IMPORTANT: To perform multiplication, enclose * within a pair of
double-quotes. e.g.
java Calc "*" 4 5
*/
public class Calc
{
public static void main(String args[])
{
char op = args[0].charAt(0);
System.out.println("Operation : "+op);
int a = Integer.parseInt(args[1]);
int b = Integer.parseInt(args[2]);
switch(op)
{
case '+':
System.out.println(a+b);
break;
case '-':
System.out.println(a+b);
break;
case '*':
System.out.println(a*b);
break;
case '/':
System.out.println(a/b);
break;
default:
System.out.println("Invalid Choice");
}
}
}public class CharacterDemo {
public static void main(String args[]) {
Character a1 = new Character('a');
Character a2 = new Character('a');
Character b = new Character('b');
int difference = a1.compareTo(b);
if (difference == 0) {
System.out.println("a1 is equal to b.");
} else if (difference < 0) {
System.out.println("a1 is less than b.");
} else if (difference > 0) {
System.out.println("a1 is greater than b.");
}
System.out.println("a1 is "
+ ((a1.equals(a2)) ? "equal" : "not equal")
+ " to a2.");
System.out.println("The character " + a1.toString() + " is "
+ (Character.isUpperCase(a1.charValue()) ? "upper" : "lower")
+ "case.");
}
}class CharFunc
{
public static void main(String args[])
{
char c1 = '9';
char c2 = '*';
if(Character.isDigit(c1)==true)
System.out.println("c1 a digit");
else
System.out.println("c1 is NOT a digit");
if(Character.isLetterOrDigit(c2)==true)
System.out.println("c2 is either a digit or letter");
else
System.out.println("c2 is neither a digit nor a letter");
}
}
/*
- Clock. The name says it all
*/
import java.io.*;
import java.util.*;
class clock
{
public static void main(String[] args)
{
Date d = new Date();
System.out.println("Date is " + d);
}
}class Comparing{
public static void main(String[] args) {
int a=24, b=25;
if (a == b){
System.out.println("Both are equal");
}
else if(a>b){
System.out.println("a is greater than b");
}
else{
System.out.println("b is greater than a");
}
}
}
class ConstructTest
{
int x=2;
ConstructTest()
{
System.out.println("This is Default constructor");
}
ConstructTest(int x1)
{
x=x1;
System.out.println("This is Paraterized consturctor x="+x);
}
void Square()
{
int s=x*x;
System.out.println("Sqaure of "+x+"is:"+s);
}
public static void main(String arg[])
{
ConstructTest t1=new ConstructTest();
ConstructTest t2=new ConstructTest(10);
t1.Square();
t2.Square();
}
}
public class Conversion
{
public static void main(String args[])
{
// int i =10.45F; ERROR-Possible loss of precision
int i = (int)10.45F; // OK - Explicit Casting
System.out.println(i);
i='A'; // OK - Implicit Casting
System.out.println(i);
// char ch = i; ERROR-Possible loss of precision
char ch = (char) i; // OK - Explicit Casting
System.out.println(ch);
ch++; // OR ch=66; Allowed
System.out.println(ch);
// ch=ch/11; ERROR-Possible loss of precision
float f = i; // OK
System.out.println(f);
f = ch; // OK
System.out.println(f);
byte b1=10,b2=20;
/* byte b3 = b1+b2;
ERROR bcoz the arithemetic is carried out in integer mode only.
*/
}
}
class MyException extends Exception
{
int num=0;
MyException()
{
System.out.println("Default of myexception"+num);
}
MyException(int n)
{
num=n;
System.out.println("Parametrized constructor of myexception"+num);
}
public String toString()
{
return"Sqr is not allowed for -ve no."+num;
}
}
class CustomException
{
void Sqr(int num)throws MyException
{
if(num<0 p=""> {
throw new MyException(num);
}
else
{
int s=num*num;
System.out.println("Sqr of "+num+" is "+s);
}
}
public static void main(String arg[])
{
CustomException ce=new CustomException();
try{
ce.Sqr(10);
ce.Sqr(-9);
}catch(MyException me)
{
System.out.println("Caught inside main"+me);
me.printStackTrace();
}
}
}
class DefaultHandling1
{
public static void main(String s[])
{
try
{
int a=10;
int b=0;
System.out.println("a="+a+"\n b="+b);
int c=a/b;
System.out.println("result is "+c);
}
catch(Exception e)
{
System.out.println("Error is "+e);
e.printStackTrace();
}
System.out.println("hello ");
}
} class DefHand
{
public static void main(String arg[])
{
try{
int a=10,b=20;
System.out.println("Value of a="+a+"and b="+b);
int c=a/b;
System.out.println("Result ="+c);
}catch(Exception ae)
{
System.out.println("Error is"+ae);
}finally
{
System.out.println("Hello");
}
}
}
import java.net.*;
import java.io.*;
public class echo
{
public static void main(String argv[])
{
Socket es=null;
PrintStream os=null;
DataInputStream is=null;
DataInputStream stdin=new DataInputStream(System.in);
try
{
es=new Socket("127.0.0.1",17);
is=new DataInputStream(es.getInputStream());
os=new PrintStream(es.getOutputStream());
}
catch (Exception e)
{
System.err.println(e);
}
if(es!=null && os!=null && is!=null)
{
try
{
String userip;
while((userip=stdin.readLine())!=null)
{
os.println(userip);
String serverip =is.readLine();
System.out.println("echo: " + serverip);
}
os.close();
is.close();
es.close();
}
catch(Exception e)
{
System.err.println(e);
}
}
}
}
0>500>500>3>3>5>
No comments:
Post a Comment