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

See the attached Question2.java source file. Its main method prompts the user to

ID: 668757 • Letter: S

Question

See the attached Question2.java source file. Its main method prompts the user to enter a list of animals and then prints out a list of corresponding animal noises once they are finished.
Write the code necessary to make Question2.java compile and produce the output below when run. You will need to create a MakesSound interface with a public method makeNoise() as well as Cat, Dog and Cow classes that implement the MakesSound interface.
Sample I/O
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
1
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
1
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
1
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
2
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
1
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
1
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
3
What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit
4
The animals want to say goodbye:
Meeow
Meeow
Meeow
Woof!
Meeow
Meeow
Moo!

import java.util.ArrayList;

import java.util.List;
import java.util.Scanner;

public class Question2 {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int option;
List animals = new ArrayList();
do {
System.out.println("What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit");
option = s.nextInt();
switch (option) {
case 1:
animals.add(new Cat());
break;
case 2:
animals.add(new Dog());
break;
case 3:
animals.add(new Cow());
break;
}
} while (option != 4);

System.out.println("The animals want to say goodbye:");

for (int i = 0; i < animals.size(); i++) {
System.out.println(((MakesSound)animals.get(i)).makeNoise());
}

}

}

Explanation / Answer

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int option;
List animals = new ArrayList();
do {
System.out.println("What animal do you see? (1) Cat (2) Dog (3) Cow (4) quit");
option = s.nextInt();
switch (option) {
case 1:
animals.add(new Cat());
break;
case 2:
animals.add(new Dog());
break;
case 3:
animals.add(new Cow());
break;
}
} while (option != 4);
System.out.println("The animals want to say goodbye:");
for (int i = 0; i < animals.size(); i++) {
System.out.println(((MakesSound)animals.get(i)).makeNoise());
}
}
}