Unformatted text preview:

2. ObjectsJava Objects vs. Real-World ObjectsInput in JavaImporting from a PackageImport allReading other typesExceptionsDefining a ClassGetter and Setter MethodsFunction vs ProcedurePropertiesUsing the classObjectEditorTracing Program ExecutionMixed Menu-based Interaction and Property EditingDebugging the user interfaceCreating instance variables for Dependent PropertiesOverloadingActive vs. Passive ObjectsSeparate class for alternative implementationsCommentsJavadoc ConventionsCommenting out Debugging CodeSingle-line vs Arbitrary CommentsWhat to CommentLong Identifiers Vs. CommentsJavadoc TagsSpecifying Classes through InterfacesNaming an InterfaceSpace & Time EfficiencySyntactic Vs. Semantic SpecificationCommenting an InterfaceEncapsulationInterface ConstantsAvoiding Code RepetitionCalling a MethodInternal vs. External CallFunction vs. Procedure CallNon-Public MethodsLeast Privilege/Need-to-Know/Information HidingNamed Constants vs. LiteralsNamed Constants vs. VariablesNatural vs. Human-Created ConstantsSpace Used by Variables, Literals, Named ConstantsUsing Variables to Remove Code RepetitionLocal Method VariablesIdentifier Visibility and ScopeInitializing vs. Un-Initializing DeclarationsUsing Procedures to Remove Code DuplicationMethods for Independent Pieces of CodeAnother Kind of ObjectAlgorithmJava ImplementationA Different RepresentationProblems with using predefined typesUsing the type PointMultiple Type ImplementationsInvoking a ConstructorDefining PointDefining a TypeSpecifying a Type: InterfaceImplementing a TypeAlternative ImplementationBottom up and Top down Interface definitionPrimitive vs Object TypesSummaryObject-based Programming COMP 114 Prasun Dewan1 2. ObjectsNow that we have a had a refresher in conventional programming, we will start our study of object-based programming. We will first look an intuitive explanation of programming objects that relates them to manufactured physical objects. Next we will understand how objects used in the context of input processing,an important issue we have not yet addressed. Input processing will introduce us also to other important programming concepts such as exceptions. To understand how objects are defined, we will use two examples. Object-oriented programming is one element of good programming style. We will look at some other such elements including creating named constant, keeping variable scope as small as possible, commenting, and initializing variables.Java Objects vs. Real-World ObjectsOne of the strengths of Java is the it allows a program to be composed of smaller structures; much as a scriptcan be broken into smaller units such as sections, paragraphs, and sentences; or a building an be broken up into rooms, doors, walls, windows, etc. The units of a building are physical objects, while the units of a script are abstract. Like the latter, the units of a program are also abstract. In fact, part of the challenge of programming will be to understand these abstractions. To make programming more intuitive (and powerful), Java and other object-based programming languages provide abstractions, called objects, which are modeled after physical objects. Coding in Java consists mainly2 of defining and interacting with these program objects. Since program objects are created by human beings, they are more like manufactured physical objects such as cars and bicycles, rather than natural objects such as trees and rocks. We interact with a (manufactured) physical object by performing different kinds of operations on it. For instance, we accelerate, brake, and steer a car. The set of operations we can perform on the car is determined by the factory that manufactured or defined it. Defining a new kind of car, thus, involves constructing a new factory for it.Similarly, we interact with a program object by performing different kinds of operations on it. Performing anoperation on the (program) object is also called invoking or executing or calling the operation; and the operation itself is called a method. The methods that can be invoked on an object are determined by the class of the object, which corresponds to the factory that defines the blue print of a manufactured physical object. Defining a new kind of computer object, then, involves creating or defining or declaring a new class for it. An object is called an instance of its class. Just as a car can be manufactured on demand by its factory;a computer object can be created on demand by insantiating its class. The reason for choosing the term “class” for a computer factory is that it classifies the objects manufactured by it. Two objects of the same 1  Copyright Prasun Dewan, 2005. 2 In a pure object-based programming language such as Smalltalk, programming consists exclusively of defining and interacting with objects. As we shall see later, Java is not such a language, since it provides both object-based and traditional programming. Other books on Java start with traditional programming, whereas, here, we are starting with object-based programming.1Object-based Programmingclass are guaranteed to have the same behavior, much as two cars produced by the same car factory are expected to work in the same way.P r o g r a m O b j e c ta d ds u b t r a c tm e t h o d se x e c u t ei n v o k ec a l lm a n u f a c t u r e d b ya c c e l e r a t eb r a k eo p e r a t i o n sp e r f o r mC l a s si n s t a n c e o fP r o g r a m O b j e c tFigure 2. Objects-1 Programming Objects vs. Manufactured Physical Objectsm a n u f a c t u r e d b ym a n u f a c t u r e d b y BufferReaderInstanceBufferReaderinstance ofBufferReaderInstanceABMISpreadsheetInstanceABMISpreadsheetinstance ofABMISpreadsheetInstanceFigure 2 Classification through factories Figure 3 Classification through classesThe following table shows the correspondence between these Java concepts and real-world entities.Java Real-worldClass FactoryComputer Object Manufactured Physical ObjectMethod OperationInvoking/Executing a Method Performing an OperationInstance of a Class Manufactured by a FactoryDefining/Declaring a Class Constructing a FactoryInstantiating a Class Manufacturing an ObjectTable 1 Java vs. Real-WorldIt is normally easier to use a manufactured object rather than build its factory blue print – that is also true of programming section. The next section illustrates object use in the context of input.2Object-based ProgrammingInput in JavaFigure 4 An Input PrinterThe user interface of


View Full Document

UNC-Chapel Hill COMP 114 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?