Advertisements
Advertisements
Question
Write a program with overloaded methods to implement the following:
Overload a method fnTab(), given that:
- int fnTab(String s) − to return how many digits are present in the argument string.
- String fnTab (String s2, char cd) − to create a new string from argument string s2 such that character cd is removed from it (if present).
- String fnTab (String s1, String s2) − to concatenate the shorter string behind the longer string and return that. For equal length of both strings, return "O".
Code Writing
Advertisements
Solution
import java.util.*;
class clOver
{
int fnTab(String s)
{
int c = 0;
for(int k = 0; k < s.length(); k++)
{
char ch = s.charAt(k);
if(ch >= '0' && ch <= '9')
{
c++;
}
}
return c;
}
String fnTab(String s, char cd)
{
String s2 = "";
for(int k = 0; k < s.length(); k++)
{
char ch = s.charAt(k);
if(ch != cd)
{
s2 = s2 + ch;
}
}
return s2;
}
String fnTab(String s1, String s2)
{
String s3 = "";
if(s1.length() > s2.length())
{
s3 = s1 + s2;
}
else if(s2.length() > s1.length())
{
s3 = s2 + s1;
}
else
{
s3 = "0";
}
return s3;
}
void main()
{
clOver obt = new clOver();
int R1 = fnTab("Code 72#Zpp81/93");
System.out.println(" R1 = " + R1);
String R2 = fnTab("january 31, march 31, august 31", 'a');
System.out.println(" R2 = " + R2);
String R3 = fnTab("FEBRUARY”, "JUNE");
System.out.println(" R3 = " + R3);
String R4 = fnTab("NOVEMBER", “DECEMBER");
System.out.println(" R3 = " + R4);
String R5 = fnTab("APRIL", “AUGUST”);
System.out.println(" R3 = "+ R5);
}
}
Output:
R1 = 6
R2 = jnury 31, mrch 31, ugust 31
R3 = FEBRUARYJUNE
R3 = 0
R3 = AUGUSTAPRIL
shaalaa.com
Is there an error in this question or solution?
