Oct

19

import java.io.*;
class Ex{
public static void main(String args[]){
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
try{
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
System.out.println(n1+n2);
System.out.println(n1-n2);
System.out.println(n1*n2);
System.out.println(n1/n2);
}
catch(ArithmeticException e){
System.out.println(e);
}
finally{
System.out.println(“Finally Called”);
}
System.out.println(“Exit from main”);
}
}

————————————————————————————–

import java.io.*;
class Ex1{
public static void main(String args[]){
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
try{
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
System.out.println(n1+n2);
System.out.println(n1-n2);
System.out.println(n1*n2);
System.out.println(n1/n2);
}
catch(Exception e){  // it will generate compile error
System.out.println(e);
}
catch(ArithmeticException e){
System.out.println(e);
}
catch(NullPointerException e){
System.out.println(e);
}

finally{
System.out.println(“Finally Called”);
}
System.out.println(“Exit from main”);
}
}

————————————————————————————-

import java.io.*;
class Ex2{
public static void main(String args[]){
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
if(n1<=5){
//throw new NumberFormatException(“Explict Thrown <=5”);
try{
throw new InterruptedException(“Explict Intruupt”);
}catch(InterruptedException i){
}
}
System.out.println(n1+n2);
}
}

——————————————————————————————-

import java.io.*;
class Ex3{
public static void main(String args[])throws InterruptedException{
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
if(n1<=5){
//throw new NumberFormatException(“Explict Thrown <=5”);
throw new InterruptedException(“Explict Intruupt”);
}
System.out.println(n1+n2);
}
}

——————————————————————————————————

import java.io.*;
class Ex4{
public static void main(String args[])throws MyExcep{
System.out.println(“Thows Exception”);
int i=0;
if(i==0)
throw new MyExcep(“This exception is created by programmer”);
System.out.println(“Exit from main”);
}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

——————————————————————————————————-

import java.io.*;
class Ex5{
public static void main(String args[]){
System.out.println(“Thows Exception”);
int i=0;
try{
if(i==0)
throw new MyExcep(“This exception is created by programmer”);
}catch(MyExcep e){
}
System.out.println(“Exit from main”);
}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

—————————————————————————————————-

import java.io.*;
class Ex6{
public static void main(String args[]){
System.out.println(“Thows Exception”);
Ex6 e6=new Ex6();
try{
e6.m1();
}catch(MyExcep e){
}
System.out.println(“Exit from main”);
}
void m1()throws MyExcep{
int i=0;
if(i==0)
throw new MyExcep(“This exception is created by programmer”);

}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

————————————————————————————————————-

import java.io.*;
class Ex7{
public static void main(String args[]){
}
void m1()throws MyExcep{
}
void m2() throws Exception{
}
void m3() throws NumberFormatException{
}
}
class Ex extends Ex7{
void m1() throws NumberFormatException{
}
void m3()throws NumberFormatException,NullPointerException{
}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

 

 

Oct

17

class TestString2{
public static void main(String args[]){
String s1=”ABC”;
String s2=new String(“ABC”);
String s3=”abc”;
String s4=” AB C “;
String s5=”abcabcabc”;
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s4.equalsIgnoreCase(s3));
System.out.println(s1.length());
System.out.println(s4.length());
System.out.println(s4.trim().length());
System.out.println(s3.toUpperCase());
//s3=s3.toUpperCase();
System.out.println(s3);
System.out.println(s1.toLowerCase());
System.out.println(s3.charAt(1));
System.out.println(s3.indexOf(‘b’));
System.out.println(s5.indexOf(‘b’));
System.out.println(s5.lastIndexOf(‘b’));
System.out.println(s5.indexOf(‘b’,5));
System.out.println(s5.indexOf(“ab”));
System.out.println(s5.indexOf(“ab”,3));
System.out.println(s5.lastIndexOf(‘a’,3));
System.out.println(s5.lastIndexOf(‘a’,2));
System.out.println(s5.lastIndexOf(‘a’,7));
}
}

——————————————————————————————

class TestString1{

public static void main(String args[]){
char ch[]={‘a’,’b’,’c’,’d’};
String s1=new String(“ABC”);
String s2=”ABC”;
String s3=new String(“ABC”);
String s4=new String(s3);
String s5=new String();
String s6=new String(ch);
System.out.println(s1+” “+s2+” “+s3+” “+s4+” “+s5+” “+s6);
if(s1==s2){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s1==s3){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
}
}

——————————————————————————————————-

class T1{
public static void main(String arg[]){
String s1=”ABC”;
String s3=”ABC”;
String s2=new String(“ABC”);
String s4=new String(s2);
if(s1==s3){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s1==s2){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s2==s4){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s1.equals(s4)){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
}
}

————————————————————————————————–

import java.io.*;

class StringTest1{
public static void main(String args[]){
Console c=System.console();
String st1=c.readLine();
String st2=st1.toUpperCase();
System.out.println(st2);
System.out.println(st1.toLowerCase());
System.out.println(“——————“);
for(int i=0;i<st1.length();i++){ System.out.println(st1.charAt(i)); } for(int i=st1.length()-1;i>=0;i–){
System.out.print(st1.charAt(i));
}
int ctr=0;
for(int i=0;i<st1.length();i++){
char ch=st1.charAt(i);
switch(ch){
case ‘a’:case ‘e’:case ‘i’:case ‘o’:case ‘u’:
case ‘A’:case ‘E’:case ‘I’:case ‘O’:case ‘U’:
System.out.print(“\n”+ch);
ctr++;
}
}
System.out.println(“\n”+ctr);
}
}

————————————————————————————————-

import java.io.*;

class StringTest2{
public static void main(String args[]){
Console c=System.console();
String st1=c.readLine();
System.out.println(st1.substring(3));//from 3rd index to length
System.out.println(st1.substring(3,5));// from 3rd index to 5-1index
}
}

—————————————————————————————————-

import java.io.*;

class StringTest3{
public static void main(String args[]){
Console c=System.console();
String st1=c.readLine(“Enter File Name”);
int i=st1.lastIndexOf(‘.’);
String s=st1.substring(i+1);
System.out.println(“File Type is ” + s);
}
}

Oct

09

Java Interface Examples

 

Filled Under: Java (Core)