Question 8
Consider the Java code given below.
interface Shape {
void draw();
}
class ShapeFactory {
private String type;
public void setType(String t) {
type = t;
}
public String getType() {
return type;
}
public Shape createShape() {
switch (getType()) {
case "Circle":
return new Circle();
case "Square":
return new Square();
case "Triangle":
return new Triangle();
default:
return null;
}
}
private class Circle implements Shape {
public void draw() {
System.out.println("Drawing Circle");
}
}
private class Square implements Shape {
public void draw() {
System.out.println("Drawing Square");
}
}
private class Triangle implements Shape {
public void draw() {
System.out.println("Drawing Triangle");
}
}
}
public class ShapeDemo {
public static void main(String[] args) {
ShapeFactory sf = new ShapeFactory();
sf.setType("Square");
// --------- LINE 1 ---------
}
}
Identify the appropriate option(s) to fill in place of LINE 1 such that the output is:
Drawing Square
Shape s = sf.createShape();
s.draw();sf.draw();
sf.createShape().draw();
Shape s = sf;
s.draw();