Exercises
-
Which of the following are invalid names? Why?
Int
playNextSong
6_05
_calloc
Xx
alphaBetaRoutine
clearScreen
_1312
z
ReInitialize
_
A$
-
Based on the example of the car in this chapter, think of an object you use every day. Identify a class for that object and write five actions you do with that object.
-
Given the list in exercise 2, use the following syntax to rewrite your list in this format:
[instance method];
-
Imagine that you owned a boat and a motorcycle in addition to a car. List the actions you would perform with each of these. Do you have any overlap between these actions?
-
Based on question 4, imagine that you had a class called Vehicle and an object called myVehicle that could be either Car, Motorcycle, or Boat. Imagine that you wrote the following:
[myVehicle prep]; [myVehicle getGas]; [myVehicle service];
Do you see any advantages of being able to apply an action to an object that could be from one of several classes?
-
In a procedural language such as C, you think about actions and then write code to perform the action on various objects. Referring to the car example, you might write a procedure in C to wash a vehicle and then inside that procedure write code to handle washing a car, washing a boat, washing a motorcycle, and so on. If you took that approach and then wanted to add a new vehicle type (see the previous exercise), do you see advantages or disadvantages to using this procedural approach over an object-oriented approach?
-
Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of a point and retrieve their values. Write an Objective-C program to implement your new class and test it.