Question: Consider the following code (assume that 'Cat' and 'Dog' classes exist that each have one private member variable 'name', a constructor that takes String

Consider the following code (assume that 'Cat' and 'Dog' classes exist that each have one private member variable 'name', a constructor that takes String name, and a getter called 'getName'): ArrayList myAnimals = new ArrayList (); myAnimals.add(new Cat("Buttons")); myAnimals.add(new Dog("Ralph")); myAnimals.add(new Cat("Elizabeth")); Cat myCat = myAnimals.get(0); a. [2 marks] What line of code will result in an error, what is that error, and why is it occurring? What can we add to that line to fix the error? Explain. b. [2 marks] Assume that we swap lines two and three with each other and keep your solution to part (a) in the code. Do we still get an error? If so, how is it different from the previous error? Explain. c. [2 marks] Discuss the issue(s) of creating an ArrayList as demonstrated in the first line of the above code. What problems can this introduce? Explain. d. [2 marks] What are some other examples can we use similar programming structures as this? Maximum number of characters (including HTML tags added by text editor): 32,000 Show Rich-Text Editor (and character count)
Step by Step Solution
There are 3 Steps involved in it
a The line of code that will result in an error is java Cat myCat myAnimalsget0 The error is Incompa... View full answer
Get step-by-step solutions from verified subject matter experts
