Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This question has 4 parts. A) The following code has a compiler error. In which line do you see the error, and what causes the
This question has 4 parts. A) The following code has a compiler error. In which line do you see the error, and what causes the program to generate such an error? 3 public class MyFactory { 4 private String name; 5 private int id; 6 private static int counter = 0; 7 private MyFactory(String name) { 8 this.name = name; 9 this.id = counter; 10 } 11 public String getName(){ 12 return this.name; 13 } 14 public int getId(){ 15 return this.id; 16 } 17 public static String getInfo(){ 18 return + counter + " + name; 19 } 20} B) Assume that we were able to correct the error in this code. Write a static factory method called getInstance() for this class, which gets a string for the name as its input parameter. The counter variable should be incremented before the new instance is created so that each new instance gets a new id. C) Write an accessor method by which a client can get access to the counter variable. D) If a client creates an instance of MyFactory and names it mf1, and then prints the object by System.out.println(mf1), what will be printed? Explain, by writing code, how you can change this output so that the name followed by a space followed by the id of mf1 is printed by System.out.println(f1). 7 A B I E E o U x2 x
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started