Computer Applications
Define a class to overload the method transform as follows:
int transform(int n) – to return the sum of the digits of the given number
Example: n = 458
output : 17
void transform(String s) – to convert the given String to upper case and print
Example: if S = “Blue”
Output : BLUE
void transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops.
Example: if ch = ‘@’
Output :
@@@
@@@
@@@
Answer
import java.util.Scanner;
public class OverloadTransform {
int transform(int n) {
int sum = 0;
while (n > 0) {
int d = n % 10;
sum += d;
n /= 10;
}
return sum;
}
void transform(String s) {
String str = s.toUpperCase();
System.out.println(str);
}
void transform(char ch) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(ch);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
OverloadTransform obj = new OverloadTransform();
System.out.print("Enter an integer: ");
int num = sc.nextInt();
System.out.println("Sum of digits: " + obj.transform(num));
sc.nextLine();
System.out.print("Enter a string: ");
String str = sc.nextLine();
obj.transform(str);
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
obj.transform(ch);
}
}Output
Related Questions
Define a class with following specifications.
class name: Hotel
Member variables:
String name – stores name of customer name
long mobno – stores mobile number
int days – stores number of days customer stayed in hotel
int bill – stores customer billMember method:
void input() – input values using Scanner class methods only
void charge() – calculate bill as per the following criteriadays charge/day first 3 days 1000 Rs/ day next 4 days 900 Rs/day > 7 days 800 Rs/day bill = bill + gst (18% of bill)
void print() - Display customer name, mobile number and bill.
Invoke all the above methods in main method with the help of an object.
Define a class to accept values into a 3x3 integer array and print the product of each row elements.
Example:
3 1 2 4 2 1 5 1 2 Output:
Row 0 – 6
Row 1 – 8
Row 2 – 10Define a class to accept a string. Check if it is a Special String or not.
A String is Special if the number of vowels equals to the number of consonants.
Example: MINE
Number of vowels = 2
Number of Consonants = 2Define a class to accept a number and check if the sum of the first digit and the last digit is an even number or an odd number.
Example:
N = 2396 N = 9316 First digit: 2 9 Last digit: 6 6 Sum: 8 15 Output: Sum is even Sum is odd