Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Function-based Programs_Programs

The document outlines multiple Java programs demonstrating function overloading and polymorphism. It includes examples for calculating averages, performing mathematical operations based on user input, and checking for prime palindrome numbers. Each program is accompanied by code snippets and explanations of their functionalities.

Uploaded by

Parshveer Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Function-based Programs_Programs

The document outlines multiple Java programs demonstrating function overloading and polymorphism. It includes examples for calculating averages, performing mathematical operations based on user input, and checking for prime palindrome numbers. Each program is accompanied by code snippets and explanations of their functionalities.

Uploaded by

Parshveer Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SOLUTION TO Function based

PROGRAMS with PTOs


1. Write a program to illustrate polymorphism through the function calcAvg( ).
a. float calcAvg (int a, int b) - calculates and returns the average of two integers.

b. int calcAvg (int a) – returns the number itself.

c. float calcAvg(byte size) –calculates and returns the average of numbers


inputted through the keyboard. Here size signifies the number of elements in
the array no, in which the inputted numbers are stored.

Write the main method and call all the three versions of the above function.

What will happen if, in the third version of the function (byte size) is replaced by (int
size)?

ANS:

package FUNCTIONS;

import java.util.*;

public class CALC_AVG

//TO FIND AVERAGE OF TWO INTEGERS.

float calcAvg(int a,int b)

float s=a+b;

float avg=s/2.0f;

return(avg);

//TO RETURN THE NUMBER ITSELF.


I-TECH 1
int calcAvg(int a)

return(a);

//TO ACCEPT SIZE AND RETURN AVERAGE OF ARRAY

float calcAvg(byte size)

int avg=0; float=0;

Scanner ob=new Scanner(System.in);

float a[]=new float[size];

System.out.println("Enter an array ");

for(int i=0;i<size;i++)

a[i]=ob.nextInt();

s=s+a[i];

avg=s/size;

return(avg);

// TO DISPLAY:

void main()

I-TECH 2
float a= calcAvg(5,10);

System.out.println(a);

System.out.println(calcAvg(5));

System.out.println(calcAvg((byte)6));

2. Design a class to overload a function num_calc ( ) as follows.

void num_calc (int num, char ch) which computes the square of integer argument if
choice ch is ‘s’ otherwise finds its cube.

void num_calc (int a, int b, char ch) which computes the product of the integer arguments,
if choice ch is ‘p’ else adds the integers.

void num_calc (String s1, String s2) which prints whether the strings are equal or
not.

ANS:

package FUNCTIONS;

import java.util.*;

public class NUM_CALC

//TO FIND SQUARE OR CUBE DEPENDING ON CHOICE

void num_calc(int num,char ch)

ch=Character.toLowerCase(ch);

I-TECH 3
switch(ch)

case 's':

System.out.println(Math.pow(num,2));

break;

default :

System.out.println(Math.pow(num,3));

//WHICH FINDS PRODUCT OR FINDS SUM BASED ON CHOICE

void num_calc(int a,int b,char ch)

switch(ch)

case 'p':

System.out.println(a*b);

break;

default :

System.out.println(a+b);

//TELLS IF STRINGS ARE EQUAL OR NOT

void num_calc(String s1,String s2)

I-TECH 4
{

System.out.println(s1.equalsIgnoreCase(s2));

//MAIN TO CALL

void main()

num_calc(5,'s');

num_calc("FATEHPUR","SIKHRI");

num_calc(5,4,'c');

3. Write a program to overload a function maximum ( ) as follows:

maximum ( ) with one integer argument should display the next integer.

maximum ( ) with two integer arguments and should display the larger value among
the two.

maximum ( ) with three integer arguments and should display the smallest value among
the three.

Display the results with appropriate messages.

ANS:

package FUNCTIONS;

import java.util.*;

public class MAXIMUM

I-TECH 5
//TO DISPLAY THE NEXT INTEGER AFTER
THE ARGUMENT

void maximum(int a)

System.out.println("The Next Integer


is"+ ++a);

//TO DISPLAY THE LARGER VALUE


AMONG THE TWO

void maximum(int a,int b)

System.out.println("The largest is:


"+Math.max(a,b));

//TO DISPLAY THE SMALLEST VALUE


AMONG THREE ARGUMENTS

void maximum(int a,int b,int c)

System.out.println("The Smallest among


them is="+Math.min(Math.min(a,b),c));

//TO CALL

void main()

maximum(5);

maximum(69,96,49);

I-TECH 6
maximum(18,21);

4. Prime palindrome is a number which is prime as well as palindrome. E.g. 101. Write
a program to display prime palindrome numbers within the given range. The limits of
the range should be entered by the user. Define two user defined functions to check
for the number. The function prototypes are given below.

boolean isPrime( int n ) boolean isPalin( int n )

ANS:

package FUNCTIONS;

import java.util.*;

public class PRIMEPALINDROME

//TO CHECK IF IT IS A PRIME NUMBER:

boolean isPrime(int n)

int i,k=0;

for(i=1;i<=n;i++)

I-TECH 7
if(n%i==0)

k++;

if(k==2)

return(true);

return(false);

//TO CHECK IF IT IS A PALINDROME

boolean isPalin(int n)

int t=n,r,p=0;

while(t>0)

r=t%10;

p=p*10+r;

t=t/10;

ifn==p)

I-TECH 8
return(true);

return(false);

void main()

Scanner ob=new Scanner(System.in);

int start,stop;boolean r1=false,r2=false;

System.out.println("Enter start and stop");

start=ob.nextInt();

stop=ob.nextInt();

for(i=start;i<=stop;i++)

boolean r1=isPalin(i);

boolean r2=isPrime(i);

if(r1==true&&r2==true)

System.out.println(i+" , ");

I-TECH 9
1
5. Design a class Overload to overload a function calc( ) as
follows:

void calc (int num1, int num2, int num3) which computes the sum of 3 integer
arguments.

void calc (int a, int b) which calculates and prints the HCF of two integers a and
b.

void calc (char c1, char c2) which prints sum of ASCII code of both the
characters.

Write main( ) to call the overloaded functions appropriately.

ANS:

package FUNCTIONS;

import java.util.*;

public class CALC

//TO CALCULATE SUM:

void calc(int a,int b,int c)

System.out.println(a+b+c);

//TO PRINT HCF [HIGHEST COMMON FACTOR]

void calc(int a,int b)

int i,hcf=0;

for(i=1;i<=Math.min(a,b);i++)
I-TECH 10
{

if(a%i==0&&b%i==0)

hcf=i;

System.out.println("HCF IS= "+max);

//TO FIND SUM OOF ASCII CODES

void calc(char c1,char c2)

System.out.println("The sum of ASCII codes is = "+((int)c1+(int)c2));

// MAIN TO CALL:

void main()

calc(8,5,9);

calc(4,5);

calc('F','S');

I-TECH 11
I-TECH 12

You might also like