Wednesday, June 16, 2004

Exercise[15]

00:14 | 00:15 | 00:16

Code Analysis


The posts Let's go to the mall (JavaScript) and Hello! Let's go! (Java) present examples of similar code written in two different programming languages. Both code snippets define classes of objects and use instances of those objects in a sequence of statements to produce possibly entertaining output.

Try to understand how the code in these examples works, especially the code in the example that pertains to the programming language that is your primary focus (either Java if your in CS A or JavaScript if you're taking CS Principles):
  1. Identify each distinct class of objects defined by the code.
  2. Sketch a diagram that shows how the classes are related to each other.
  3. For a given class, identify the class constructor method, other methods of the class, and class fields.
  4. Pay special attention to classes that inherit fields and methods from super classes, and identify any overloaded methods that are overridden in the process.
  5. Note the special syntax used in class constructor methods to invoke the constructor of a super class.

Required Reading


If you're studying Java (CS A), then read pages 92-101 (up to the References section on page 101) from Barron's AP Computer Science A (7th edition) by Roselyn Teukolsky, M.S. Key concept discussed on these pages include:
  • Objects
  • Classes
  • The keywords public, private and static
  • Methods
    • Constructors
    • Accessor methods
    • Mutator methods
    • Static vs. instance methods
    • Overloaded methods
  • Scope
  • The keyword this
If you're studying JavaScript (CS Principles), then read pages 115-119, 166-170, 187, 199-205, and 228-233 in JavaScript: The Definitive Guide (6th edition) by David Flanagan. Key concepts discussed on these pages include:
  • Objects
  • Prototypes
  • Creating objects using:
    • Object literals
    • The keyword new
    • Object.create() 
  • Classes
  • Constructors
  • The constructor property of a prototype object
  • Invoking functions and methods
  • Methods and the keyword this
  • The methods call() and apply() and their relationship to function objects

Coding Project


If you're taking AP Computer Science A, then compare the classes in Let's go to the Mall! (JavaScript) with those in Hello! Let's go! (Java). Then create your own Java program that does something similar to Let's go to the Mall! (JavaScript) using Java classes and methods of your choosing.

If you're taking AP Computer Science Principles, then create your own program similar to Let's go to the Mall! (JavaScript) using different JavaScript classes and methods of your choosing.

In both cases, when creating your own program:
  • Your code should include at least three classes.
  • Instances of each class should be created using the keyword new.
  • At least one class should inherit from or extend another class. The constructor for at least one subclass should call the constructor of its superclass.
  • At least one method or property should be overridden by a subclass method or property.