Advertisements
Advertisements
Question
Write a program as per the given specification.
Design a class to overload a method Action () given that
- void Action (int, int) - Print the larger variable
- void Action (char, char) - Print the first character in lower case, second character in upper case [assume both are alphabets]
- void Action (String, String) – Print the longer string
Write the above methods and the main () method to call the three overloaded methods.
Code Writing
Advertisements
Solution
import java.util.*;
class clOver
{
void Action(int a, int b)
{
if(a > b)
{
System.out.println("Larger is :" + a);
}
else if(b > а)
{
System.out.println("Greater is :" + b);
}
else
{
System.out.println("Numbers are Equal
}
}
void Action(char p, char q)
{
boolean valid = false;
if (p>='A' && p <= 'Z'(
{
p = Character.toLowerCase(p);
valid = true;
}
if (q >= 'a' && q <= 'z')
q = Character.toUpperCase(q);
valid = true;
}
if(valid = false)
{
System.out.println("Not Valid ");
}
else
{
System.out.println(p +""+ q);
}
}
void Action (String s1, String s2)
{
if(s1.length() > s2.length())
{
System.out.println("Longer String is : "+ s1);
}
else if(s2.length() > s1.length())
{
System.out.println("Bigger String is: "+ s2);
}
else
{
System.out.println("Strings have equal length. ")
}
}
void main()
{
clOver vob = new clOver();
vob.Action(25, 45);
vob.Action('A', 'v');
vob.Action('?', '$');
vob.Action("Hydrogen", "Oxygen");
}
}
Output:
Greater is 45
a V
Not Valid
Longer String is Hydrogen
shaalaa.com
Is there an error in this question or solution?
