Unformatted text preview:

Object-based ProgrammingProgram Components ~ Physical ObjectsProgram Objects ~ Manufactured ObjectsClassification through FactoriesClassification through ClassesUsing vs. Creating ObjectsString InputSlide 8Reading a StringChained constructionTry-Catch BlockImporting a PackageImport all vs. selective importInteger InputSlide 15readInt()Alternative readInt()Reading other primitive valuesSlide 19ABMISpreadsheetDeclaring Instance VariablesInstance VariablesOutside Access to a ClassAccessing Instance Variables Via Public MethodsCoding the MethodsSlide 26Coding Getter and Setter MethodsFunctions vs. ProceduresFunction vs. ProcedureSlide 30Assignment StatementPropertiesRead-Only and Editable PropertiesProperties ClassificationUsing ABMISpreadsheetABMIDriverSlide 37Slide 38ABMIEditorObjectEditor.edit()Slide 41Calling Getter and Setter MethodsSlide 43Tracing Method CallsActual TraceModified ABMISpreadsheetProperties + Class MenuEditing in slow motion : Initial valueEditing in slow motion: text string editedEditing in slow motion: return triggers setter and getter callsRenaming getter methodObjectEditor does not know it is getterReducing AccessHeight is not a readable propertyChanging setterHeight is not a writeable propertySlide 57AnotherBMISpreadsheetSlide 59Methods that ChangessetWeight()setHeight()getBMI()Complete CodeSimilarities in the two ClassesSlide 66Slide 67InterfaceImplementing an InterfaceReal-World AnalogySlide 71Using Car Specification to ClassifyUsing Interface to ClassifyClass-based typingClass-based TypingInterface-based typingInterface-based TypingChanging the classCannot Instantiate SpecificationInterface as Syntactic SpecificationSlide 81Slide 82Pros and cons of two alternativesSlide 84ABMISpreadsheet vs. AnotherBMISpreadSheetTime-Space TradeoffSlide 87Relating Interface and Class NamesOverloadingOverloaded OperatorsAmbiguous ContextSlide 92Slide 93Slide 94Method Invocation SyntaxSlide 96Slide 97Original ABMIDriverModified ABMIDriverObject-based vs. conventional programmingUI Code and ObjectsSlide 102Slide 103Slide 104UI vs. Debugging CodeSlide 106CommentsRemoving Debugging CodeJavadoc ConventionsWhat to Comment?Slide 111Javadoc TagsImproving the StyleWhy Avoid Code Duplication?Example Changes: LB, InchesHow to avoid code duplication?Principle of Least PrivilegeOnly Public Methods in InterfacePure vs. Impure FunctionsImpure FunctionsSide EffectSlide 122Slide 123Declaring Named ConstantsVariables vs. Named ConstantsAccidental or Malicious ModificationLiterals, Named Constants, Constants, VariablesSlide 128What is a Magic Number?Slide 130More Code RepetitionRemoving Code RepetitionIndependent Code = MethodLocal vs. Global VariableSlide 135Identifier ScopeScopeMultiple definitionsOverriding ScopesScope of Public ItemsSlide 141Slide 142Slide 143Slide 144Slide 145Variable ScopeNamed-Constant ScopeNamed constant scopeSlide 149Slide 150Initializing DeclarationUn-initializing DeclarationUn-initialized VariableInitializing all VariablesSlide 155Instance-independent InstantiationInstance-dependent InitializationInstance-dependent InstantiationConstructorHow Java processes a constructor callDefault ConstructorSlide 162Case ConventionsErrorsStatic vs. non staticAnother Object-based ProblemAnother Example: PointSolutionRelated ProblemRelated SolutionDrawbacks of monolithic solutionsPoint InterfacePoint RepresentationsAlgorithmsClass: ACartesianPointClass: APolarPointUsing the Interface and its ImplementationsShared main methodShared print methodGet method CartesianGet method PolarBottom-up Programming with Coarse-grained stepsTop-Down with Finer StepsReal lifeObject-based Programming• Intuitive explanation• Using objects to read input• Creating objects• Style rulesManufactured ObjectsNatural ObjectsProgram Components ~ Physical Objects~ Program ObjectsProgram Objects ~ Manufactured ObjectsProgram Objectaddsubtractmethodsexecuteinvokecallmanufactured byacceleratebrakeoperationsperformClassinstance ofProgram ObjectClassification through Factoriesmanufactured bymanufactured byClassification through ClassesBufferReader InstanceBufferReaderinstance ofBufferReaderInstanceABMISpreadsheet InstanceABMISpreadsheetinstance ofABMISpreadsheet InstanceUsing vs. Creating Objects•Driving a car–Using BufferReader for input•Building a factory–Creating ABMISpreadsheetString InputString Inputpackage main;import java.io.BufferedReader;import java.io.InputStreamReader;public class AnInputPrinter { public static void main (String[] args) { System.out.println("Please enter the line to be printed"); System.out.println ("The input was: " + readString()); } static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static String readString() {try {return inputStream.readLine();} catch (Exception e) {System.out.println(e);return ""; } }}Classifying/ typing instancesInvoking operationconstructing new instancestatic BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static String readString() {try {return inputStream.readLine();} catch (Exception e) {System.out.println(e);return ""; } }}Reading a String• Wait for the user to enter a string on the next line.• In case the user terminates input before entring a string, return “” and print an error message.Objects constructed to allow reading of strings from System.inChained construction•Order light fixtures•Pass them to builder•Construct InputStreamReader instance•Pass it as a parameter to instance of BufferedReader•Understand better when we implement our own class static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));Byte input streamChar tokensLine tokenstry {return inputStream.readLine();} catch (Exception e) {System.out.println(e);return 0;}Try-Catch BlockException Object Program fragment that can cause exceptionException Handlerpublic class AnInputPrinter { ... …}import java.io.BufferedReader;public class AnInputPrinter { …. …... }Importing a Package new BufferedReader(…) new java.io.BufferedReader(...) package java.io;public class BufferedReader { ….}short namefull nameImport DeclarationPackage declaration makes full class name long Import declaration allows use of short nameImport all vs. selective importimport java.io.*;import java.util.*;•import all classes in java.io•convenient•can accidentally import and use undesired classes•must look at entire code to determine


View Full Document

UNC-Chapel Hill COMP 114 - Object-based Programming

Download Object-based Programming
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 Object-based Programming 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 Object-based Programming 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?