3) (6 Pts) Here is a main program that doesn\'t work because the class it is usi
ID: 3585025 • Letter: 3
Question
3) (6 Pts) Here is a main program that doesn't work because the class it is using hasn't been written. On the back of this page write a complete definition for the missing class that will allow the program to run correctly. public class Main public static void main(String[] args) [ Echo e-new Echo("foo"); System.out.println(e.multi(2)); // Prints "foofoo" System.out.println(e.multi(3)); // Prints "foofoofoo" System.out.println(e); // Set word to be echoed to "foo" // Prints "word: foo (echoed 5 times)"Explanation / Answer
Main.java
public class Main {
public static void main(String[] args) {
Echo e = new Echo("foo");
System.out.println(e.multi(2));
System.out.println(e.multi(3));
System.out.println(e);
}
}
class Echo {
private String s;
private static int count;
public Echo(String s) {
this.s = s;
}
public String multi(int n) {
count+=n;
String str="";
for(int i=0;i<n;i++) {
str+=s;
}
return str;
}
@Override
public String toString() {
return "word: "+s+" (echo "+count+" times)";
}
}
Output:
foofoo
foofoofoo
word: foo (echo 5 times)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.