0% found this document useful (0 votes)
167 views

WAP To Find Out Factorial of A Number Through Recursion.: Program-1

The document contains 15 Java programs demonstrating various OOP concepts like inheritance, polymorphism, abstraction, encapsulation etc. Program 1 shows recursion to find factorial of a number. Program 2 prints Fibonacci series using a while loop. Program 3 accepts and prints command line arguments.

Uploaded by

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

WAP To Find Out Factorial of A Number Through Recursion.: Program-1

The document contains 15 Java programs demonstrating various OOP concepts like inheritance, polymorphism, abstraction, encapsulation etc. Program 1 shows recursion to find factorial of a number. Program 2 prints Fibonacci series using a while loop. Program 3 accepts and prints command line arguments.

Uploaded by

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

Program-1

WAP to find out factorial of a number through recursion.


import java.lang.*;

import java.util.Scanner;

class FactorialDemo

{ public static void main(String args[])

{ Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number:");

int num = scanner.nextInt();

int factorial = fact(num);

System.out.println("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 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

WAP to obtain a number by a user & check if it’s prime or not.


import java.lang.*;

import java.util.Scanner;

public class Num

{ public static void main(String args[])

{ int num, i, count=0;

Scanner scan = new Scanner(System.in);

System.out.print("Enter a Number : ");

num = scan.nextInt();

for(i=2; i<num; i++)

{ if (num%i == 0)

{ count++;

Break;} }

If (count == 0)

{ System.out.print("This is a Prime Number"); }

else

{ System.out.print("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 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

{ public static void main(String[] args)

{ conOverload co1 = new constructorOverloading(3, 4, 5);

co1.volume(); }}

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()

{System.out.println("Volume: " + l * b * h);}

}
Program-7
WAP to count the no.of objects created in a program.

import java.lang.*;

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();

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;

public static void main(String args[])

{ Operation op=new Operation();

System.out.println("before change "+op.data);

op.change();

System.out.println("after change "+op.data); }

}
// call by reference.

import java.lang.*;

class CallbyReference{

int x;

public static void main ( String[] args ) {

Number a = new Number();

a.x=3;

System.out.println("Value of a.x before calling increment() is "+a.x);

increment(a);

System.out.println("Value of a.x after calling increment() is "+a.x);

public static void increment(Number n) {

System.out.println("Value of n.x before incrementing x is "+n.x);

n.x=n.x+1;

System.out.println("Value of n.x after incrementing x is "+n.x);

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.

//To access the variable of parent class


import java.lang.*;
class base{
int num = 100;
}
class derived extends base{
int num = 110;
void print(){
System.out.println(super.num);
}
public static void main(String[] args) {
derived d = new derived();
d.print();
}
}

//To invoke constructor of parent class


import java.lang.*;
class base{
base(int num){
System.out.println("Base class Constructor");
System.out.println(num);
}
}
class derived extends base{
derived(int num_in){
super(num_in);
System.out.println("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 java.lang.*;

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=b1.volume();

System.out.println("Volume of box1 is"+vol);

System.out.println("Weight of box1 is"+b1.weight);

vol=b2.volume();

System.out.println("Volume of box2 is"+vol);

System.out.println("Weight of box2 is"+b1.weight);

vol=b3.volume();

System.out.println("Volume of box3 is"+vol);

System.out.println("Weight of box3 is"+b1.weight);}


}
Program-12
WAP that implements multilevel inheritance.

import java.lang.*;

class A

{A()

{System.out.println("A's Constructor called");}}

class B extends A

{B()

{System.out.println("B's Constructor called");}}

class C extends B

{C()

{System.out.println("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 java.lang.*;
class Calculator

{int i,j;

void mul(int i,int j)

{int m=i*j;

System.out.println("Product is"+" "+m);}

void sub(int i,int j)

{int diff=i-j;

System.out.println("Difference is"+" "+diff);}

void add(int i,int j)

{int sum=i+j;

System.out.println("Sum is"+" "+sum);}}

class Calc extends Calculator

{int i,j;

void sub(int i,int j)

{int diff=j-i;

System.out.println("Difference is"+" "+diff);}}

class Demo

{ public static void main(String args[]){

Calculator c=new Calculator();

c.mul(2,3);

c.sub(5,4);

c.add(9,1);

Calc a=new Calc();

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

{ default void show()

{ System.out.println("Default Interface1"); } }

interface I2

{default void show()

{ System.out.println("Default Interface2"); }}

class Demo implements I1, I2

{ public void show()

{ I1.super.show();

I2.super.show(); }

public static void main(String args[])

{ Demo d = new Demo();

d.show(); } }

You might also like