Program-1
WAP to find out factorial of a number through recursion.
import [Link].*;
import [Link];
class FactorialDemo
{ public static void main(String args[])
{ Scanner scanner = new Scanner([Link]);
[Link]("Enter the number:");
int num = [Link]();
int factorial = fact(num);
[Link]("Factorial of entered number is: "+factorial);
static int fact(int n)
{ int output;
if(n==0)
{ return 1; }
else
return(n * fact(n-1));
}
Program-2
WAP to print Fibonacci series.
import [Link].*;
import [Link].*;
class fibonacci{
public static void main(String[] args) {
int a=0, b=1, c;
int limit;
[Link]("Enter the limit: ");
Scanner sc = new Scanner([Link]);
limit = [Link]();
while(a <= limit){
[Link](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<[Link];i++)
{
[Link](args[i]);
}
}
}
Program-4
WAP to obtain a number by a user & check if it’s prime or not.
import [Link].*;
import [Link];
public class Num
{ public static void main(String args[])
{ int num, i, count=0;
Scanner scan = new Scanner([Link]);
[Link]("Enter a Number : ");
num = [Link]();
for(i=2; i<num; i++)
{ if (num%i == 0)
{ count++;
Break;} }
If (count == 0)
{ [Link]("This is a Prime Number"); }
else
{ [Link]("This is not a Prime Number"); }
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 [Link].*;
class main{
public static void main(String[] args) {
Accounts ac1 = new Accounts(1, "Balwant", "SBI", 3000);
[Link]();
[Link](2000);
[Link]();
[Link](5000);
[Link]();
}
}
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(){
[Link]("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 [Link].*;
class main
{ public static void main(String[] args)
{ conOverload co1 = new constructorOverloading(3, 4, 5);
[Link](); }}
class conOverload
{ float l, b, h;
constructorOverloading(int length, int breadth, int height)
{ l = length;
b = breadth;
h = height;}
conOverload(float length, float breadth, float height){
l = length;
b = breadth; h = height; }
public void volume()
{[Link]("Volume: " + l * b * h);}
}
Program-7
WAP to count the [Link] objects created in a program.
import [Link].*;
class Test
{ static int noOfObjects = 0;
public Test()
{ noOfObjects += 1; }
public static void main(String args[])
{ Test t1 = new Test();
Test t2 = new Test();
Test t3 = new Test();
[Link]([Link]);
}
Program-8
WAP to show call by value & call by reference.
//call by value
import [Link].*;
class Operation
int data=50;
void change()
data=data+100;
public static void main(String args[])
{ Operation op=new Operation();
[Link]("before change "+[Link]);
[Link]();
[Link]("after change "+[Link]); }
}
// call by reference.
import [Link].*;
class CallbyReference{
int x;
public static void main ( String[] args ) {
Number a = new Number();
a.x=3;
[Link]("Value of a.x before calling increment() is "+a.x);
increment(a);
[Link]("Value of a.x after calling increment() is "+a.x);
public static void increment(Number n) {
[Link]("Value of n.x before incrementing x is "+n.x);
n.x=n.x+1;
[Link]("Value of n.x after incrementing x is "+n.x);
Program-9
WAP to implement method over ridding & method overloading.
import [Link].*;
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)
{ [Link]("Sum: " + add(3, 4));
[Link]("Sum: " + add(3, 4, 5));
derived d = new derived();
[Link](); }}
class base
{ void show()
{ [Link]("Base Class"); }
}
class derived extends base
{ void show()
{ [Link]("Derived class");}}
Program-10
WAP that demonstrates all the usages of “super” keyword.
//To access the variable of parent class
import [Link].*;
class base{
int num = 100;
}
class derived extends base{
int num = 110;
void print(){
[Link]([Link]);
}
public static void main(String[] args) {
derived d = new derived();
[Link]();
}
}
//To invoke constructor of parent class
import [Link].*;
class base{
base(int num){
[Link]("Base class Constructor");
[Link](num);
}
}
class derived extends base{
derived(int num_in){
super(num_in);
[Link]("Derived class Constructor");
}
public static void main(String[] args) {
derived d = new derived(4);
}
}
Program-11
Create a class box having height, width , depth as the instance variables &
calculate its volume. Implement constructor overloading in it. Create a subclass
named box_new that has weight as an instance variable. Use super in the
box_new class to initialize members of the base class.
import [Link].*;
class Box
{double length,breadth,height;
Box()
{length=-1;
breadth=-1;
height=-1;}
Box(double l,double b,double h)
{length=l;
breadth=b;
height=h;}
double volume()
{return length*breadth*height;}
class Box_new extends Box
{double weight;
Box_new(double w,double p,double q,double r)
{super (w,p,q);
weight=r;}
Box_new()
{weight=-1;}
class Demo
{ public static void main(String args[]){
Box_new b1=new Box_new(10,20,30,5);
Box_new b2=new Box_new(1,7,9,5);
Box_new b3=new Box_new();
double vol;
vol=[Link]();
[Link]("Volume of box1 is"+vol);
[Link]("Weight of box1 is"+[Link]);
vol=[Link]();
[Link]("Volume of box2 is"+vol);
[Link]("Weight of box2 is"+[Link]);
vol=[Link]();
[Link]("Volume of box3 is"+vol);
[Link]("Weight of box3 is"+[Link]);}
}
Program-12
WAP that implements multilevel inheritance.
import [Link].*;
class A
{A()
{[Link]("A's Constructor called");}}
class B extends A
{B()
{[Link]("B's Constructor called");}}
class C extends B
{C()
{[Link]("C's Constructor called");}}
class Demo
{ public static void main(String args[]){
C c=new C();}}
Program-13
WAP to implement Run time polymorphism.
import [Link].*;
class Calculator
{int i,j;
void mul(int i,int j)
{int m=i*j;
[Link]("Product is"+" "+m);}
void sub(int i,int j)
{int diff=i-j;
[Link]("Difference is"+" "+diff);}
void add(int i,int j)
{int sum=i+j;
[Link]("Sum is"+" "+sum);}}
class Calc extends Calculator
{int i,j;
void sub(int i,int j)
{int diff=j-i;
[Link]("Difference is"+" "+diff);}}
class Demo
{ public static void main(String args[]){
Calculator c=new Calculator();
[Link](2,3);
[Link](5,4);
[Link](9,1);
Calc a=new Calc();
c=a;
[Link](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 [Link].*;
interface I1
{ default void show()
{ [Link]("Default Interface1"); } }
interface I2
{default void show()
{ [Link]("Default Interface2"); }}
class Demo implements I1, I2
{ public void show()
{ [Link]();
[Link](); }
public static void main(String args[])
{ Demo d = new Demo();
[Link](); } }