object-based programming intuitive explanation two concrete examples calculators addition bmi...
Post on 21-Dec-2015
223 Views
Preview:
TRANSCRIPT
Object-based Programming
• Intuitive explanation
• Two concrete examples
• Calculators
• addition
• BMI
• Programming Environment
Structuring in Scripts
Speech
Introduction ConclusionBody
Paragraph 1 Paragraph 2
Sentence 1 Sentence 2
Script components are
abstract
So are program components
Program Objects ~ Manufactured Objects
Program
Object
add
subtract
methodsexecuteinvokecall
manufactured byaccelerate
brake
operations
perform
Classinstance of
Program
Object
Classification through Classes
ASquareCalculator
Instance
ASquareCalculator instance of
ASquareCalculator
Instance
ABMICalculator
Instance
ABMICalculator instance of
ABMICalculator
Instance
Mathematics Vs Java Function Syntax
Inspiration for Functional Languages
square: I Isquare(x) = x2
Mathematics
public int square(int x) {
return x*x;}
Java
ABMICalculator
Body Mass
Index
weight
height2
(kg)
(metres)
public class ASquareCalculator {
public int square(int x) {
return x*x; }
}
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
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
Setting Path and ClasspathSET 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
Reboot!
previous classpath
current directory jar files
may be zipped
Program Development ProcessText Editor
ABMICalculator Source Code
creates
Java Compiler
reads
ABMICalculator Object (Byte) Code
creates
Java Interpreter
ABMICalculator Instance
calculateBMI
instantiates calls
ObjectEditor
main
calls
ClassHeader
ClassBody
Anatomy of a Class
2.
3.
4.
5.
6.
7.
{
{
}
1.
}
AccessSpecifier
public class ABMICalculator
MethodHeader
Method Body
public double calculate BMI(double weight, double height
ReturnType
ReturnStatement
return
ReturnExpression
weight/(height*height);
ParameterName
Parameter Type
Formal Vs Actual Parameters
parametersactual
public double calculateBMI(double weight, double height) {
return weight / (height*height);}
parametersformal
Invoke method
weight 0
height 0
variables memory
assigned
74
1.77
Errors
class ABMICalculator {double calculateBMI(double weight, double height)
{return (height*heigh)/weight
}
Syntax Error
Semantics Error
Logic Error
Access Error
JVC Error Reporting
ABMICalculator.java (3,3) : error J0232: Expected '{' or ';'ABMICalculator.java (3,3) : error J0021: Expected type specifierABMICalculator.java (3,3) : error J0019: Expected identifierABMICalculator.java (5,1) : error J0020: Expected 'class' or'identifier'
Line no char. no
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)
top related