This unit%u2019s readings introduce the concept of constructors , methods that s
ID: 402915 • Letter: T
Question
This unit%u2019s readings introduce the concept of constructors, methods that share the same name as a class that are run whenever objects of that class are instantiated. Constructors allow you to define and build the World in a specific way. Each time you press the Reset button in the Greenfoot workspace, you force the World to redraw itself to its initial state, invoking the constructor methods of the actors.
In this Application, you place Actor subclasses called Dog into an empty subclass of World called DogWorld. You build a world of dogs and govern their behavior according to specific methods, some already defined and some that you define yourself. Download, unzip, and open the good-dog.zip scenario and complete each of the following exercises
Explanation / Answer
var d = new Dog; document.write('Dog age: ' +d.age); \Outputs "1" as expected document.write(''); document.write('Dog name: ' +d.name); \Outputs "undefined" as expected, 'cause it's a private variable. document.write('
'); document.write('Get Dog name: ' +d.getName()); \Outputs "Fido", as expected. d.name = "Stinky"; document.write('
'); document.write('Dog name Again: ' +d.name); document.write('
'); document.write('Get Dog name Again: ' +d.getName());
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.