I need help creating a Java program. Please make the code as simple to follow an
ID: 3783255 • Letter: I
Question
I need help creating a Java program. Please make the code as simple to follow and understand as possible. Here is the exercise:
Write a program with the following overloaded methods:
public static void printMe(char c, int n)
public static void printMe(int someInt, int n)
public static void printMe(double someDouble, char c, int n)
public static void printMe(String s, int n)
The method printMe will print the arguments in the method call n times. For example the call printMe(‘a’, 5) will display
aaaaa
The call printMe(5.2,’Z’,3) will display
5.2Z5.2Z5.2Z
Include a main that will call each of the printMe methods. You may hardcode the arguments as shown in the examples above rather than gather this information from the user.
Explanation / Answer
OverloadedMethodsDemo.java
public class OverloadedMethodsDemo {
public static void main(String[] args) {
//Calling overloaded methods
printMe('a', 5);
printMe(5,4);
printMe(5.2,'Z',3);
printMe("Hello",6);
}
//These over loaded methods display output based on number of times
private static void printMe(String str, int k) {
for(int i=0;i<k;i++)
System.out.print(str);
System.out.println();
}
private static void printMe(double d, char c, int k) {
for(int i=0;i<k;i++)
System.out.print(d+""+c);
System.out.println();
}
private static void printMe(int i, int k) {
for(int m=0;m<k;m++)
System.out.print(i);
System.out.println();
}
private static void printMe(char c, int k) {
for(int m=0;m<k;m++)
System.out.print(c);
System.out.println();
}
}
__________________
Output:
aaaaa
5555
5.2Z5.2Z5.2Z
HelloHelloHelloHelloHelloHello
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.