Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3) (6 Pts) Here is a main program that doesn\'t work because the class it is usi

ID: 3586077 • 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)f 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)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote