I need help creating a Java program. Please make the code as simple to follow an
ID: 3783291 • 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
Hi, Please find my implementation.
Please let me know in case of any issue.
public class PrintMeOverload {
public static void printMe(char c, int n){
for(int i=1; i<=n; i++){
System.out.print(c);
}
System.out.println();
}
public static void printMe(int someInt, int n){
for(int i=1; i<=n; i++){
System.out.print(someInt);
}
System.out.println();
}
public static void printMe(double someDouble, char c, int n){
for(int i=1; i<=n; i++){
System.out.print(someDouble+""+c);
}
System.out.println();
}
public static void printMe(String s, int n){
for(int i=1; i<=n; i++){
System.out.print(s);
}
System.out.println();
}
public static void main(String[] args) {
printMe('a', 5);
printMe(4, 4);
printMe(5.2,'Z',3);
printMe("XXX", 5);
}
}
/*
Sample run:
aaaaa
4444
5.2Z5.2Z5.2Z
XXXXXXXXXXXXXXX
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.