Sie sind auf Seite 1von 16

JAVA PRACTICAL

ASSIGNMENT
SESSION-10
1.Create a class called ClassThrow that creates and throws an
Arithmetic exception when user inputs any negative numbers.

class Classthrow
{
public static void main(String args[])
{
int a;
try
{
a=Integer.parseInt(args[0]);
}
catch(ArithmaticException e)
{
System.out.println("negative no."+e);
}
}
}
2.Create a user defined Exception class called MyException
that will raise an exception if any user gives a salary value less
than 1000. The program should implement “throws”

class MyException
{
public static void main(String args[])
{
int a;
try
{
a=Integer.parseInt(args[0]);
if(a<1000)
throw new Exception();
}
catch(Exception e)
{
System.out.println("salary value less than 1000"+e);
}
}
}
JAVA PRACTICAL
ASSIGNMENT
SESSION-9
1.Write a class Excep1 that will receive a command line
argument and divide a certain number by the argument. If the
argument is zero the it will raise appropriate exception

class Excep
{
public static void main(String args[])
{
int a,b,c;
try
{
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=a/b;
}
catch(Exception e)
{
System.out.println("divide by zero"+e);
}
}
}
2.Write a program that will implement multiple catch using
ArrayIndexOutOfBounds, ArithmeticException. Put a
“finally” block which raises “I am Finally” message.

class My
{
public static void main(String args[])
{
int a,b,c;
try
{
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
if(b<0)
throw new Exception();
c=b/a;
}
catch(ArithmeticException e)
{
System.out.println("divide by zero"+e);
}
catch(Exception e)
{
System.out.println("b<0"+e);
}
finally
{
System.out.println("i m finally");
}
}
}
JAVA PRACTICAL
ASSIGNMENT
SESSION-8
1.Write a class that has the following members:
An int[] of 10 elements: arr[10]
Void add(int) to add integers into that array sequentially.
Int out(void) to take out the integers from that array following
LIFO pattern.
Write a menu-driven program to implement that class

class A
{
public static void main(String args[])
{
B b=new B();
int size=Integer.parseInt(args[0]);
int arr[]=new int[size];
int sum=0;
for(int i=0; i<size; i++)
{
arr[i]=Integer.parseInt(args[i+1]);
}
int c;
b. add(arr,size);
c=b.out(arr,size);
}
}
class B
{
void add(int a[], int size)
{
int sum=0;
for(int i=0; i<size; i++)
{
sum+=a[i];
}
System.out.println("sum:"+sum);
}
int out(int arr[],int size)
{
System.out.println("out in lifo pattern:");
for(int i=size; i>0; i--)
{
System.out.println(arr[i]);
}
return 0;
}
}
2.Write a java program that will contain two arrays. In the
first array store the following computer peripherals name:
 Monitor
 CPU
 Mouse
 Keyboard
 Modem
 Printer
And store the following ID in the second array
 60
 30
 90
 80
 40
 50
Write a method that will display the products with their
corresponding ID

class B
{
public static void main( String args[])
{
String arr[]=new String[6];
arr[0]="Monitor";
arr[1]="CPU";
arr[2]="Mouse";
arr[3]="Keyboard";
arr[4]="Modem";
arr[5]="Printer";
int a[]=new int[6];
a[0]=60;
a[1]=30;
a[2 ] =90;
a[3] =80;
a[4]=40;
a[5]=50;

for(int i=0;i<6;i++)
{
System.out.println(arr[i]+"*"+a[i]);
}
}
}
3.Take a int[][] of length 4 * 4 and display its contents in a 4*4
matrix format.

class C
{
public static void main(String args[])
{
int a[][]=new int[4][4];
int k=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=Integer.parseInt(args[k]);
k++;
}
}

for(int i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.printf(" %d ",a[i][j]);
}
System.out.println();
}
}
}
4.Write a program to add two 2D matrix using 2D arrays and
store the added matrix in a third 2D array an display the
content.

class D
{
public static void main(String args[])
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];int k=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=Integer.parseInt(args[k]);
k++;
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=Integer.parseInt(args[k]);
k++;
}
}
System.out.println("sum of matrix:");
for(int i=0;i<3;i++)
{
System.out.println();
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.printf(" %d ",c[i][j]);
}
}
}
}

JAVA PRACTICAL
ASSIGNMENT
SESSION-7
1.Create a package and place the Calculator class of Session 4
question 3 into that package.
a) Use the instance of that class within another class
from a different package.
b) Write a class that inherits the above Calculator class
to override the method divide() so that the devisor
cannot be zero.
i) Place the class in the same package.
ii) Place it in a different package
Test the above class with different access
specifiers.

package Pck;
class Calculator
{
int opp1;
int opp2;
void add(opp1,opp2)
{
System.out.println("The result is:"+(opp1+opp2));
}
void subtract(opp1,opp2)
{
System.out.println("The result is:"+(opp1-opp2));
}
void multiply(opp1,opp2)
{
System.out.println("The result is:"+(opp1*opp2));
}
void divide(opp1,opp2)
{
System.out.println("The result is:"+(opp1/opp2));
}
}
class Calc extends Calculator
{
public static void main(String args[])
{
int m;
int n;
System.out.println("Enter the first number"+args[0]);
m=Integer.parseInt(args[0]);
System.out.println("Enter the second number"+args[1]);
n=Integer.parseInt(args[1]);
Calculator c=new Calculator();
c.add(m,n);
c.subtract(m,n);
c.multiply(m,n);
c.divide(m,n);
}
}

Das könnte Ihnen auch gefallen