· Define new terms
· Describe the concepts involved in object oriented programming
· Explain methods and how they are used to manipulate properties and respond to events
· Describe properties and how they are important to the concepts of objects and classes
· Explain how objects respond to events
The primary concept of object oriented programming is obviously enough, the object. An object is a collection of information and related functions. An object may be something that represents an actual, physical object such as a Student object that holds information about a student. An object may also be something with a computer meaning such as a button on a computer form or a Window on the screen. An object may also be something completely unrelated to a physical or virtual object but be a useful way to organize information such as a piece of work to accomplish.
These objects include the information that is associated with it as well as the operations that can be performed on the object. For example, a student object would include such information as the student’s name, their year of graduation, their gender and what classes they are taking. The operations that could be performed on the student object include things like enrolling in a class, changing a homeroom, or calculating a grade point average.
The first part of object-oriented design is determining what objects are required. This can become very complicated as you start to think about the data that is required for an object and the operations that the object must support. Many of these things are not as obvious as the parts of physical object that you may be used to using to build physical objects.
Objects are one form of what computer scientists like to call Abstract Data Types or ADTs for short. An abstract data type is a <fill in>. The idea behind an ADT is that the insides, how it works, do not have to be understood by a programmer who uses one. The interface, the information that goes into the ADT and the information it returns to a programmer are clearly defined and documented. The programmer who uses an ADT only needs to know the interface. If the ADT has been properly designed and tested, the programmer can use it without having to duplicate someone else’s work.
ADTs are often used as building blocks for more complicated program structures and new ADTs. They can save programmers a lot of time and energy. In fact, Visual Studio and its various parts have been created using ADTs. Many of these existing ADTs are available for you to use in your programs. The Object class, a very powerful ADT, is the heart of objects you will use in every C# program you write.
A second important concept in object creation is encapsulation. Encapsulation means that details of an object that a programmer does not need to see or understand is hidden or buried inside the object. A programmer will understand that information is stored inside the object but does not have direct access to this information. The exact way the information is stored is not important for other programmers. They only need to know that it is there and how they have to ask for it. Think of capsules that you know in your life. Medicine uses encapsulation all the time. You have probably taken pills for different sicknesses at various times. You did not have to know exactly what is inside or how it works. You only needed to know what capsule to take for your specific medical need. You trust others to put the right things inside the capsule. You don’t usually break open the capsule to study it. You just use it.
Encapsulation is important for a number of reasons. The first is that it prevents a programmer for accidentally putting bad information into the object. The program does not access the information directly but needs to use the documented paths. These paths can perform checks to verify the data before it is saved. This prevents many types of programming problems that are difficult to find and fix.
Another important part of encapsulation is that it protects the programmer from changes that are made to the object over time. If not for encapsulation, a programmer might base assumptions on how the object works. If the way the object works is changed by the programmer responsible for the object then the second programmer’s program might break. Because of encapsulation, this will not happen as long as the documented ways to use the object do not change.
For these reasons, the ways a programmer uses an object must be carefully thought out. The more details a programmer needs to know the more likely that changes to an object may cause them problems. This implies that objects should be carefully designed before programming even begins. Careful planning always pays off in the end.
Properties are what differentiate one object from an other. This means more then just what differentiates a chair from a table but what differentiates one table from an other table.
In their most basic form, properties are the attributes that make a particular object be that object. In the physical world, we say that a bicycle has two wheels, pedals, a seat, and handlebars. If it does not have those properties, it is not a bicycle. A unicycle has only one wheel and no handlebars so is not a bicycle.
Different bicycles may have different values for some properties but are still bicycles. Even though one has 27-inch wheels and another has 24-inch wheels, bicycles with different size wheels are still bicycles. The property is the same but the value is different.
Program objects also have properties. The properties of different objects of the same type will have different values but the same names and characteristics. For example, a common object in a Windows program is a textbox. All textboxes have names and a display property called Text. The value of the Text property is displayed inside a box on a form. Different text boxes will have different names so that the programmer can identify them. Each box may also have a different set of characters to display in the text property.
Object that represent concepts also have properties. For example, you can create an object that represents students in a school. Properties would include things like a student ID number, a first and last name, and a list of classed that are being taken. An object that does not have a student ID property would not be a student object. On the other hand, each student object in your program would have a different value for the student ID property.
One of the first steps in object-oriented programming is determining the properties of the various objects that are required to solve a problem.
Objects in the physical world have functions or tasks that they are designed to accomplish. For example, a table holds things off the floor. Items are placed on a table or taken off the table. A bicycle has a pedal function that moves the wheel. The handlebar function steers the bicycle. These tasks are called methods in programming terms.
In C#, and other object oriented languages, a method is a task that is performed on behalf of an object. Methods are usually small manageable pieces of program code. These methods are part of the building blocks that make up a complete object.
Programming languages that are not object oriented use the term function to refer to the small pieces of code that are combined to make a whole program. If you have programmed in other languages you will find that methods are very similar to functions.
There are several types of methods involved in an object. Some methods will change the values of the data that is part of the object. It is possible to write object that allow their data to be directly changed by a program. This is dangerous and violated the idea of data encapsulation. It is dangerous because it assumes that the programmer who is changing the data validates the information before changing the value. It also requires that a programmer using the object knows the name and type of the value to be changed. This means that if the name or type of value changes other programs that use the class will no longer work correctly.
If a method, that is part of the object, is used to change the value then these problems can be avoided. The method can validate or check the data before it is stored. If the name or type of the value is changed then the method can be changed to handle these changes and any program that uses the object will continue to work as expected. These methods are sometimes called modifiers because they modify the encapsulated data.
Other methods are used to read encapsulated values and return them to a calling program. This allows the programmer of the class to change the way data is stored without breaking other programs that use the class. It also means that some values can be calculated when needed and not stored. This can save space and time when a value is needed infrequently. These methods are sometimes called accessers because they access the data.
A third type of method performs operations that use the data in the object to perform other types of operations. For example, objects generally include a method called ToString. The purpose of this method is to take the data in an object and return a string of characters that represent the data. This means that an integer value, a number, can be converted into a string for display in fields that expect a string. A card object, useful for programming card games, might store information internally as one number for the suit and another number for the face value. The ToString method would return something like “Ace of hearts” rather then a pair of numbers that would be meaningless to someone not other then the programmer. Because the object itself included the means to convert the data to a string, a programmer using the card class does not have to know how the data is stored. They also do not have to take the time to write, test and correct a method to do this themselves.
Other methods respond the events, or things that happen to the object, as you will see below.
Events are things that happen to objects. Objects can respond to these events. In the physical world, an event that happens to a table is that something is placed on top of it. The table responds to the event. Unless the item placed on the table is very heavy, you probably do not see the table bend under the weight but it probably does. If you place a very heavy item on a weak table you may see the table respond to this event by collapsing.
Many virtual objects, such as windows, command buttons, and menus, in a Windows program respond to events such as a mouse click or a key press. As a programmer, you will want to create methods that respond to these events.
The data inside an object is usually private. In this context, private means that the methods that are part of an object have access to the data but methods that are part of other objects do not. Public data may also be used in an object. Public data can be directly read and changed from methods that are part of completely separate objects. Public data should be avoided as it opens the door for many problems.
Methods may also be either public or private. Public methods are intended to be used by programmers writing other methods that use an object. They should be clearly named and well documented. Private methods are usually helper methods. These methods are using by other methods, both public and private, that are part of the class. Private methods are often used when one function must be used in several places. By creating a helper function as a private method, a programmer can create and test the code once and then use it throughout the class. Because the method is only written once, even though it is used in several places, it will only have to be changed once if a problem is discovered or an improvement is needed.
Other private methods are written if one function gets very big or very complicated. You will read more about this as you go in this book.
It has been said that no computer program is ever completely finished. A set of goals is created and software is built to meet those goals. Once the program is in use, more goals are found for it. Sometimes these are corrections to meet changes in process. Other times these are new features or improvements to existing features. Because more can always be added to a software product, software design is a continuous process. The process starts with a problem to solve but continues through a number of phases. These phases are called the software development cycle.
As with any problem, the first step in solving a computer problem is the completely understand the problem. In a professional development environment, this may mean hours of interviews with the people who will be involved in using the completed program. It will mean studying the existing system to understand the complications of the project and what needs to be done to deal with them. Professionals write a problem description document that explains their understanding of the project before they start writing any program code. This document is approved by the final customer before the next phase of development is begun.
Understanding the problem is no less important for small projects such as the one you will find in this book. A perfect solution for the wrong problem is of no use to anyone. Professionals are not paid for solutions that do not meet the customer’s need. Students do not get full credit for projects that do not meet the teacher’s requirements. You will want to make sure that you read all project specifications closely and ask questions about anything that is not clear to you. Asking for clarification is the professional way to make sure you understand the problem.
Sometimes beginners start to write their program as soon as the thing they understand the nature of the problem. This is a mistake. Professional programmers design and test a plan before they start to write program code. A design is the next step after understanding the problem.
Once a problem is understood, a plan may be developed. This plan is called an algorithm. An algorithm is a description of the steps used to solve a problem. You are probably used to algorithms used in mathematics. For example, if you have a right triangle and know the length of the sides but need to know the length of the hypotenuse you would use an algorithm to calculate the answer. The Pythagorean Theorem says that the square of the length of the hypotenuse is equal to the sum of the squares of the lengths. Mathematicians write this algorithm as follows: A2 + B2 = C2
This design includes three basic parts. The first part is the list of information coming into the algorithm. This information is called the input. The second part is the information that must be produced, also called output. The final, and often the most difficult, part are the steps required to convert the input and produce the output.
The algorithms and plans you will develop for programming problems will be written in a natural language or a combination of natural language and mathematics. They may also involve diagrams and charts. Some algorithms are written using something called pseudo code. Pseudo code is a combination of natural language statements and statements that look very similar to computer code. The most important thing about your plan or algorithm is that it must include all of the information and steps required to solve the problem.
An untested plan is only a little better then no plan at all. Once your algorithm is developed you must test it. Testing involves taking sample information and using it to step through each step in the algorithm and make sure that if gives the results that are expected. Good testing requires that you know the answer that a correctly designed plan would present. You cannot just assume that your plan works. If you were writing a program that used the Pythagorean theorem to calculate the length of the hypotenuse of a right triangle you would test it with known values. If you enter three as the length of one side and four is the length of the second side, your algorithm should return five as the answer.
Test values should be determined while you are developing your plan. These values will be used to test the plan as well as the finished program.
Programming will begin after the plan has been developed and tested. Much of the rest of this book is about how to turn your plan into a working program.
The reason this is called a development cycle is that once a program is completed the whole process starts all over again. New sets of problems are discovered, or problems that were set aside for the first version are revisited, and the understanding and planning begins again.
Objects are self-contained program building blocks. Objects encapsulate data and methods that can be used by programmers who do not need to understand their inner workings. The use of objects in program design makes them easier and faster to develop. Objects are at the root of object-oriented design.
The trio of properties, methods and events are the basic building and design blocks of objects in C#. An object is designed with specific properties that describe the object. Methods are included in an object to access, modify and use the data in the object. Methods are written to respond to events that happen to the object. The programmer who creates an object must carefully design the data and methods to work together with each other and to respond to the events that will be allowed for the object.
Different objects of the same type will have the same properties and methods and will respond to the same events. These different objects will have different values for some or all of their properties.
The primary steps of problem solving are:
· Understand the problem
· Develop an algorithm to solve the problem
· Test the algorithm
· Convert the algorithm into C# code
Understanding problems requires careful reading of any written instructions, questioning any ambiguous information, and careful thought. Problem solutions include descriptions of all the information required as input to the program or output from the program. It also includes the carefully thought out steps to convert inputs into outputs. A completed plan is tested using considered information and known, expected results. A program can be developed from a completed and tested plan has a lot greater chance for success then a program developed without a plan.
Abstract Data Types An abstract data type is
Accesser A method that returns information from an object.
ADT See Abstract Data Type
Encapsulation Encapsulation is the including of data and methods inside the implementation of an object so that the object can be used without knowing how the data is stored.
Events Events are things that happen to an object. A mouse click is one example of an event that can happen to Windows objects.
Methods Methods are distinct pieces of program code that perform a specific action to or on behalf of an object.
Modifiers A modifier is a method that changes data inside an object.
Object An object is a collection of information and related functions.
Private Private data or methods may be directly used only by methods that are part of an object.
Properties Properties are the information that describes an object. An object’s properties are what makes it different from other objects and what make it useful.
Public Public data or methods may be directly used by code that is not part of the object that owns them.
1. Design a Dice class. List the data that will be required by the class. List the methods that will be used to access and modify the class.
2. Design a playing card class. List the data that will be required by the class. List the methods that will be used to access and modify the class.
3. Design a student class for tracking information about a high school student. List the data that will be required by the class. List the methods that will be used to access and modify the class.
4. Design a coin class. List the data that will be required by the class. List the methods that will be used to access and modify the class.