WAP To Find Out Factorial of A Number Through Recursion.: Program-1
WAP To Find Out Factorial of A Number Through Recursion.: Program-1
import java.util.Scanner;
class FactorialDemo
{ int output;
if(n==0)
{ return 1; }
else
return(n * fact(n-1));
}
Program-2
WAP to print Fibonacci series.
import java.lang.*;
import java.util.*;
class fibonacci{
public static void main(String[] args) {
int a=0, b=1, c;
int limit;
System.out.println("Enter the limit: ");
Scanner sc = new Scanner(System.in);
limit = sc.nextInt();
while(a <= limit){
System.out.println(a);
c = a + b;
a = b;
b = c;
}
}
Program-3
WAP to accept Command line arguments & print them.
class Demo
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
}
}
Program-4
import java.util.Scanner;
num = scan.nextInt();
{ if (num%i == 0)
{ count++;
Break;} }
If (count == 0)
else
Program-5
WAP that creates a class Accounts with following details:Instance variables:
ac_no., name, ac_name, balance .Methods: withdrawal(),deposit(),display().Use
constructors to initialize members.
import java.util.*;
class main{
public static void main(String[] args) {
Accounts ac1 = new Accounts(1, "Balwant", "SBI", 3000);
ac1.display();
ac1.withdrawal(2000);
ac1.display();
ac1.deposit(5000);
ac1.display();
}
}
class Accounts{
int ac_no;
String name, ac_name;
float balance;
public float withdrawal(float amount){
balance -= amount;
return balance;
}
public float deposit(float amount){
balance += amount;
return balance;
}
public void display(){
System.out.println("Balance: " + balance);
}
Accounts(int ac_no_in, String name_in, String ac_name_in, float balance_in){
ac_no = ac_no_in;
name = name_in;
ac_name = ac_name_in;
balance = balance_in;
}
}
Program-6
WAP to implement constructor overloading.
import java.lang.*;
class main
co1.volume(); }}
class conOverload
{ float l, b, h;
{ l = length;
b = breadth;
h = height;}
l = length;
b = breadth; h = height; }
}
Program-7
WAP to count the no.of objects created in a program.
import java.lang.*;
class Test
public Test()
{ noOfObjects += 1; }
System.out.println(Test.noOfObjects);
}
Program-8
WAP to show call by value & call by reference.
//call by value
import java.lang.*;
class Operation
int data=50;
void change()
data=data+100;
op.change();
}
// call by reference.
import java.lang.*;
class CallbyReference{
int x;
a.x=3;
increment(a);
n.x=n.x+1;
Program-9
WAP to implement method over ridding & method overloading.
import java.lang.*;
class main
{ public static int add(int a, int b)
{ return a + b; }
public static int add(int a, int b, int c){
return a + b + c; }
public static void main(String[] args)
{ System.out.println("Sum: " + add(3, 4));
System.out.println("Sum: " + add(3, 4, 5));
derived d = new derived();
d.show(); }}
class base
{ void show()
{ System.out.println("Base Class"); }
}
class derived extends base
{ void show()
{ System.out.println("Derived class");}}
Program-10
WAP that demonstrates all the usages of “super” keyword.
import java.lang.*;
class Box
{double length,breadth,height;
Box()
{length=-1;
breadth=-1;
height=-1;}
{length=l;
breadth=b;
height=h;}
double volume()
{return length*breadth*height;}
{double weight;
{super (w,p,q);
weight=r;}
Box_new()
{weight=-1;}
class Demo
double vol;
vol=b1.volume();
vol=b2.volume();
vol=b3.volume();
import java.lang.*;
class A
{A()
class B extends A
{B()
class C extends B
{C()
class Demo
C c=new C();}}
Program-13
WAP to implement Run time polymorphism.
import java.lang.*;
class Calculator
{int i,j;
{int m=i*j;
{int diff=i-j;
{int sum=i+j;
{int i,j;
{int diff=j-i;
class Demo
c.mul(2,3);
c.sub(5,4);
c.add(9,1);
c=a;
c.sub(5,4);}
Program-14
WAP to implement interface. Create an interface named Shape having area() &
perimeter() as its methods. Create three classes circle, rectangle & square that
implement this interface.
Program-15
WAP to show multiple inheritance.
import java.lang.*;
interface I1
{ System.out.println("Default Interface1"); } }
interface I2
{ System.out.println("Default Interface2"); }}
{ I1.super.show();
I2.super.show(); }
d.show(); } }