Showing posts with label Function Overloading. Show all posts
Showing posts with label Function Overloading. Show all posts

Saturday, December 17, 2022

ICSE Function Overloading Program On Different Values

 A design a class to overload a function num_calc() as follows:


a) void num_calc(int num, char ch) with one integer argument and one character argument computes 

the square of integer argument if choice ch is ‘s’ otherwise finds its cube.   


b) void num_calc(int a, int b, char ch) with two integer arguments and one character argument,

computes the product of integer arguments if ch is ‘p’ else adds the integers.


c) void num_calc(String s1, String s2) with two string arguments, which prints whether the 

strings are equal or not.


Program


                
class Diff
{
    public void num_calc(int num, char ch)
  {
        if(ch == 's' || ch == 'S')
      {
            int s = num * num;
            System.out.println("Square = " + s);
        }
        else
    {
            int c = num * num * num;
            System.out.println("Cube = " + c);
        }
    }
    public void num_calc(int a, int b, char ch)
{
        if(ch == 'p' || ch == 'P')
{
            int p = a * b;
            System.out.println("Product = " + p);
        }
        else
{
            int s = a + b;
            System.out.println("Sum = " + s);
        }
    }
    public void num_calc(String s1, String s2)
{
        if(s1.equalsIgnoreCase(s2))
            System.out.println("They are equal");
        else
            System.out.println("They are unequal");
    }
  public static void main(String args[])
    {
Diff ob=new Diff();
     ob.num_calc(1,'s');
      ob.num_calc(1,2,'p');
     ob.num_calc("java","j2se");
    }
}

Friday, December 16, 2022

Function Overloading ICSE Program Comparing Values

 Design a class to overload a function compare() as follows:


(a) void compare(int, int): to compare two integer values and print the greater of the two integers.


(b) void compare(char, char): to compare the numeric values of two characters and print the character 

with higher numeric value.


(c) void compare(String, String): to compare the length of the two strings and print the longer of 

the two.


Program

                

class Comp
{
    public void compare(int a, int b)
{
        int large = (a > b)? a : b;
        System.out.println(large);
    }
    public void compare(char a, char b)
{
        char large = (a > b)? a : b;
        System.out.println(large);
    }
    public void compare(String a, String b)
{
        String large = (a.length() > b.length())? a : b;
        System.out.println(large);
    }
 
  public static void main(String args[])
  {
Comp ob=new Comp();
    ob.compare(9,18);
    ob.compare('o','x');
    ob.compare("Neymar","Messi");
  }
}

Thursday, December 15, 2022

Function Overloading Polygon Function For ICSE

 Design a class to overload a function polygon() as follows:


(i) void polygon(int n, char ch): with one integer argument and one character 

argument that draws a filled square of side n using the character stored in ch.


Input value of n = 2, ch = ‘O’

Output:

OO

OO


(ii) void polygon(int x, int y): with two integer arguments that draws a filled 

rectangle of length x and breadth y, using the symbol ‘@’.

Input value of x = 2, y = 5

Output:

@@@@@

@@@@@



(iii) void polygon(): with no arguments that draws a filled triangle shown below.

Example:


Output:

*

**

***


Program

                

class Poly
{
  
public static void polygon(int n, char ch)
{
        for(int i = 1; i <= n; i++)
       {
            for(int j = 1; j <= n; j++)
           {
                System.out.print(ch);
           }
            System.out.println();
       }
  }
    public static void polygon(int x, int y)
{
        for(int i = 1; i <= y; i++)
       {
            for(int j = 1; j <= x; j++)
          {
                System.out.print("@");
            }
            System.out.println();
       }
    }
    public static void polygon()
{
       for(int i=0;i<3;i++)
{
 for(int j= 0;j<=i;j++)
{
System.out.print("*");
}
        System.out.println();
}
} 
    public static void main(String args[])
    {
        polygon(5,'o');
        polygon(4,7);
        polygon();
    }
}

Wednesday, December 14, 2022

Function Overloading Series Function For ICSE

 Design a class to overload a function series() as follows:


(i) double series(double n) with one double argument and returns 

the sum of the series, sum = 1 / 1 + 1 / 2 + 1 / 3 + … + 1 / n.


(ii) double series(double a, double n) with two double arguments and returns

the sum of the series, sum = 1 / a^2  +  4 / a^5  +  7 / a^8  +  10 / a^11 + … to n terms.


Program


class Ser
{
    public double series(double n)
{
        double sum = 0.0;
        for(int i = 1; i <= n; i++)
            sum = sum + 1.0 / i;
        return sum;
    }
    public double series(double a, double n)
{
        double sum = 0.0;
        int j = 1;        
        for(int i = 1; i <= n; i++)
{
            sum =sum + j/ Math.pow(a, j+1);
           j=j+3;
        }
        return sum;
    }
  public static void main(String args[])
  {
    Ser ob = new Ser();
    System.out.println(ob.series(5.0));
    System.out.println(ob.series(1.3,6.0)); 
  }
}


Monday, December 12, 2022

Overloading Function Area In ICSE BlueJ Program

 Design a class to overload a function area() as follows:


(i) double area(double a, double b, double c) with three double arguments, 

returns the area of a scalene triangle using the formula:area = √(s(s – a)(s – b)(s – c))

where s = (a + b + c) / 2.


(ii) double area(int a, int b, int height) with three integer arguments,

 returns the area of a trapezium using the formula:area = 1/2 × height × (a + b)


(iii) double area(double diagonal1, double diagonal2) with two double arguments, 

returns the area of a rhombus using the formula:area = 1/2 × (diagonal1 × diagonal2)


Program


                

class Area
{
    public double area(double a, double b, double c)
{
        double s = (a + b + c) / 2.0;
    System.out.println(s);
        double areaTr = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        return areaTr;
    }
    public double area(int a, int b, int height)
{
        double areaT = 1.0 / 2 * height * (a + b);
        return areaT;
    }
    public double area(double diagonal1, double diagonal2)
{
        double areaR = 1.0 / 2 * (diagonal1 * diagonal2);
        return areaR;
    }
 
public static void main(String args[])
  {
    Area ob = new Area();
    System.out.println(ob.area(3.0,9.0,7.5));
    System.out.println(ob.area(8,9,9));
    System.out.println(ob.area(3.0,3.2)); 
  }
}

Saturday, December 10, 2022

Function Overloading ICSE BlueJ Programs On String

 Design a class to overload a function joyString() as follows:


(i) void joyString(String s, char ch1, char ch2)- with one string argument and two character arguments

that replaces the character argument ch1 with the character argument ch2 in the given string s and prints 

the new string.

Example: INPUT:s = “TECHNALAGY”

ch1 = ‘A’

ch2 = ‘O’

OUTPUT:“TECHNOLOGY”


(ii) void joyString(String s)- with one string argument that prints the position of the first space 

and the last space of the given string s.

Example:

INPUT:

s = “Cloud computing means Internet-based computing”

OUTPUT:

First index: 5

Last index: 36


(iii) void joyString(String s1, String s2)- with two string arguments that combines the two strings 

with a space between them and prints the resultant string.

Example:

INPUT:

s1 = “COMMON WEALTH”

s2 = “GAMES”

OUTPUT:

COMMON WEALTH GAMES

(use library functions)


Program


class Overloading
{
  
    void joyString(String s, char ch1, char ch2)
{
        s = s.replace(ch1, ch2);
        System.out.println(s);
    }
  void joyString(String s)
{
        int first = s.indexOf(' ');
        int last = s.lastIndexOf(' ');
        System.out.println("First index of space is: " + first);
        System.out.println("Last index of space is: " + last);
    }
    void joyString(String s1, String s2)
{
    String s= s1.concat(" ");
    s= s.concat(s2);    
        System.out.println(s);
    }
 
  public static void main(String args[])
  {
    Overloading ob = new Overloading();
    ob.joyString("computer",'o','i');
    ob.joyString("topicwise computer icse questions");
    ob.joyString("computer","programming");
  }
}


Thursday, December 8, 2022

Function Overloading ICSE Program On Series

 Design a class to overload a function SumSeries() as follows: 


(i) void SumSeries(int n, double x) – with one integer argument and one double argument

 to find and display the sum of the series given below:

s = (x/1) – (x/2) + (x/3) – (x/4) + (x/5) … to n terms


(ii) void SumSeries() – To find and display the sum of the following series:

s = 1 + (1 X 2) + (1 X 2 X 3) + … + (1 X 2 X 3 X 4 X … 20)


Program

class Series
{
  public void SumSeries(int n, double x)
 {
    double sum = 0;
    for (int i = 1; i <= n; i++)
 {
      if (i % 2 == 0) 
        sum = sum - (x / i);
       else 
        sum = sum + (x / i);
      
    }
    System.out.println("Sum = " + sum);
  }
  public void SumSeries() {
    int sum = 0;
    for (int i = 1; i <= 20; i++) 
{
      int p = 1;
      for (int j = 1; j <= i; j++) 
{
        p = p * j;
      }
      sum = sum + p;
    }
    System.out.println("Sum = " + sum);
  }
 
    public static void main(String args[])
  {
   Series ob = new Series();
    ob.SumSeries(6,2);
    ob.SumSeries();
  }
}

BlueJ Programs Function Overloading On String

 Design a class to overload a function check() as follows:


i) void check(String str, char ch) – to find and print the frequency of a character in a string.

Example :Input: Str = “Computer” ch = ‘e’

Output: number of e present is=1


ii) void check (String s1) – to display only the vowels from string s1 , after converting it to lower case.

Example :Input:S1= “Vidyanagar” 

Output: i a a a


Program


class Str
{
  public void check(String str, char ch) 
  {
    int count = 0;
    for (int i = 0; i < str.length(); i++) 
    {
      char Char = str.charAt(i);
      if (ch == Char) 
      {
        count++;
      }
    }
    System.out.println("number of "+ch+ " present is = " + count);
  }
  public void check(String s1) 
  {
    s1 = s1.toLowerCase();
    for (int i = 0; i < s1.length(); i++) 
    {
      char Char = s1.charAt(i);
      if (Char == 'a' || Char == 'e' || Char == 'i' ||Char == 'o'
          || Char == 'u') {
        System.out.print(Char + " " );
      }
    }
  }
  
    public static void main(String args[])
  {
    Str ob = new Str();
    ob.check("Computer",'e');
    ob.check("Vidyanagar");
  }
}

Tuesday, December 6, 2022

BlueJ Program On Function Overloading To Show Area Of Different shapes

 Design a class to overload a function volume() as follows:

(i)double volume(double r) – with radius ‘r’ as an argument, 

returns the volume of sphere using the formula: v = 4 / 3 × 22 / 7 × r3


(ii)double volume(double h, double r) – with height ‘h’ and radius ‘r’ as the arguments,

returns the volume of a cylinder using the formula: v = 22 / 7 × r2 × h

 

(iii) double volume(double l, double b, double h) –

with length ‘l’, breadth ‘b’ and height ‘h’ as the arguments, 

returns the volume of a cuboid using the formula: v = l × b × h


Program

class Area
{
    double volume(double r) 
    {
        double vol = 4.0 / 3 * 22 / 7 * Math.pow(r, 3);
        return vol;
    }
    double volume(double h, double r) 
    {
        double vol = 22/ (double)7 * Math.pow(r, 2) * h;
        return vol;
    }
    
    double volume(double l, double b, double h) 
    {
        double vol = l * b * h;
        return vol;
    }
  
    public static void main(String args[])
  {
     Area ob = new Area();
    System.out.println(ob.volume(3.0));
    System.out.println(ob.volume(5.0,8.0));
    System.out.println(ob.volume(3.0,3.2,2.0)); 
  }
}

Function Overloading Programs on Series

 Design a class to overload a function series() as follows:


(a) void series(int x, int n): to display the sum of the series given below:

x1 + x2 + x3 + … + xn terms


(b) void series(int p): to display the following series:

0, 7, 26, 63, … p terms.


(c) void series(): to display the sum of the series given below:

1/2 + 1/3 + 1/4 + … + 1/10.


Program on Function  Overloading To Display Series

public class Series
{
 
    public void series(int x, int n)
    {
        double sum = 0L;
        for(int i = 1; i <= n; i++)
        {
            sum=sum+Math.pow(x, i);
        }
        System.out.println("Sum of series is (1st Overloaded function)" + sum);
    }
    public void series(int p)
    {
        int term=0;
        System.out.print("\n2nd Overloaded function ");
        for(int i = 1; i <= p; i++)
        {
            term = (int)(Math.pow(i, 3)-1);
            System.out.print(term + " ");
        }
    }
    public void series()
    {
        double sum = 0.0;
        for(int i = 2; i <= 10; i++)
            sum = sum+ 1.0 / i;
        System.out.println("\n\nSum of series is (3rd Overloaded function)" + sum);
    }
    
    public static void main(String args[])
    {
      Series ob = new Series();
      ob.series(2,4);
      ob.series(6);
      ob.series();  
    } 
  
}

Back To BlueJ Series Display Programs Home Page: CLICK HERE

Friday, October 30, 2020

BlueJ Program On Function Overloading and String

  Design a class to overload a function check ( ) as follows :
(i) void check (String str, char ch) – to find and print the frequency of a character in a string.
Example : *
Input:
str = “success”
ch = ‘s’ .
Output:
number of s present is = 3

(ii) void check(String si) – to display only vowels from string si, after converting it to lower case.
Example:
Input:
s1 = “computer”
Output:
o u e
       
       import java.util.*;
       class Ab
       {
            String str1;
            int i,len,choice;
            char ch;
            Scanner sc=new Scanner(System.in);
            public void take()
            {
                 
                   System.out.print("\nEnter your choice (1 for checking a character in a sentence, 2 for displaying vowels:");// "THIS IS A TEST OF ALL A'S "
                   choice=sc.nextInt();
                   sc=new Scanner(System.in);
                   if(choice==1)
                   {
                       System.out.print("\nEnter sentence:");
                       str1=sc.nextLine().toLowerCase();
                       System.out.print("\nEnter character:");
                       ch=sc.next().charAt(0);
                       check(str1,ch);
                    }
                    else if(choice==2)
                    {
                        System.out.print("\nEnter sentence:");
                       str1=sc.nextLine().toLowerCase();
                       
                       check(str1);
                    }
                    else
                    System.out.print("\nWrong Choice...");
                }
                    
                  private void check(String s, char ch)
                  {
                      int i,len,c=0;
                      len=s.length();
                      for(i=0;i<len;i++)
                      {
                      if(s.charAt(i)==ch)
                   c++;
                }
                 System.out.print("\nCharacter '"+ ch+ "' is present for "+ c+ " times is sentence:"+s);
                   
               }
               private void check(String s)
                  {
                      int i,len;
                      char ch;
                      len=s.length();
                System.out.print("\nThe Vowels are as follows: ");
                      for(i=0;i<len;i++)
                      {
                      ch=s.charAt(i);
                      switch(ch)
                      {
                           case 'a':
                           case 'e':
                           case 'i':
                           case 'o':
                           case 'u':
                            System.out.print(" "+ch);
                        }
                  
                }
                
                   
               }
            }

             

Monday, August 19, 2019

Menu Based Function Overloading Program To Display Area



BlueJ program on function overloading. Function overloading is one type of polymorphism.


Define a class TraingleArea that contains the overloaded area methods – one accepts the length and breadth of a triangle as parameter. The other area method accepts the three sides of a triangle.

The two formulas of computing the area are:
Area1 = ½*base*height
Area2 = Square Root of s(s-a)*s(s-b)*s(s-c)
Where s=(a+b+c)/2 and a,b,c are three sides


import java.util.*;
class TraingleArea
{
Scanner sc=new Scanner(System.in);
public void takeDatas()
{
    int choice, a,b,c,base,height;
    double area1;


System.out.print("\nEnter Choice (1 for Area1 and 2 for Area2):");
choice=sc.nextInt();
switch(choice)
{
     case 1:
            System.out.print("\nEnter Base:");
            base=sc.nextInt();
            System.out.print("\nEnter Height:");
            height=sc.nextInt();
            area1=area(base,height);
            System.out.print("\nArea of triangle as per first formula = "+area1);
            break;
           
     case 2:
           System.out.print("\nEnter 1st Side:");
           a=sc.nextInt();
           System.out.print("\nEnter 2nd Side:");
            b=sc.nextInt();
            System.out.print("\nEnter 3rd Side:");
            c=sc.nextInt();
            area1=area(a,b,c);
            System.out.print("\nArea of triangle as per second formula = "+area1);
            break;
     default:
              System.out.print("\nWrong Choice.");
 }
}
 private double area(int a,int b)
 {
      double r;
      r=1/2.0*a*b;
      return r;
  }
 
  private double area(int a,int b,int c)
 {
      double r;
      double s;
      s=(a+b+c)/2.0;
      s=s*(s-a)*s*(s-b)*s*(s-c);
      r=Math.sqrt(s);
      return r;
  }   
   
public static void main(String args[])throws Exception
{
TraingleArea ob=new TraingleArea ();
ob.takeDatas();
}
}


Related Post: BlueJ Menu Based Programs

Subscribe via email

Enter your email address:

Delivered by FeedBurner