Overload A Function
1.
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.
SOLUTION
class Overload2019
{
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 " + sum);
}
public void series(int p)
{
int term=0;
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("Sum of series
is " + sum);
}
/**main function to run this program
public static void main(String args[])
{
Overload2019 ob = new Overload2019();
ob.series(2,4);
ob.series(3);
ob.series();
} */
}
2.
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
SOLUTION
class Overload2018
{
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;
}
/**main function to run this program
public static void main(String args[])
{
Overload2018 ob = new Overload2018();
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));
}*/
}
3.
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 s1) – to display only the vowels from
string s1 , after converting it to lower case.
Example :Input:S1= “computer”
Output: o u e
SOLUTION
class Overload2017
{
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 + " "
);
}
}
}
/**main function to run this program
public static void main(String args[])
{
Overload2017 ob = new Overload2017();
ob.check("overload",'o');
ob.check("overloaded");
}*/
}
NOTE: This
question is also a part of string section
4.
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)
SOLUTION
class Overload2016
{
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 product = 1;
for (int j = 1; j <= i; j++) {
product = product * j;
}
sum = sum + product;
}
System.out.println("Sum = " + sum);
}
/**main function to run this program
public static void main(String args[])
{
Overload2016 ob = new Overload2016();
ob.SumSeries(4,2);
ob.SumSeries();
}*/
}
5.
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)
SOLUTION
class Overload2015{
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);
//String s = s1 + " " + s2;
System.out.println(s);
}
/**main function to run this program
public static void main(String args[])
{
Overload2015 ob = new Overload2015();
ob.joyString("computer",'o','i');
ob.joyString("topicwise computer icse questions");
ob.joyString("computer","programming");
}*/
}
NOTE:This
question is also a part of string section
6.
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)
SOLUTION
class Overload2014{
public double area(double a, double b, double c){
double s = (a + b + c) / 2.0;
System.out.println(s);
double areaTriangle = Math.sqrt(s * (s
- a) * (s - b) * (s - c));
return areaTriangle;
}
public double area(int a, int b, int height){
double areaTrapezium = 1.0 / 2 * height
* (a + b);
return areaTrapezium;
}
public double area(double diagonal1, double diagonal2){
double areaRhombus = 1.0 / 2 *
(diagonal1 * diagonal2);
return areaRhombus;
}
/**main function to run this program
public static void main(String
args[])
{
Overload2014 ob = new Overload2014();
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));
}*/
}
7.
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 / a2 + 4 / a5
+ 7 / a8 + 10 / a11 + … to n terms.
SOLUTION
class Overload2013{
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;
}
/**main function to run this program
public static void main(String args[])
{
Overload2013 ob = new Overload2013();
System.out.println(ob.series(3.0));
System.out.println(ob.series(1.9,5.0));
}*/
}
8.
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:
*
**
***
SOLUTION
class Overload2012{
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(){
System.out.println("*");
System.out.println("**");
System.out.println("***");
}
/**main function to run this program
public static void main(String args[])
{
polygon(5,'o');
polygon(4,7);
polygon();
}*/
}
NOTE:This question is also a part of
pattern section
9.
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.
SOLUTION
class Overload2011{
public static void compare(int a, int b){
int large = (a > b)? a : b;
System.out.println(large);
}
public static void compare(char a, char b){
char large = (a > b)? a : b;
System.out.println(large);
}
public static void compare(String a, String b){
String large = (a.length() >
b.length())? a : b;
System.out.println(large);
}
/**main function to run this program
public static void main(String args[])
{
compare(9,8);
compare('o','i');
compare("domain","double");
}*/
}
NOTE: This question is also a part
of string section
10.
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.
SOLUTION
class Overload2009
{
public static 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("Square =
" + c);
}
}
public static 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 static void num_calc(String s1, String s2){
if(s1.equalsIgnoreCase(s2))
System.out.println("They are
equal");
else
System.out.println("They are unequal");
}
/**main function to run this program
public static void main(String args[])
{
num_calc(1,'s');
num_calc(1,2,'p');
num_calc("java","j2se");
}*/
}
NOTE: This question is also a part
of string section
No comments:
Post a Comment