370 likes | 609 Views
Object-based Programming. Intuitive explanation Two concrete examples Calculators addition BMI Programming Environment. Computer Vs Program Model. CPU. Program. ???. Compiler. Structuring in Scripts. Speech. Introduction. Body. Conclusion. Paragraph 1. Paragraph 2.
E N D
Object-based Programming • Intuitive explanation • Two concrete examples • Calculators • addition • BMI • Programming Environment
Computer Vs Program Model CPU Program ??? Compiler
Structuring in Scripts Speech Introduction Body Conclusion Paragraph 1 Paragraph 2 Script components are abstract Sentence 1 Sentence 2 So are program components
Natural Objects Manufactured Objects Program Components ~ Physical Objects ~ Program Objects
operations manufactured by accelerate perform brake add instance of Class subtract execute Program Object invoke methods call Program Objects ~ Manufactured Objects Program Object
Classification through Factories manufactured by manufactured by
Classification through Classes instance of ASquareCalculator ASquareCalculator Instance ASquareCalculator Instance instance of ABMICalculator ABMICalculator Instance ABMICalculator Instance
A Simple Class publicclass ASquareCalculator { publicint square(int x) { return x*x; } }
2.2 Nature of a Function Domain/Parameter Values Range/Result Values 1 1 2 4 3 9 ... ... Mapping
Mathematics square: I I square(x) = x2 Java publicint square(int x) { return x*x; } Mathematics Vs Java Function Syntax Inspiration for Functional Languages
weight (kg) Body Mass Index height2 (metres) publicclass ASquareCalculator { publicint square(int x) { return x*x; } } ABMICalculator public class ABMICalculator { public int calculateBMI (int weight, int height) { return weight/(height*height); } }
Bare Bone Environment • MS-DOS Prompt (Command Interpreter) • Notepad (editor) • Jvc (J++ compiler) • Jview (J++ interpreter) • ObjectEditor
Using Notepad to Edit Autoexec notepad C:\autoexec.bat
Setting Path and Classpath • Path • List of Command Directories • Must have Jvc and Jview Directories • Class Path • List of Library Directories, Jar Files, Zip Files • Add oe.jar, shapes.jar, swingall,jar
previous classpath current directory jar files Reboot! Setting Path and Classpath SET PATH=%PATH%;C:"\Program Files\Microsoft Visual Studio\VJ98" SET CLASSPATH=%CLASSPATH%;.;D:\Java\lib\oe.jar;D:\Java\lib\shapes.jar;D:\Java\lib\swingall.jar may be zipped
creates ABMICalculator Source Code ABMICalculator Instance reads Java Compiler calculateBMI creates calls instantiates ABMICalculator Object (Byte) Code ObjectEditor main calls Program Development Process Text Editor Java Interpreter
Method Header Method Body Class Header Class Body Parameter Type Parameter Name Access Specifier Return Type Return Statement Return Expression Anatomy of a Class 1. public class ABMICalculator 2. { public double calculate BMI(double weight, double height 3. 4. { return weight/(height*height); 5. } 6. 7. }
Invoke method variables memory weight 0 height 0 1.77 Formal Vs Actual Parameters publicdouble calculateBMI(double weight, double height) { return weight / (height*height); } formal parameters assigned actual parameters 74
Syntax Error Semantics Error Logic Error Access Error Errors class ABMICalculator { double calculateBMI(double weight, double height) { return (height*heigh)/weight }
Line no char. no JVC Error Reporting ABMICalculator.java (3,3) : error J0232: Expected '{' or ';' ABMICalculator.java (3,3) : error J0021: Expected type specifier ABMICalculator.java (3,3) : error J0019: Expected identifier ABMICalculator.java (5,1) : error J0020: Expected 'class' or 'identifier'
Case Conventions • Start variable name with lowercase letter (weight). • Start class name with uppercase letter (ABMICalculator) • Start each new word with upper case letter (ASquareCalculator)
Object Editor Changes Case calculateBMI()
Identifiers • Reserved Words/KeyWords • double, class, int, public, return • boldface • Programmer-defined Names • Variable Names • Class Names • First character is letter • Other characters:letter, digit, _ • calculateBMI2 • calculate_BMI