Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
70 views17 pages

Java Programs: Factorial, Reverse, Method Overloading, Constructors

The document contains 10 programming problems and their solutions. Each problem statement is followed by the objective and program code. The problems cover topics like finding the factorial of a number, reversing a number, method overloading, multiple main methods, implicit data type conversion, passing arguments through objects, anonymous objects, default values, constructors, and copy constructors.

Uploaded by

Ashirvad
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)
70 views17 pages

Java Programs: Factorial, Reverse, Method Overloading, Constructors

The document contains 10 programming problems and their solutions. Each problem statement is followed by the objective and program code. The problems cover topics like finding the factorial of a number, reversing a number, method overloading, multiple main methods, implicit data type conversion, passing arguments through objects, anonymous objects, default values, constructors, and copy constructors.

Uploaded by

Ashirvad
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/ 17

Ashirvad Raj MCA III(A) ROLL NO.

21

PROBLEM STATEMENT 1: WAP TO PRINT FACTORIAL OF A


NUMBER

OBJECTIVE: INPUT VALUE THROUGH COMMAND LINEARGUMENT.

PROGRAM:

class prog1

public static void main(String [] args)

int n=Integer.parseInt(args[0]);

int p=1;

int temp=n;

while(n>1)

p=p*n;

n--;

System.out.println("factorial of no. "+temp+ " is "+ p );

}
Ashirvad Raj MCA III(A) ROLL NO. 21

PROBLEM STATEMENT 2: WAP TO REVERSE A NUMBER

OBJECTIVE: INPUT THROUGH COMMAND LINR ARGUMENT.

PROGRAM:

class prog2

public static void main(String [] args)

int n=Integer.parseInt(args[0]);

int rev=0, rem=0;

int temp=n;

while(n>0)

rem=n%10;

n=n/10;

rev=rev*10+rem;

System.out.println("Reverse of a no."+temp+"= "+rev);

}
Ashirvad Raj MCA III(A) ROLL NO. 21

PROBLEM STATEMENT 3: TO CREATE A CLASS N CALL FUNCTION


WITHOUT OBJECT CREATING.

OBJECTIVE: METHOD OVERLOADING.

PROGRAM:

class mo

static int sum(int x, int y)

return (x+y);

static double sum(double x, double y)

return(x+y);

static void sum(int x)

x++;

System.out.println(x);

class fmo

{
Ashirvad Raj MCA III(A) ROLL NO. 21

public static void main(String [] args)

System.out.println(mo.sum(5,6));

System.out.println(mo.sum(5.6,6.5));

mo.sum(7);

PROBLEM STATEMENT 4: WAP TO WORK WITH MULTIPLE MAIN


FUNCTIONS.

OBJECTIVE: BY METHOD OVERLOADING.

PROGRAM:

class prog4

public static void main(String [] args)


Ashirvad Raj MCA III(A) ROLL NO. 21

System.out.println("STANDARD MAIN METHOD INVOKE");

main(3);

public static void main(int a)

System.out.println("INT ARGUMENT MAIN METHOD INVOKE");

main(2.4);

public static void main(double a)

System.out.println("DOUBLE ARGUMENT MAIN METHOD


INVOKE");

}
Ashirvad Raj MCA III(A) ROLL NO. 21

PROBLEM STATEMENT 5: WAP TO SHOW IMPLICIT DATA TYPE


CONVERSION.

OBJECTIVE: BY METHOD OVERLOADING

PROGRAM:

class check

static void sum(int a)

System.out.println("INT ARUGMENT METHOD INVOKED");

static void sum(double a)

System.out.println("DOUBLE ARUGMENT METHOD


INVOKED");

class prog5

public static void main(String [] args)

check.sum(2);

check.sum('N');
Ashirvad Raj MCA III(A) ROLL NO. 21

check.sum(2.5f);

check.sum(5.7);

PROBLEM STATEMENT 6: WAP TO PASS ARGUMENTS THROUGH


OBJECT.

OBJECTIVE: BY CLASS AND OBJECT

PROGRAM:

class test

int rollno;

String name;

double per;

void getdata(int a, String n, double d)

rollno=a;
Ashirvad Raj MCA III(A) ROLL NO. 21

name=n;

per=d;

void show()

System.out.println("STUDENT ROLLNO: "+rollno);

System.out.println("STUDENT NAME: "+name);

System.out.println("STUDENT PERCENTAGE: "+per);

class testmain

public static void main(String[] args)

test t=new test();

t.getdata(02, Ashirvad raj", 75);

t.show();

}
Ashirvad Raj MCA III(A) ROLL NO. 21

PROBLEM STATEMENT 7: WAP TO CALL A FUNCTION WITHOUT


CREATING AN OBJECT.

OBJECTIVE: HOW A ANONYMOUS OBJECT WORKS

PROGRAM:

class test

void show()

System.out.println("ANONYMOUS OBJECT");

class ao

public static void main(String[] args)

new test().show();

}
Ashirvad Raj MCA III(A) ROLL NO. 21

PROBLEM STATEMENT 8: WAP TO DISPLAY DEFAULT VALUES OF


DIFFERENT DATA TYPES.

OBJECTIVE: BY CLASS AND OBJECT.

PROGRAM:

class test

int rollno;

String name;

double per;

void show()

System.out.println("STUDENT ROLLNO: "+rollno);

System.out.println("STUDENT NAME: "+name);


Ashirvad Raj MCA III(A) ROLL NO. 21

System.out.println("STUDENT PERCENTAGE: "+per);

class zerovalue

public static void main(String[] args)

test t=new test();

t.show();

PROBLEM STATEMENT 9: WAP TO INITIALIZE INSTANCE


VARIABLE WHILE CREATING OBJECT OF A CLASS.
Ashirvad Raj MCA III(A) ROLL NO. 21

OBJECTIVE: BY CONSTRUCTOR.

PROGRAM:

class student

int rollno;

String name;

double per;

student()

rollno=21;

name="Ashirvad raj";

per=75;

System.out.println("DEFAULT CONSTRUCTOR");

void show()

System.out.println("STUDENT ROLLNO: "+rollno);

System.out.println("STUDENT NAME: "+name);

System.out.println("STUDENT PERCENTAGE: "+per);

class constdemo

{
Ashirvad Raj MCA III(A) ROLL NO. 21

public static void main(String[] args)

student t=new student();

t.show();

PROBLEM STATEMENT 10: WAP TO PASS VALUES THROUGH


CONSTRUCTOR

OBJECTIVE: BY PARAMETERISED CONSTRUCTOR.

PROGRAM:

class student

int rollno;

String name;

double per;

student(int r, String n, double d)

{
Ashirvad Raj MCA III(A) ROLL NO. 21

rollno=r;

name=n;

per=d;

System.out.println("PARAMETERISED CONSTRUCTOR");

void show()

System.out.println("STUDENT ROLLNO: "+rollno);

System.out.println("STUDENT NAME: "+name);

System.out.println("STUDENT PERCENTAGE: "+per);

class paraconst

public static void main(String[] args)

student t=new student(21, "Ashirvad raj", 75);

t.show();

}
Ashirvad Raj MCA III(A) ROLL NO. 21

PROBLEM STATEMENT 11: WAP TO SHOW THE COPY


CONSTRUCTOR

OBJECTIVE: COPY CONSTRUCTOR.

PROGRAM:

class Product

String name;

int id;

Product(String name,int id)

this.name=name;

this.id=id;

Product(Product obj)
Ashirvad Raj MCA III(A) ROLL NO. 21

name=obj.name;

id=obj.id;

void show()

System.out.println("name of Product"+name);

System.out.println("id of Product"+id);

class DemoProduct

public static void main(String[] args)

Product P1= new Product("Fan",101);

Product P2= new Product("table",102);

Product P3= new Product(P1);

P1.show();

P2.show();

P3.show();

}
Ashirvad Raj MCA III(A) ROLL NO. 21

You might also like