Java Programs
Chapter 4:
Q1] Write a program to accept password from user and throw ‘Authentication
failure’ exception if password is incorrect.(8M)(W-15)
import [Link].*;
class Wrong_password extends Exception{
String pass;
Wrong_password(String a){
pass=a;
public String toString()
return("authentication failure"+pass);
class passworddemo
public static void main(String arg[])
{
String pass,pass1;
Scanner s1=new Scanner([Link]);
[Link]("enter the password=");
pass=[Link]();
[Link]("Re-enter the password=");
pass1=[Link]();
if(!([Link](pass1)==0))
try
throw new Wrong_password(pass1);
catch(Wrong_password e)
[Link](e);
else
[Link]("set password="+pass);
}
Q2]
Define
an
exception called ‘No match Exception’ that is thrown when a string is not equal to
“MSBTE”. Write program. (8M) (W-16)
import [Link].*;
class No_match_Exception extends Exception{
String pass;
No_match_Exception(String str2){
pass=str2;
}
public String toString(){
return("String is Not Equals to MSBTE="+pass);
}
}
class MSBTE{
public static void main(String arg[]){
String str1="MSBTE";
String str2;
Scanner s=new Scanner([Link]);
[Link]("Enter String :=");
str2=[Link]();
if(!([Link](str2)==0)){
try{
throw new No_match_Exception(str2);
}
catch(No_match_Exception e){
[Link](e);
}
}
else{
[Link](" String is Equals To:="+str1);
}
}
}
import [Link].*;
class Even_No_Excetion extends Exception{
int n;
Even_No_Excetion(int a){
n=a;
}
public String toString(){
return("Even Number Exception="+n);
}
}
class EvenDemo{
public static void main(String arg[]){
int no;
Scanner s=new Scanner([Link]);
[Link]("Enter Any Integer No. :=");
no=[Link]();
if(no%2==0){
try{
throw new Even_No_Excetion(no);
}
catch(Even_No_Excetion e){
[Link](e);
}
}
else{
[Link](" Odd No:="+no);
}
}
}