English

Write a program with overloaded methods to implement the following: Overload a method Pack(), given that: (a) void Pack(int a, int b) − to print the average of the arguments.

Advertisements
Advertisements

Question

Write a program with overloaded methods to implement the following:

Overload a method Pack(), given that:

  1. void Pack(int a, int b) − to print the average of the arguments.
  2. void Pack(int n) − to print the cube of the arguments.
  3. void Pack(String s) − to print the last character of the argument.
Code Writing
Advertisements

Solution

import java.util.*;
class clOver
{
    void pack(int a, int b)
    {
        double avg = (a + b)/2.0;
        System.out.println("Average of the arguments " + a + " & " + b + " is " + avg);
    }
    void pack(int n)
    {
        System.out.println(n + "^3 = " + (n * n * n));
    }
    void pack(String s)
    {
        int len = s.length();
        System.out.println("Last char of " + s + " is " + s.charAt(len - 1));
    }
    void main()
    {
        pack(25, 30);
        pack(7);
        pack("SIMPLE LIFE");
    }
}

Output:

Average of the arguments 25 & 30 is 27.5
7^3 = 343 
Last char of SIMPLE LIFE is E

shaalaa.com
  Is there an error in this question or solution?
Chapter 11: Programs Overall - Method Overloading [Page 253]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 11 Programs Overall
Method Overloading | Q 1. | Page 253
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×