Skip to content

Commit

Permalink
Merge pull request bhoffman0#5 from barbarer/master
Browse files Browse the repository at this point in the history
Changes to 2.1 and 2.2 to add more practice problems, add subheadings, reduce text
  • Loading branch information
barbarer authored Oct 17, 2019
2 parents 63b75cd + aa2e035 commit b297dad
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 19 deletions.
83 changes: 73 additions & 10 deletions _sources/Unit2-Using-Objects/topic-2-1-objects-intro-turtles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ Today, we can play with virtual turtles in a graphical world. Below is a sample


The program above creates a ``World`` object called ``world`` and a ``Turtle`` object called ``yertle`` and places ``yertle`` in the center of the world. The code
asks ``yertle`` to go forward, turn left, and then go forward. As the turtle moves it draws with its pen.
Notice that case matters in Java, so ``world`` and ``World`` are two different things.
asks ``yertle`` to go forward, turn left, and then go forward. It didn't tell the turtle how much to go forward, so it goes forward 100 pixels by default. As the turtle moves it draws with its pen.
There is hidden Java code that defines the ``World`` and ``Turtle`` classes. Notice that a world was first
created and then a turtle. Turtles need to be created in a world.

.. note ::
Case matters in Java, so ``world`` and ``World`` are two different things. Also, notice that the **dot operator** (.) is used to run an object's method. You can think of the (.) as asking the object to do something (execute one of its methods). For example, ``yertle.forward()`` asks the turtle ``yertle`` to go ``forward``.
.. mchoice:: 2_1_turle_dir
:practice: T
:answer_a: North
Expand All @@ -137,6 +140,7 @@ Another way to say this is that a **class** in Java defines a new **abstract dat
Also notice that the **dot operator** (.) is used to run an object's method. You can think of the (.) as asking the object to do something (execute one of its methods). For example, ``yertle.forward()`` asks the turtle ``yertle`` to go ``forward``. It doesn't tell ``yertle`` how much to go forward, so it goes forward 100 pixels by default. The parentheses ``()`` after a method name are there in case you need to give the method **arguments** (some data) to do its job, for example to go forward 50 pixels instead of 100. Try changing the code above to go forward 50 pixels instead and then run it again. What happens?

.. parsonsprob:: 2_1_Turtle_L
:practice: T
:numbered: left
:adaptive:
:noindent:
Expand Down Expand Up @@ -172,7 +176,52 @@ Also notice that the **dot operator** (.) is used to run an object's method. You
} // end main
} // end class

Classes can **inherit** attributes and methods from another class in Java. Here is a class diagram that shows some of the attributes and methods that the class ``Turtle`` inherits from the ``SimpleTurtle`` class.

A computer doesn't automatically know what we mean by a robot turtle or world. We have to write Java classes to define what we
mean. The class defines the data that every turtle object knows about itself (called **fields** or **attributes**) like where it is in the world and which way it is facing. The class also defines
what objects of the class can do (called **methods** or **behaviors**) like ``turnRight`` and move ``forward``.

.. mchoice:: 2_1_type_object
:answer_a: object
:answer_b: class
:answer_c: attribute
:answer_d: method
:correct: a
:feedback_a: Yes, yertle is an object of the Turtle class.
:feedback_b: A class defines the data and behavior for all objects of that type.
:feedback_c: An attribute is something the object knows about itself.
:feedback_d: A method is something an object can do like go forward.

What type of thing is yertle in the program above?

.. mchoice:: 2_1_type_turn_right
:answer_a: object
:answer_b: class
:answer_c: attribute
:answer_d: method
:correct: d
:feedback_a: An object has data and behavior.
:feedback_b: A class defines the data and behavior for all objects of that type.
:feedback_c: An attribute is something the object knows about itself.
:feedback_d: A method is something an object can do like turn right.

What type of thing is turnRight in the program above?

.. mchoice:: 2_1_type_pos
:answer_a: object
:answer_b: class
:answer_c: attribute
:answer_d: method
:correct: c
:feedback_a: An object has data and behavior.
:feedback_b: A class defines the data and behavior for all objects of that type.
:feedback_c: An attribute is something the object knows about itself like its position.
:feedback_d: A method is something an object can do like turn right.

What type of thing is the position of a turtle in a world?


Classes can **inherit** attributes and methods from another class in Java, just like people can inherit money from a relative. Here is a class diagram that shows some of the attributes and methods that the class ``Turtle`` inherits from the ``SimpleTurtle`` class.

.. creately.com for figure
Expand All @@ -185,6 +234,20 @@ Classes can **inherit** attributes and methods from another class in Java. Here

Figure 2: Turtle Class Diagram

.. mchoice:: 2_1_type_turn
:practice: T
:answer_a: object
:answer_b: class
:answer_c: attribute
:answer_d: method
:correct: d
:feedback_a: An object has data and behavior.
:feedback_b: A class defines the data and behavior for all objects of that type.
:feedback_c: An attribute is something the object knows about itself.
:feedback_d: A method is something an object can do like turn.

A turtle object knows how to turn by a specified number of degrees. What type of thing is turn?

.. parsonsprob:: 2_1_Turtle_Turn
:numbered: left
:adaptive:
Expand Down Expand Up @@ -294,7 +357,7 @@ two turtle objects are created: ``yertle`` and ``myrtle``. You can name your tu
What are Classes and Objects?
-----------------------------

In Java, a **class** is used to define a new data type (classify something). The class defines what objects of the class need to know (attributes or instance variables) and do (behaviors or methods). A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.
In Java, a **class** is used to define a new **abstract data type** (classify something). The class defines what objects of the class need to know (attributes or instance variables) and do (behaviors or methods). A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.

There are many classes that are part of the Java language, but you only have to know a few of these for the AP CS A exam (``String``, ``Math``, ``System``, ``ArrayList``).

Expand Down Expand Up @@ -434,7 +497,7 @@ Summary

- An **abstract data type** is a definition of the attributes and methods for all objects of a type (of the same class).

- An **instance variable** is another name for an attribute, which is data an object knows about itself such as its position.
- An **instance variable** is another name for an attribute, which is data an object knows about itself such as its position.


AP Practice
Expand All @@ -457,13 +520,13 @@ AP Practice

A student has created a ``Dog`` class. The class contains variables to represent the following.
- A String variable called ``breed`` to represent the breed of the dog
- An int variable called ``age`` to represent the age of the dog
- An int variable called ``age`` to represent the age of the dog
- A String variable called ``name`` to represent the name of the dog

The object ``pet`` is declared as type Dog.
Which of the following descriptions is accurate?


.. mchoice:: AP2-1-2
:practice: T
:answer_a: An attribute of the myParty object is boolean.
Expand All @@ -472,7 +535,7 @@ AP Practice
:answer_d: An attribute of the Party instance is myParty.
:answer_e: An instance of the Party object is numOfPeople.
:correct: c
:feedback_a: An attribute of myParty is numOfPeople.
:feedback_a: An attribute of myParty is numOfPeople.
:feedback_b: myParty is an instance of the Party class.
:feedback_c: myParty is an object that is an instance of the Party class.
:feedback_d: An attribute of the Party class is numOfPeople.
Expand All @@ -482,7 +545,7 @@ AP Practice
- An int variable called ``numOfPeople`` to represent the number of people at the party.
- A boolean variable called ``discoLightsOn`` to represent whether the disco ball is on.
- A boolean variable called ``partyStarted`` to represent whether the party has started.

The object ``myParty`` is declared as type Party. Which of the following descriptions is accurate?


Expand Down
74 changes: 65 additions & 9 deletions _sources/Unit2-Using-Objects/topic-2-2-constructors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ A new object is created with the ``new`` keyword followed by the class name (``n
World world = new World(); // create a new World object
Turtle t = new Turtle(world); // create a new Turtle object
There can be more than one constructor defined in a class. This is called **overloading** the constructor. There is usually a constructor that has no parameters (nothing inside the parentheses following the name of the constructor) like the ``World()`` constructor above. This is also called the **no-argument constructor**. The **no-argument** constructor usually sets the attributes of the object to default values. There can also be other constructors that take parameters like the ``Turtle(world)`` constructor call above. A **parameter** (also called **actual parameter** or **argument**) is a value that is passed into a constructor and used to initialize the attributes of an object.
The ``World`` class actually has 2 constructors that take different parameters.
Overloading Constructors, the No-Argument Constructor, and Parameters
----------------------------------------------------------------------

There can be more than one constructor defined in a class. This is called **overloading** the constructor. There is usually a constructor that has no parameters (nothing inside the parentheses following the name of the constructor) like the ``World()`` constructor above. This is also called the **no-argument constructor**. The **no-argument** constructor usually sets the attributes of the object to default values. There can also be other constructors that take parameters like the ``Turtle(world)`` constructor call above. A **parameter** (also called **actual parameter** or **argument**) is a value that is passed into a constructor. It can be used to initialize the attribute of an object.
The ``World`` class actually has 2 constructors. One doesn't take any parameters and one takes the world's width and height.


.. figure:: Figures/worldConstructors.png
Expand Down Expand Up @@ -87,8 +90,10 @@ The ``World`` class actually has 2 constructors that take different parameters.

Which of these is valid syntax for creating and initializing a World object?

The World Class Constructors
----------------------------------------------------------

The ``World()`` constructor creates a graphical window with 640x480 pixels. The ``World(int width, int height)`` constructor takes two integer parameters, and initializes the ``World`` object's width and height to them, for example ``new World(300,400)`` creates a 300x400 pixel world.
The constructor that doesn't take any parameters, ``World()``, creates a graphical window with 640x480 pixels. The ``World(int width, int height)`` constructor takes two integer parameters, and initializes the ``World`` object's width and height to them, for example ``new World(300,400)`` creates a 300x400 pixel world.

.. code-block:: java
Expand All @@ -105,9 +110,12 @@ The ``World()`` constructor creates a graphical window with 640x480 pixels. The

Figure 2: The coordinate (0,0) is at the top left of the Turtle world.

The Turtle Class Constructors
----------------------------------------------------------

The ``Turtle`` class also has multiple constructors, although it always requires a world as an parameter in order to have a place to draw the turtle. The default location for the turtle is right in the middle of the world.

There is another ``Turtle constructor`` that places the turtle at a certain (x,y) location in the world, for example at the coordinate (50, 100) below. The coordinate (0,0) is at the top left corner of the world.
There is another ``Turtle constructor`` that places the turtle at a certain (x,y) location in the world, for example at the coordinate (50, 100) below.

.. code-block:: java
Expand Down Expand Up @@ -147,8 +155,24 @@ There is another ``Turtle constructor`` that places the turtle at a certain (x,y
}
}

You can also declare an **object variable** and initialize it to **null** (``Turtle t1 = null;``). An object variable holds a **reference** to an object. A **reference** is a way to find the object in memory. A reference is like a tracking number that you can use to track the location of a package. The code ``Turtle t1 = null;`` creates a variable ``t1`` that refers to a ``Turtle`` object, but the ``null`` means that it doesn't refer to an object yet. You could later create the object and set the object variable to refer to that new object (``t1 = new Turtle(world)``). However, you will typically declare an object variable and initialize it in the same line of code (``Turtle t1 = new Turtle(world);``).
Object Variables and References
---------------------------------

You can also declare an **object variable** and initialize it to **null** (``Turtle t1 = null;``). An object variable holds a **reference** to an object. A **reference** is a way to find the object in memory. A reference is like a tracking number that you can use to track the location of a package. The code ``Turtle t1 = null;`` creates a variable ``t1`` that refers to a ``Turtle`` object, but the ``null`` means that it doesn't refer to an object yet. You could later create the object and set the object variable to refer to that new object (``t1 = new Turtle(world)``). However, you will typically declare an object variable and initialize it in the same line of code (``Turtle t2 = new Turtle(world);``).

.. code-block:: java
World world = new World();
Turtle t1 = null;
t1 = new Turtle(world);
Turtle t2 = new Turtle(world);
.. note ::
Notice that you only specify the type (class name) for an object reference when you declare it (``Turtle t1 = null``) and not when you assign it a value (``t1 = new Turtle(world)``).
Constructor and Method signatures
-----------------------------------

.. |turtle documentation| raw:: html

Expand Down Expand Up @@ -257,8 +281,10 @@ Here is the |documentation| for the GregorianCalendar class which lists two cons
- GregorianCalendar() : Constructs a default ``GregorianCalendar`` using the current time in the default time zone with the default locale.
- GregorianCalendar(int year, int month, int dayOfMonth) : Constructs a ``GregorianCalendar`` with the given date set in the default time zone with the default locale.

Formal Parameters, Actual Parameters, and Call by Value
----------------------------------------------------------

When a constructor like ``GregorianCalendar(2001,1,1)`` is called, the **formal parameters**, (year, month, dayOfMonth) in this example, are filled in with copies of the **actual parameters** (or **arguments**), which are (2001,1,1) in this example, using a process called **call by value** that copies values, and these values in turn are used by the constructor to initialize the instance variables for the new object.
When a constructor like ``GregorianCalendar(2001,1,1)`` is called, the **formal parameters**, (year, month, dayOfMonth), are set to copies of the **actual parameters** (or **arguments**), which are (2001,1,1). This is also called **call by value** which means that copies of the actual parameter values are passed to the constructor. These values are used to initialize the object's attributes.

.. figure:: Figures/parameterMapping.png
:width: 600px
Expand All @@ -268,7 +294,35 @@ When a constructor like ``GregorianCalendar(2001,1,1)`` is called, the **formal

Figure 4: Parameter Mapping

The type of the values being passed in as arguments have to match the type of the formal parameter variables. We cannot give a constructor a ``String`` object when it is expecting an ``int``. And the order of the arguments matters. If you mix up the year and month in the ``GregorianCalendar`` constructor, you will get a completely different date!
The type of the values being passed in as arguments have to match the type of the formal parameter variables. We cannot give a constructor a ``String`` object when it is expecting an ``int``. The order of the arguments also matters. If you mix up the year and month in the ``GregorianCalendar`` constructor, you will get a completely different date!

.. mchoice:: 2_2_formal_parms
:practice: T
:answer_a: objects
:answer_b: classes
:answer_c: formal parameters
:answer_d: actual parameters
:correct: c
:feedback_a: Objects have attributes and behavior.
:feedback_b: A class defines the data and behavior for all objects of that type.
:feedback_c: A formal parameter is in the constructor's signature.
:feedback_d: A actual parameter (argument) is the value that is passed into the constructor.

In ``public World(int width, int height)`` what are ``width`` and ``height``?

.. mchoice:: 2_2_actual_parms
:practice: T
:answer_a: objects
:answer_b: classes
:answer_c: formal parameters
:answer_d: actual parameters
:correct: d
:feedback_a: Objects have attributes and behavior.
:feedback_b: A class defines the data and behavior for all objects of that type.
:feedback_c: A formal parameter is in the constructor's signature.
:feedback_d: A actual parameter (argument) is the value that is passed into the constructor.

In ``new World(150, 200)`` what are ``150`` and ``200``?

In Unit 5, you will learn to write your own classes. However, if you see a class definition on the AP exam, like the one below for a class called ``Date``, you should be able to pick out the attributes (instance variables) and the constructors and know how to use them.

Expand Down Expand Up @@ -366,11 +420,13 @@ Summary

- A **no-argument constructor** is a constructor that doesn't take any passed in values (arguments).

- **Parameters** allow values to be passed to the constructor to initialize the newly created object's attributes. These values are called the **parameters** or **actual parameters** or **arguments**.
- **Parameters** allow values to be passed to the constructor to initialize the newly created object's attributes.

- The **parameter list**, in the header of a constructor, is a list of the type of the value being passed and a variable name. These variables are called the **formal parameters**.

- **Actual parameters** are passed using **call by value** which initializes the formal parameters with copies of the actual parameters' values. The values passed to a constructor must be compatible with the types identified in the formal parameter list.
- **Actual parameters** are the values being passed to a constructor. The formal parameters are set to a copy of the value of the actual parameters.

- **Formal parameters** are the specification of the parameters in the constructor header. In Java this is a list of the type and name for each parameter (``World(int width, int height``).

- **Call by value** means that when you pass a value to a constructor or method it passes a copy of the value.

Expand Down

0 comments on commit b297dad

Please sign in to comment.