AP Computer Science A

Unit 2: Selection and Iteration

8 topics to cover in this unit

Unit Progress0%

Unit Outline

2

Objects: Instances of Classes

This topic introduces the fundamental concept of an object as an instance of a class, emphasizing that classes serve as blueprints while objects are the concrete entities created from those blueprints. Students learn how to declare object reference variables.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Confusing a class with an object (e.g., thinking 'String' is a string, rather than 'String' being the type of a string object).
  • Believing that declaring an object reference variable (e.g., `String s;`) actually creates an object.
2

Creating and Storing Objects (Instantiation)

Students learn the process of creating an object using the `new` keyword and a constructor, and then assigning the newly created object's memory address to a reference variable. This covers the full instantiation process.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Thinking the reference variable *is* the object, rather than a pointer to it.
  • Misunderstanding `null` as an empty object instead of the absence of a reference to an object.
2

Calling Methods

This topic focuses on how to interact with objects by calling their methods using the dot operator. It distinguishes between methods that return values and `void` methods, and explains the concepts of arguments and parameters.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Forgetting the dot operator when calling an object's method.
  • Not understanding that a method's return value must be used or stored if it's not `void`.
2

Constructors

Explores constructors as special methods used to initialize the state of a new object. It covers default constructors, overloaded constructors, and the role of parameters in setting initial values.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Confusing constructors with regular methods (e.g., trying to call a constructor after an object has been created).
  • Believing that all objects *must* be initialized with specific values, overlooking default constructors.
3

String Objects: Concatenation, Literals, and Escape Sequences

Introduces the `String` class as a fundamental object type for representing text. It covers `String` literals, the concept of immutability, string concatenation using the `+` operator, and escape sequences.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Thinking `String` is a primitive type like `int` or `double`.
  • Believing that `s = s + 'x';` modifies the original string `s` in memory, rather than creating a new string and reassigning `s` to it.
3

String Methods

Focuses on common and essential methods of the `String` class for manipulating and comparing text. Key methods include `length()`, `substring()`, `indexOf()`, `equals()`, and `compareTo()`.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Using `==` to compare `String` objects, which compares memory addresses, not content.
  • Off-by-one errors when using `substring()` or `indexOf()` due to misunderstanding inclusive/exclusive bounds.
3

Wrapper Classes: Integer and Double

Explains wrapper classes (`Integer`, `Double`) that allow primitive types (`int`, `double`) to be treated as objects. It covers autoboxing, unboxing, and methods for converting between strings and primitive types.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Confusing `int` with `Integer` and `double` with `Double`, especially regarding method calls.
  • Trying to call `parseInt()` on an `int` variable instead of a `String`.
3

Exploring the Math Class

Introduces the `Math` class, a utility class that provides static methods for common mathematical functions like `abs()`, `pow()`, `sqrt()`, and `random()`. This highlights the concept of static methods and classes that are not instantiated.

Program Design and Algorithm DevelopmentCode Implementation
Common Misconceptions
  • Attempting to create an object of the `Math` class (e.g., `Math m = new Math();`).
  • Forgetting to prefix `Math.` when calling static methods (e.g., `abs(x)` instead of `Math.abs(x)`).

Key Terms

classobjectinstanceinstantiationreference variablenew operatorconstructornullmethod calldot operatorargumentparameterreturn valuedefault constructoroverloaded constructorinitializationStringliteralconcatenationescape sequenceimmutablelength()substring()indexOf()equals()compareTo()wrapper classIntegerDoubleautoboxingunboxingMath classstatic methodabs()pow()sqrt()

Key Concepts

  • Classes define the structure and behavior, objects embody them.
  • An object reference variable stores the memory address of an object, not the object itself.
  • The `new` operator allocates memory for a new object and calls a constructor.
  • Object reference variables store memory addresses, allowing multiple variables to refer to the same object.
  • Methods define the behaviors and actions an object can perform.
  • Arguments passed during a method call correspond to parameters in the method's definition.
  • Constructors ensure objects are in a valid initial state upon creation.
  • Multiple constructors (overloading) allow for different ways to initialize an object.
  • Strings are objects, not primitive types, with special literal syntax.
  • Strings are immutable; operations that appear to modify a string actually create a new string.
  • The `String` class provides a rich API for text processing.
  • Use `equals()` or `compareTo()` for comparing string content, not `==`.
  • Wrapper classes bridge the gap between primitive types and objects.
  • Autoboxing and unboxing simplify the conversion between primitives and their wrapper objects.
  • The `Math` class provides ready-to-use mathematical operations.
  • Static methods belong to the class itself, not to specific objects, and are called using the class name.

Cross-Unit Connections

  • **Unit 3: Booleans and If Statements**: Object comparison using `equals()` and `compareTo()` directly feeds into conditional logic.
  • **Unit 5: Writing Classes**: This unit lays the foundation for understanding how to design and implement your own classes, including constructors and methods.
  • **Unit 6: Array**: Arrays can store objects, requiring an understanding of object references and instantiation.
  • **Unit 7: ArrayList**: `ArrayList` specifically stores objects, making wrapper classes and the concept of objects crucial.
  • **Unit 9: Inheritance**: Builds upon the object-oriented principles introduced here, showing how classes can inherit properties and behaviors from other classes.