****************************Ass:4.Set:A.Que:1*********************************
Que) 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.
*******************************Java Code**********************
import java.io.*;
class AgeNotWithinRangeException extends Exception
{
AgeNotWithinRangeException(String s)
{
super(s);
}
}
class NameNotValidException extends Exception
{
NameNotValidException(String s)
{
super(s);
}
}
class student
{
int rno;
String name;
int age;
String course;
student(int rno,String name,int age,String course)
{
this.rno=rno;
this.name=name;
this.age=age;
this.course=course;
}
void display()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("rollno :"+rno+"\n name:"+name+"\n age:"+age+"\n course:"+course+"\n");
}
}
class studentdemo
{
public static void main(String args[])throws IOException
{
student s1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s1= new student(1,"dam",89,"science");
s1.display();
System.out.print("Enter the roll number:");
int rno=Integer.parseInt(br.readLine());
try
{
System.out.println("Enter the student name:");
String name=br.readLine();
for(int i=0;i<name.length();i++)
if(Character.isDigit(name.charAt(i)))
throw new NameNotValidException("Name Not Valid");
}
catch( NameNotValidException e)
{
System.out.println(e);
}
try
{
System.out.println("Enter the age of student:");
int age=Integer.parseInt(br.readLine());
if(age<15 || age>20)
throw new AgeNotWithinRangeException("AgeNot within the range");
}
catch(AgeNotWithinRangeException e)
{
System.out.println(e);
}
System.out.println("Enter the course:");
String course=br.readLine();
}
}
********************************Output***************************************
[root@localhost 3017java]#java setA1
Enter the roll number:
2453
Enter the student name:
Ramesh
Enter the age of student:
20
Enter the course:
BCS
Age is not Within Range
[root@localhost 3017java]#java setA1
Enter the roll number:
2435
Enter the student name:
Pooja@
Enter the age of student:
21
Enter the course:
BSc
Name is not valid
********************************************************************************
****************************Ass:4.Set:A.Que:2*********************************
Que)A program accepts two integers as command line arguments. It displays all prime
numbers between these two. Using assertions, validate the input for the following
criteria: Both should be positive integers. The second should be larger than the first.
*********************************Java Code***********************************
import java.io.*;
class primeint
{
public static void main(String args[])
{
int num;
int flag=0;
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
assert((a>0 && b>0) && (b>a)) : "INVALID INPUT";
num=a;
System.out.println("Prime no's between "+a+" & "+b+" are :");
while(num<=b)
{
flag=1;
int temp,j;
temp=num/2;
for(j=2;j<=temp;j++)//num<b;)
if((num%j)==0)
flag=0;
if(flag==1)
{
System.out.println(num);
}
num++;
}
}
}
**********************************************************************
Output:
C:\>java primeint 1 50
Prime no's between 1 & 50 are :
1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
******************************************************************************
****************************Ass:4.Set:B.Que:1*********************************
Que) Define class MyDate with members day, month, year. Define default and
parameterized constructors. Accept values from the command line and create a date
object. Throw user defined exceptions – “InvalidDayException†or
“InvalidMonthException†if the day and month are invalid. If the date is valid,
display message “Valid dateâ€.
*********************************Java Code***********************************
class InvalidDayException extends Exception
{
public String toString()
{
return "Invalid Date given";
}
}
class InvalidMonthException extends Exception
{
public String toString()
{
return "Invalid Month given";
}
}
class MyDate
{
public static void main(String[]args)
{
try
{
if(args.length<3)
throws new NullPointerExcaption();
else
{
int dd=Integer.parseInt(args[0]);
int mm=Integer.parseInt(args[1]);
int yy=Integer.parseInt(args[2]);
boolean leap=((yy%400==0)||(yy%4==0 && yy%100!=0));
if(mm < 1||mm >12)
throw new InvalidMonthException();
switch(mm)
{
case 1:
case 2:
if(leap && dd>29)
throw new InvalidDayException();
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
if(dd < 1||dd>30)
throw new InvalidDayException();
case 11:
case 12:
if(dd < 1||dd >31)
throw new InvalidDayException();
}
System.out.println("Valid input");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
******************************Output*****************************************
[ty14@bcs92 ass4]$ java MyDate
java.lang.NullPointerException
[ty14@bcs92 ass4]$ java MyDate 23 5 2000
Valid input
[ty14@bcs92 ass4]$ java MyDate 23 15 2000
Invalid Month given
[ty14@bcs92 ass4]$ java MyDate 30 2 2000
Invalid Date given
*****************************************************************************
****************************Ass:4.Set:B.Que:2*********************************
Que) Write a program which accept two integers and an arithmetic operator from the
command line and performs the operation. Fire the following user defined exceptions:
i. If the no of arguments are less than 3 then fire “IllegalNumberOfArgumentsâ€
ii. If the operator is not an Arithmetic operator, throw “InvalidOperatorExceptionâ€.
iii. If result is –ve, then throw “NegativeResultExceptionâ€
*********************************Java Code***********************************
import java.io.*;
class NegativeNumberException extends Exception
{
NegativeNumberException(int n)
{
System.out.println("Negative input:"+n);
}
}
public class Exception Test
{
public static void main(String[]args)
{
int num,i,sum=0;
try
{
num=Integer.parseInt(args[0]);
if(num<0)
throw new NegativeNumberException(num);
for(i=0;i<num;i++)
sum=sum+i;
}
catch(NumberFormatException e)
{
System.out.println("Invalid Format:");
}
catch(NegativeNumberException e)
{ }
finally
{
System.out.println("The sum is:"+sum);
}
}
}
********************************Output***************************************
[ty14@bcs95 ass4]$java ExceptionTest abc
Invalid format
The sum is:0
[ty14@bcs95 ass4]$java ExceptionTest -3
Negative input:-3
The sum is:0
[ty14@bcs95 ass4]$java ExceptionTest 10
The sum is:45
**************************************************************************
Que) 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.
*******************************Java Code**********************
import java.io.*;
class AgeNotWithinRangeException extends Exception
{
AgeNotWithinRangeException(String s)
{
super(s);
}
}
class NameNotValidException extends Exception
{
NameNotValidException(String s)
{
super(s);
}
}
class student
{
int rno;
String name;
int age;
String course;
student(int rno,String name,int age,String course)
{
this.rno=rno;
this.name=name;
this.age=age;
this.course=course;
}
void display()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("rollno :"+rno+"\n name:"+name+"\n age:"+age+"\n course:"+course+"\n");
}
}
class studentdemo
{
public static void main(String args[])throws IOException
{
student s1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s1= new student(1,"dam",89,"science");
s1.display();
System.out.print("Enter the roll number:");
int rno=Integer.parseInt(br.readLine());
try
{
System.out.println("Enter the student name:");
String name=br.readLine();
for(int i=0;i<name.length();i++)
if(Character.isDigit(name.charAt(i)))
throw new NameNotValidException("Name Not Valid");
}
catch( NameNotValidException e)
{
System.out.println(e);
}
try
{
System.out.println("Enter the age of student:");
int age=Integer.parseInt(br.readLine());
if(age<15 || age>20)
throw new AgeNotWithinRangeException("AgeNot within the range");
}
catch(AgeNotWithinRangeException e)
{
System.out.println(e);
}
System.out.println("Enter the course:");
String course=br.readLine();
}
}
********************************Output***************************************
[root@localhost 3017java]#java setA1
Enter the roll number:
2453
Enter the student name:
Ramesh
Enter the age of student:
20
Enter the course:
BCS
Age is not Within Range
[root@localhost 3017java]#java setA1
Enter the roll number:
2435
Enter the student name:
Pooja@
Enter the age of student:
21
Enter the course:
BSc
Name is not valid
********************************************************************************
****************************Ass:4.Set:A.Que:2*********************************
Que)A program accepts two integers as command line arguments. It displays all prime
numbers between these two. Using assertions, validate the input for the following
criteria: Both should be positive integers. The second should be larger than the first.
*********************************Java Code***********************************
import java.io.*;
class primeint
{
public static void main(String args[])
{
int num;
int flag=0;
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
assert((a>0 && b>0) && (b>a)) : "INVALID INPUT";
num=a;
System.out.println("Prime no's between "+a+" & "+b+" are :");
while(num<=b)
{
flag=1;
int temp,j;
temp=num/2;
for(j=2;j<=temp;j++)//num<b;)
if((num%j)==0)
flag=0;
if(flag==1)
{
System.out.println(num);
}
num++;
}
}
}
**********************************************************************
Output:
C:\>java primeint 1 50
Prime no's between 1 & 50 are :
1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
******************************************************************************
****************************Ass:4.Set:B.Que:1*********************************
Que) Define class MyDate with members day, month, year. Define default and
parameterized constructors. Accept values from the command line and create a date
object. Throw user defined exceptions – “InvalidDayException†or
“InvalidMonthException†if the day and month are invalid. If the date is valid,
display message “Valid dateâ€.
*********************************Java Code***********************************
class InvalidDayException extends Exception
{
public String toString()
{
return "Invalid Date given";
}
}
class InvalidMonthException extends Exception
{
public String toString()
{
return "Invalid Month given";
}
}
class MyDate
{
public static void main(String[]args)
{
try
{
if(args.length<3)
throws new NullPointerExcaption();
else
{
int dd=Integer.parseInt(args[0]);
int mm=Integer.parseInt(args[1]);
int yy=Integer.parseInt(args[2]);
boolean leap=((yy%400==0)||(yy%4==0 && yy%100!=0));
if(mm < 1||mm >12)
throw new InvalidMonthException();
switch(mm)
{
case 1:
case 2:
if(leap && dd>29)
throw new InvalidDayException();
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
if(dd < 1||dd>30)
throw new InvalidDayException();
case 11:
case 12:
if(dd < 1||dd >31)
throw new InvalidDayException();
}
System.out.println("Valid input");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
******************************Output*****************************************
[ty14@bcs92 ass4]$ java MyDate
java.lang.NullPointerException
[ty14@bcs92 ass4]$ java MyDate 23 5 2000
Valid input
[ty14@bcs92 ass4]$ java MyDate 23 15 2000
Invalid Month given
[ty14@bcs92 ass4]$ java MyDate 30 2 2000
Invalid Date given
*****************************************************************************
****************************Ass:4.Set:B.Que:2*********************************
Que) Write a program which accept two integers and an arithmetic operator from the
command line and performs the operation. Fire the following user defined exceptions:
i. If the no of arguments are less than 3 then fire “IllegalNumberOfArgumentsâ€
ii. If the operator is not an Arithmetic operator, throw “InvalidOperatorExceptionâ€.
iii. If result is –ve, then throw “NegativeResultExceptionâ€
*********************************Java Code***********************************
import java.io.*;
class NegativeNumberException extends Exception
{
NegativeNumberException(int n)
{
System.out.println("Negative input:"+n);
}
}
public class Exception Test
{
public static void main(String[]args)
{
int num,i,sum=0;
try
{
num=Integer.parseInt(args[0]);
if(num<0)
throw new NegativeNumberException(num);
for(i=0;i<num;i++)
sum=sum+i;
}
catch(NumberFormatException e)
{
System.out.println("Invalid Format:");
}
catch(NegativeNumberException e)
{ }
finally
{
System.out.println("The sum is:"+sum);
}
}
}
********************************Output***************************************
[ty14@bcs95 ass4]$java ExceptionTest abc
Invalid format
The sum is:0
[ty14@bcs95 ass4]$java ExceptionTest -3
Negative input:-3
The sum is:0
[ty14@bcs95 ass4]$java ExceptionTest 10
The sum is:45
**************************************************************************
ReplyDeleteReport Bugs Topic tells about the bug reports of this blogs....
Java Course in Chennai