Recall the \"Heroes vs. Goblins\" question from assignment 3. The following code
ID: 3861779 • Letter: R
Question
Recall the "Heroes vs. Goblins" question from assignment 3. The following code contains a syntax or logic error. Explain the error and show how to correct it. Line 1 Wizard merlin = new Hero ("Merlin"); Recall the "Ships" question from assignment 3. The following code contains a syntax or logic error. Explain the error and show how to correct it. Line 1 Ship fleet = new Ship[2]; Line 2 fleet(0) = new CruiseShip("Loveboat", 2016, 3000);//3000 passengers, built in 2016 Line 3 String name = fleet[0].getName(); Line 4 int year = fleet[0].getYear(); Line 5 int passengers = fleet[0].getNumPassengers();Explanation / Answer
Syntax error:
Wizard merlin=new Hero("Merlin");
Explanation:
An object,merlin is created of class Wizard type
must be instatiated with same class name "Wizard".
But the declaration is incorrect since
"Hero" is incorrect class to instatiate the Wizard
class object.
Only Wizard class has constructor with same name
as Wizard ,not Hero.
Correction:
Wizard merlin=new Wizard("Merlin");
---------------------------------------------------------------
Syntax Error:
Line 1 Ship fleet= new Ship[2]; //Error
The declaration of the array of type Ship
of size 2 is incorrect.
Correct code:
Ship[] fleet= new Ship[2];
Line2 ,Line3 ,Line4 and Line5 have no errors.
CruiseShip is a subclass of Ship class .
Line3 returns the ship name as string of object fleet[0]
Line4 returns the year of the ship built of object fleet[0]
Line5 returns the number of passengers of object fleet[0]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.