/ *
Create a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “AgeNotWithinRangeException”. If name contains numbers or special symbols raise exception “NameNotValidException”. Define the two exception classes.
*/
import
java.lang.Exception;
class
AgeNotWithinRange extends Exception{
String
msg; AgeNotWithinRange(String m) {
this.msg = m;
}
public
String toString(){
return this.getClass().getName()+":"+msg;
}
}
class NameNotValidException extends
Exception{
String
msg; NameNotValidException(String m) {
this.msg = m;
}
public
String toString(){
return this.getClass().getName()+":"+msg;
}
}
class Student{
int
rollno;
int
age;
String
name;
String
course;
Student(int
r,String n, int a, String c) throws AgeNotWithinRange,NameNotValidException{
if(a < 15 || a > 21){
throw new
AgeNotWithinRange(String.valueOf(a));
}
for(int i = 0;i < n.length();i++){
if(!(Character.isLetter(n.charAt(i)))){
throw new NameNotValidException(String.valueOf(i));
}
}
this.rollno = r;
this.name = n;
this.age = a;
this.course = c;
System.out.println("Object Created
successfully.");
}
public static void main(String args[]){
try{
Student s1 = new
Student(Integer.parseInt(args[0]),args[1],Integer.parseInt(args[2]),args[3]);
}
catch(AgeNotWithinRange abc){
System.out.println(abc);
}
catch(NameNotValidException def){
System.out.println(def);
}
finally{
System.out.println("Congratulation now you are
UG.");
}
try{
Student s2 = new Student(Integer.parseInt(args[4]),args[5],Integer.parseInt(args[6]),args[7]);
}
catch(AgeNotWithinRange abc){
System.out.println(abc);
}
catch(NameNotValidException def){
System.out.println(def);
}
finally{
System.out.println("Congratulation now you are PG.");
}
}
}
class NameNotValidException extends Exception{
class Student{
int rollno;
public static void main(String args[]){
try{
}
try{
}
Developed at : MCA - 3 (A), Department of MCA, B H Gardi College, Rajkot
No comments:
Post a Comment