0% found this document useful (0 votes)
7 views6 pages

Mid Prog QB Solution

Uploaded by

sanket1shah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Mid Prog QB Solution

Uploaded by

sanket1shah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

[15]

Write a complete program to accept N integer numbers from the command line.
Raise and handle exceptions for following cases :
-When anumber is–ve
-when a number is evenly divisible by 10
-when a number is greater than 1000 and less than 2000
-when a number is greater than 7000
Skip the number if an exception is raised for it, otherwise add it to find total sum.

Code:

class myError extends Exception


{

String err;
public myError(String x)
{
err=x;

}
public String toString()
{
return err;
}
}
class CustomErr
{
public static void main(String args[])
{
int sum=0,x=0;
try
{
for(int i=0;i<args.length;i++)
{
x=Integer.parseInt(args[i]);
if(x<0)
{
throw new myError("Negative Number");
}
else if(x>1000 && x<2000)
{
throw new myError(">1000 && <2000");
}
else if(x%10 ==0)
{
throw new myError("/10");
}
else if(x>7000)
{
}
else
{
sum=sum+Integer.parseInt(args[i]);
}
}
System.out.println("Sum = "+sum);
}
catch(Exception e)
{
System.out.println("Exception :"+e.toString());
}
}
}

Output:
[5]

Define a recursive method for computing x raised to power y by doing repetitive


multiplication where x and y are positive integer numbers. Define main to use above
method.

Code:

import java.io.*;
import java.util.*;
class power
{
int pow(int x,int y)
{
if(y==0)
{
return 1;
}
else if(y==1)
{
return x;
}
else
{
return x*pow(x,y-1);
}
}
public static void main(String args[])
{
power p=new power();
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
int ans=p.pow(x,y);
System.out.println("ans is"+ans);
}
}

Output:
[6]
Write a method for computing first n terms of Fibonacci sequence. Define method Main
taking value of n as command line argument and calling the method.

Code:

import java.io.*;
import java.util.*;
class fibo
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,limit;
limit=Integer.parseInt(args[0]);
System.out.print(n1+" "+n2);
for(i=2;i<limit;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}

Code:
[8]

Write a method for computing x raised to y doing repetitive multiplication. X and y are
of type integer and are to be given as command line arguments. Raise and handle
exception(s) for invalid values of x and y.

Code:

class pow
{
public static void main(String args[])
{
int x,y,mul=1;
try
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
for(int i=0;i<y;i++)
{
mul=mul*x;
System.out.println(x+"^"+y+" : "+mul);
}
}
catch(NumberFormatException e)
{
System.out.println("Exception :"+e);
}
}
}

Output:
[9]
Write a program to create circle class with area function to find area of circle

Code:
import java.util.Scanner;
class Circle
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the radius:");
double rad= s.nextDouble();
double a=area(rad);
System.out.println("Area of Circle is: " + a);
}
static double area(double r)
{
return (22*r*r)/7;
}
}

Output:

You might also like