Saturday, 22 March 2014

Write 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 “Age Not Within The Range”.If name contains numbers or special symbols raise exception “Name not valid”.

import java.io.*;
class AgeNotWithInRangeException extends Exception
{
    public String toString()
    {
        return("Age is not between 15 and 21 ... Please ReEnter the Age");
    }
}

class NameNotValidException extends Exception
{
    public String validname()
    {
        return("Name is not Valid ... Please ReEnter the Name");
    }
}


class Student
{
    int roll,age;
    String name,course;
    Student()
    {
        roll=0;
        name=null;
        age=0;
        course=null;
    }
    Student(int r,String n,int a,String c)
    {
        roll=r;
        course=c;
        int l,temp=0;
        l=n.length();
        for(int i=0;i<l;i++)
        {
            char ch;
            ch=n.charAt(i);
            if(ch<'A' || ch>'Z' && ch<'a' || ch>'z')
            temp=1;
        }
                /*———-Checking Name——————–*/
        try
        {
            if(temp==1)
                throw new NameNotValidException();
            else
                name=n;
        }
        catch(NameNotValidException e2)
        {
            System.out.println(e2);
        }
                /*———-Checking Age——————–*/
        try
        {
            if(a>=15 && a<=21)
                age=a;
            else
                throw new AgeNotWithInRangeException();
        }
        catch(AgeNotWithInRangeException e1)
        {
            System.out.println(e1);
        }
    }
    void display()
    {
        System.out.println("Roll\tName\tAge\tCourse");
        System.out.println("----------------------------------");
        System.out.println(roll+"\t"+name+"\t"+age+"\t"+course);
    }
}

class StudentDemo
{
    public static void main(String args[])throws IOException
    {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int r,a;
        String n,c;

        System.out.println("Enter Roll,Name,Age,Course");

        r=Integer.parseInt(br.readLine());
        n=br.readLine();
        a=Integer.parseInt(br.readLine());
        c=br.readLine();
        Student s=new Student(r,n,a,c);
        s.display();
    }
}




D:\Rushi>javac StudentDemo.java

D:\Rushi>java StudentDemo
Enter Roll,Name,Age,Course
1
Akash
21
MCA
Roll    Name    Age     Course
-------------------------------
1       Akash   21      MCA

No comments:

Post a Comment